-1

How do you guys actually use the StreamReader to read the .txt file and match with two of my Combobox's text such as SGD - Singapore Dollar and USD - US Dollar so that it writes on the label that shows the number of 1.26?

Exchange.txt:

SGD - Singapore Dollar || USD - US Dollar = 1.26

Here's the code:

private void GetExchangeRate()
{
    using (StreamReader sr = new StreamReader("Exchange.txt"))
    {
        string[] store = new string[100];
        int index = 0;
        string line;

        while ((line = sr.ReadLine()) != null)
        {
            store[index] = line;
            index++;
            lblexchange.Text = sr.ReadLine();
        }
    }
}

private void tocountry_SelectedIndexChanged(object sender, EventArgs e)
{
    btnupdate.Enabled = true;
    txtvalue.Enabled = true;
    GetExchangeRate();
}

In the end, the label did not show the value of 1.26. I don't know what's wrong with it. I need help

4

2 回答 2

1

你为什么不直接使用

File.ReadAllLines("Exchange.txt")
它将返回字符串数组中的所有行。

于 2013-07-24T07:29:34.740 回答
0

你可以这样做

private void GetExchangeRate()
{
    string[] lines = File.ReadAllLines("Exchange.txt");

    foreach (var line in lines) { 
        //Suppose your line contains 'Singapore' and you want to do somthing if line contains the singapore then you should do as 
         if(line.Contains("Singapore"))
          {
               lblDisplay.Text = "Singapore"
          }
       //Do your functionality that is which line to display depending upon country
       // You can match the line and display them according to your need  
    }
 }
于 2013-07-24T10:32:38.683 回答