按ENTER键更改箭头的位置

时间:2014-12-19 09:45:46

标签: c# winforms refresh

我正在开发一个C#项目,我想通过按ENTER键来改变箭头的位置,不同的comboBox选择具有不同的迭代大小。实际上它可以工作,但问题是我无法在更改组合框选择之前刷新表单。我希望逐步看到迭代,但如果我更改了comboBox选项,它会移动。这是代码:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (comboBox1.SelectedIndex == 0)
            {

                this.BackColor = Color.Black;
                label1.ForeColor = Color.Silver;
                label1.Text = "Environment is Space";
                pictureBox2.Image = list.Images[4];
                t = 100; // iteration amount

            }

            else if (comboBox1.SelectedIndex == 1)
            {
                this.BackColor = Color.PaleTurquoise;
                label1.Text = "Environment is Water";
                pictureBox2.Image = list.Images[3];
                t = 50; // iteration amount

            }

            else if (comboBox1.SelectedIndex == 2)
            {
                this.BackColor = Color.DarkGoldenrod;
                label1.ForeColor = Color.Firebrick;
                label1.Text = "Environment is Honey";
                pictureBox2.Image = list.Images[2];
                t = 20; // iteration amount
            }

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            // Drawing arrow
            Pen pen = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
            pen.StartCap = LineCap.ArrowAnchor;
            pen.EndCap = LineCap.RoundAnchor;
            e.Graphics.DrawLine(pen, x+50, 200, x, 200);

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // pressed Enter => change x
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
                if (x < y)
                {
                    x += t;

                }
            }
        }

更清楚: 我想要:点击 - &gt;输入+ Move-&gt;箭头+点击 - >输入+移动 - &gt;箭头
现在它的工作方式如下:Click-&gt;输入+更改 - &gt; comboBox + Move-&gt;箭头

非常感谢!

1 个答案:

答案 0 :(得分:0)

你想要一个计时器解决方案吗?

System.Timers.Timer timer = new System.Timers.Timer(500);
        timer.Elapsed += (s, e) => { 
            this.Invoke((MethodInvoker)delegate { comboBox1.SelectedIndex = (comboBox1.SelectedIndex + 1) % comboBox1.Items.Count; });
        };
        timer.Start();

很难理解你是什么意思。