You're declaring aRadio
as a local variable within your constructor. You need to declare it as an instance variable, and just assign it a value within your constructor:
// TODO: Give this a better name
private readonly string[] aRadio;
// TODO: Give your form a better name too
public Form1()
{
InitializeComponent();
// TODO: You might want to reconsider reading files in a GUI constructor, too
// TODO: Use Path.Combine(strTemp, "rstations.txt" instead of concatenation
string strRadio = Utils.ReadFile(strTemp + @"
stations.txt");
aRadio = strRadio.Split(new string[] { "#" },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < aRadio.Length; i += 2)
{
listBox.Items.Add(aRadio[i]);
}
}
I wouldn't be surprised if you could do rather better than this approach though, by adding a custom object (or just a KeyValuePair<string, string>
) to the list box and binding the display part through properties. That way you could get the selected item rather than the selected index... there's no need to keep text/value pairs like this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…