【问题标题】:Change the location of an arrow by pressing ENTER key按 ENTER 键更改箭头的位置
【发布时间】:2014-12-19 09:45:45
【问题描述】:

我正在处理一个 C# 项目,我想通过按 ENTER 键来更改箭头的位置,并针对不同的组合框选择使用不同的迭代大小。实际上它有效,但问题是我无法在更改组合框选择之前刷新表单。我想逐步查看迭代,但如果我更改组合框选择,它会移动。代码如下:

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;

                }
            }
        }

为了更清楚: 我想要:单击-> 输入 + 移动-> 箭头 + 单击-> 输入 + 移动-> 箭头
现在它的工作方式如下:单击-> Enter + Change-> comboBox + Move->Arrow

非常感谢!

【问题讨论】:

  • 数组是一种逻辑数据结构,我不知道你会如何移动它。您能否提供一个完整的可编译示例,因为您的描述不是很清楚。
  • 我打算写一个箭头而不是数组,对不起。我编辑了问题。

标签: c# winforms refresh


【解决方案1】:

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

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();

很难理解你的意思。

【讨论】:

  • 不,我要一步一步看变化。更改组合框选择后,我不想看到整个更改。我只想看到 Click-> Enter + Move-> Arrow + Click-> Enter + Move -> Arrow。但是,它现在就像 Click-> Enter + Change-> comboBox + Move->Arrow 一样流动。