简体   繁体   English

从ListBox逐行读取

[英]Reading from ListBox Line by line

I am creating a program where the user is pressing buttons like move 10 and move 1 to move a robot and when a user presses the button it will add a text in a listbox like "MoveRobot1" so when the user has finished pressing the buttons and presses the Play button, the program should go down the list line by line moving the robot based on the list at an interval of 300ms but I don't understand how to make it read line by line instead of all at once when I press Play. 我正在创建一个程序,其中用户按下按钮,例如移动10并移动1以移动机器人,当用户按下按钮时,它将在“ MoveRobot1”之类的列表框中添加文本,因此当用户完成按钮的按下操作后,按“播放”按钮,程序应以300ms为间隔逐行移动基于列表的机器人,但我不知道如何按行而不是一次按行读取它。

private void BtnPlay_Click(object sender, EventArgs e)
    {
        this.WorkProgress += new WorkProgressHandler(DoWork);
        _counter = 0;

        this.robot.Reset();
        this.MoveRobot(0);
        string query1 = "MoveRobot(1)";
        string query2 = "MoveRobot(10)";
        for (int i = 0; i < MoveBox.Items.Count; i++)
        {
            if (MoveBox.Items[i].ToString() == query1)
            {
                this.MoveRobot(1);
                DoWork();

            }
            if (MoveBox.Items[i].ToString() == query2)
            {
                this.MoveRobot(10);
                DoWork();
            }
        }
    }

 private void DoWork()
    {
        _counter++; // increment the counter
    }

Use Thread.Sleep in the end of every iteration : 在每次迭代结束时使用Thread.Sleep

for (int i = 0; i < MoveBox.Items.Count; i++)
{
    if (MoveBox.Items[i].ToString() == query1)
    {
        this.MoveRobot(1);
        DoWork();
    }
    if (MoveBox.Items[i].ToString() == query2)
    {
        this.MoveRobot(10);
        DoWork();
    }
    Thread.Sleep(300);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM