我打算将此功能添加到我的应用程序中,该功能将在用户导入的标签上显示一行文本。
工作原理:用户导入文本文件,然后在Button
后单击Label
,文本将变为文本文件上的第一行用户导入。在 X 秒后,它将更改为第二个秒。基本上,它将垂直向下移动直到最后一行,然后停止。
List<string> lstIpAddress = new List<string>();
int nCount = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 30000;
}
private void LoadBTN_Click(object sender, EventArgs e)
{
OpenFileDialog load = new OpenFileDialog();
if (load.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
load.InitialDirectory = Environment.SpecialFolder.Desktop.ToString();
load.Filter = "txt files (*.txt)|*.txt";
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(load.OpenFile()))
{
string line;
while ((line = r.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int nlstItem = 0; nlstItem < lstIpAddress.Count; nlstItem++)
{
listBox1.Items.Add(lstIpAddress[nlstItem]);
}
label2.Text = listBox1.Items[nCount].ToString();
nCount++;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
label2.Text = listBox1.Items[nCount].ToString();
timer1.Start();
}
答案 0 :(得分:0)
您需要将nCount++;
移至Timer1单击事件。另外,您应该检查nCount
是否在listBox1.Items.Count
的范围内,否则会出现异常。
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
nCount++;
if (nCount < listBox1.Items.Count)
{
label2.Text = listBox1.Items[nCount].ToString();
}
timer1.Start();
}