本文介绍了我们可以在鼠标点击事件中使用参数c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用循环用于二维数组的按钮。我不知道如何知道阵列中哪些按钮被点击了 这是我的代码: { for(int j = 0; j< 100; i ++) { arrButton [i,j] = new Button(); arrButton [i,j] .Size = new Size(size1button,size1button); arrButton [i,j] .Location = new Point(j * size1button,i * size1button); arrButton.Click + = new EventHandler(arrButton_Click); } } 可以使用参数i,j进行鼠标点击事件喜欢: private void arrButton_Click(object sender,EventArgs e,int i,int j) { //我的想法:将i,j添加到另一个int [,]数组以跟踪被点击的按钮} 如果退出,如何正确写?或者您可以推荐或方法来确切了解数组中按钮的位置?解决方案您不能将EventHandler签名更改为包含你的我和 j 。 但是,你可以从已经传递给 arrButton_Click 方法的信息获取该信息。由于您将每个按钮的位置设置为 new Point(j * size1button,i * size1button),您可以获得每个 i size1button 。 ,c>和 j 要获得该位置,您可以使用发件人,这是您的 Button (a必须): private void arrButton_Click(object sender,EventArgs e) {按钮btnClicked =(按钮)发件人; int i = btnClicked.Location.Y / size1button; int j = btnClicked.Location.X / size1button; } 另外,代码你目前用于创建按钮有几个错误。 首先,你永远不会增加 j ;第二个循环 i ++ 。 其次,如果您希望按钮出现,您必须将其添加到窗体的控件中。 最后,我不认为您的表单上可以有10 000个活动按钮,请尝试较少的数字,例如25。 所以修正后的代码如下所示: for(int i = 0; i { for(int j = 0; j< 5; j ++) { arrButton [i,j] = new Button() arrButton [i,j] .Size = new Size(size1button,size1button); arrButton [i,j] .Location = new Point(j * size1button,i * size1button); arrButton [i,j] .Click + = arrButton_Click; Controls.Add(arrButton [i,j]); } } 您还可以注意到,我删除了您的新的EventHandler ,这是多余的。 I use the loop for a two dimensions array of buttons. I don't know how to know exactly which buttons in array were clicked or notHere are my code: for (int i = 0; i < 100 ; i++) { for (int j=0 ; j< 100; i++) { arrButton[i, j] = new Button(); arrButton[i,j].Size = new Size(size1button, size1button); arrButton[i,j].Location = new Point(j*size1button, i*size1button); arrButton.Click += new EventHandler(arrButton_Click); } }Can I use parameters i, j for mouse click event like: private void arrButton_Click(object sender, EventArgs e, int i, int j) { //my idea : add i, j to another int[,] array to keep track of buttons which were clicked }If this exits, how to write it correctly? Or can you recommend or method to know exactly where the button was clicked in array ? 解决方案 You cannot change the EventHandler signature to include your i and j.However, you can get that information from what is already passed to the arrButton_Click method. Since you set the location of each button as new Point(j*size1button, i*size1button), you can get each i and j component back by dividing the location of your button by size1button.To get that location, you can use the sender, which is your Button (a cast is necessary):private void arrButton_Click(object sender, EventArgs e){ Button btnClicked = (Button) sender; int i = btnClicked.Location.Y / size1button; int j = btnClicked.Location.X / size1button;}Also, the code you're currently using to create the buttons have a couple errors.First, you're never incrementing j; the second loop does i++. Second, if you want your buttons to appear, you have to add them to your Form's Controls. Finally, I don't think you can have 10 000 active buttons on your form, try a lower number, like 25.So the corrected code would look like:for (int i = 0; i < 5; i++){ for (int j = 0; j < 5; j++) { arrButton[i, j] = new Button(); arrButton[i, j].Size = new Size(size1button, size1button); arrButton[i, j].Location = new Point(j*size1button, i*size1button); arrButton[i, j].Click += arrButton_Click; Controls.Add(arrButton[i,j]); }}You can also notice that I removed your declaration of new EventHandler, which was redundant. 这篇关于我们可以在鼠标点击事件中使用参数c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 18:22