所以我有这段代码可以将文本文件中的项目添加到组合框中以进行分配。我将如何着手将所有项目一起添加到文本文件中,而不是像我现在所做的那样一个接一个地添加。
private void Form1_Load(object sender, EventArgs e)
{
TextReader tr;
try
{
cboCity.Items.Clear();
tr = File.OpenText("C:\\Users\\Alexander\\Desktop\\CPI Institute\\CPR\\Debugging\\cities.txt");
for (int counter = 4; counter < 5; counter++)
{
cboCity.Items.Add(tr.ReadLine());
cboCity.Items.Add(tr.ReadLine());
cboCity.Items.Add(tr.ReadLine());
cboCity.Items.Add(tr.ReadLine());
tr.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error opening cities file", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
请您参考如下方法:
你可以使用
cboCity.Items.AddRange(File.ReadAllLines("C:\\Users\\Alexander\\Desktop\\CPI Institute\\CPR\\Debugging\\cities.txt"));
一行完成。
