【问题标题】:Ask for user input on button click event in C#在 C# 中的按钮单击事件上要求用户输入
【发布时间】:2017-06-02 13:28:58
【问题描述】:

我正在制作一个国际象棋游戏,并且我正在动态使用按钮数组,因此我对所有按钮都使用了单击事件。我想要求用户输入按钮的单击事件(单击按钮后用户想要放置该块的位置)。我怎么能做到这一点,因为我是 C# 的新手并且已经尝试了很多但无法弄清楚。

这是我的代码:

    private void Btn_Click(object sender, EventArgs e)
    {
        //for black pieces

        
        Button btn2 = new Button();
        btn2 = sender as Button;
        int k;
        int l;
        for (int i=0; i<8; i++)
        {
            for(int j=0; j<8; j++)
            {
                if (btn2.BackgroundImage == blackpawn)
                {
                    if (btn2 == btn[i, j])
                    {
                        //here i want to ask user where he wants to put the piece
                        btn[i, j].BackgroundImage = null;
                        k = ++i;
                        l = j;
                        btn[k, l].BackgroundImage = blackpawn;
                        btn[k, l].BackgroundImageLayout = ImageLayout.Stretch;

                    }   
                }
            }
        }
        
    }

【问题讨论】:

  • 您不需要明确询问用户输入。在第一次点击用户选择一件。在第二次单击时,用户选择该作品的新位置。轮流为两个用户重复,直到游戏结束
  • /*在第一次点击时用户选择一个。在第二次单击时,用户选择该作品的新位置。*/我不知道该怎么做。这正是我在问题中提出的问题。

标签: c# winforms user-interface usability


【解决方案1】:

我不知道您是否正在努力在动态按钮上创建事件,只是找到一个按钮,所以两者都做不好。

在创建按钮时,需要给它添加事件。

Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);

//protected void button_Click (object sender, EventArgs e) { }

(引自此处:How can i create dynamic button click event on dynamic button?

或者为了找到那个按钮,可能会遍历表单上的控件并识别你想要的那个?

foreach (Control item in this.Controls)
       {
          if (item.GetType() == typeof(Button) && item.Name == "random coordinate")
               {
                 //Do WORK
               }
       }

【讨论】:

  • 我没有遍历我已经完成的按钮。我想问一下如何要求用户输入以在第一次单击时将按钮的图片移动到用户第二次单击的另一个按钮。
  • 在这种情况下,我认为使用按钮不是执行此操作的“正确”方式。但是您可以简单地处理鼠标单击事件并获取当时鼠标的坐标。记录它,然后将按钮设置到该位置。
【解决方案2】:

你不必做任何复杂的事情,这样的事情就可以了

bool firstClick = true;

private void Btn_Click(object sender, EventArgs e)
{
    if (firstClick)
    {
        // Do the stuff you'd like to do on the first click.

        firstClick = false; // Remember to set firstClick to false.
    }
    else
    {
        // Do the stuff you'd like to do on the second click.

        firstClick = true; // Remember to set firstClick to true.
    }
}

【讨论】:

    【解决方案3】:

    也许保留源和目标按钮的引用会有所帮助:

    Button FirstButton = null, SecondButton = null;
    
            private void ButtonSelected(object sender, EventArgs e)
            {
                if(FirstButton == null && SecondButton == null) // no button already selected
                {
                    FirstButton = sender as Button;
                }
                else if(FirstButton != null && SecondButton == null) // one button selected, its second's turn
                {
                    SecondButton = sender as Button;
                    // your code here to move chess pieces from first button to second button
                    SecondButton.BackgroundImage = FirstButton.BackgroundImage;
                    FirstButton.BackgroundImage = null;
                    // and some other stuff
    
                    FirstButton = null; SecondButton = null; // Set both buttons to null after one piece move.
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2016-11-19
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      相关资源
      最近更新 更多