-1
I need to open the txt file with the data already inserted with three columns and separated by ';' and after opening the file I insert it into an Array with List, save the data and need the values inserted inside the file to appear in a textbox. However I need to take an exact and/or close value of the value typed in a previous textbox and these value is in column 1 of the file and when the value is selected I need it to show me the value of column 3 of the same row of the selected value.
I already managed to open the file, throw the data of the file in the Array, but it does not return me the values of column 3 that I need. I finished this code, but it shows me the last line of the file with the three columns. I believe the error is from the foreach, because it reads only the last line. And I don’t know how to do an If (if it’s the If I need to do) so that it reads the value of the previous textbox and read the next value in the file and select column 3. I have tried to make if or insert the iterations inside the code in the foreach, but no success.
Note: I have read several websites/forums about this, but none was according to what I need.
In the txt file is thus the data:
For example: the value typed in the previous textbox gave 173.10 so the next value in the txt file is 172.0585213 so I want it to select this value and show the value of the third column (16) in the following textbox.
I’ll enter my code below:
List<string> lista = new List<string>();
string dados = (@"C: .... .txt"); // // caminho do documento em C: com a extensão em .txt
string linha; // //declara uma variável para receber linha por linha do doc.
using (StreamReader Str = new StreamReader(dados)) // a leitura do arquivo.
{
while ((linha = Str.ReadLine()) != null) //cria um loop para adicionar linha por linha do doc até sua ultima linha.
{
lista.Add(linha);
}
for (int i = 0; i < dados.Length; i++) // Percorro o array e para cada linha.
{
foreach (var item in lista)
{
var coluna1 = Convert.ToInt32(item[0]);
var coluna3 = Convert.ToInt32(item[2]);
var valores = File.ReadAllLines(dados).Select(linha => linha.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
.Where(Split => Split.Length >= 3).Select(Split => Split[2]).ToList();
Txtbox2.Text = (item).ToString();
}
}
It’s very confusing... is it Textbox or Listbox/Combobox? It could be a text box and a list box, where the list items are filtered based on the value typed in the text box, would that be?
– Kiritonito
What do you mean
Convert.ToInt32(item[0]);
read the first character? Why are you reading the file twice????– Leandro Angelo
@Kiritonito That’s right, I want it to Filter in the file the value typed in the previous textbox and and and show the value in the next textbox (but it would be the value of the third column as it will filter the value of column 1). I will clarify further in the question.
– Cristina
@Leandroangelo I wouldn’t have to insert the items (columns) of my txt file to make it filter? Saying that column 1 is [0] and column 3 is [2].
– Cristina