winforms 在动态创建的按钮上替换MousePosition线以列出窗体

gzszwxb4  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(58)

我在WinFormsC #中创建了一个程序,当你释放鼠标按钮1时,你可以动态地添加存储MousePosition.X和.Y的按钮。问题是,当我创建了一个以上的按钮,并使用这些按钮在x和y的2个列表中输入MousePositions时,按钮只是在单击它们时不断添加新的位置,并且一旦它们只是在x和y的2个列表中添加新的MousePositions,就不要用新的替换线。之后我有另一个按钮,当点击它时,它会遍历所有的MousePositons,并逐个点击那个点。基本上,我想要一个按钮,是独立的代码从1按钮到另一个,所以它改变了鼠标位置独立或跟踪哪个按钮被点击,并删除和添加新的MousePositions在列表中的旧MousePositions点,所以它不保持每次添加新的线。

private void **addBtn_Down**(object sender, MouseEventArgs e)
        {
            
            Cursor = Cursors.PanNW;

        }
private void **addBtn_Up**(object sender, MouseEventArgs e)
  • //将MP.X和.Y添加到要在button5_Click中使用的列表中。但我不知道如何更换电线的特定按钮,我再次按下更换它们,而不是添加新的电线到列表中。
{

            int xMouse = MousePosition.X;
            int yMouse = MousePosition.Y;
                xInput.Add(xMouse);
                yInput.Add(yMouse);
            Cursor = DefaultCursor;
            
        }
private void **button6_Click**(object sender, EventArgs e)
  • //这是一个动态创建新按钮的按钮,并订阅了addBtn_Down和addBtn_Up方法,这些方法包含了要做什么的代码。
{
                Button newButton = new Button();
                newButton.Text = "Drag me to Desired Position";
                newButton.Size = new Size(82, 75);
                newButton.Location = new Point(leftX, topY);//They arent in this code
                
                newButton.MouseDown+=addBtn_Down;
                newButton.MouseUp += addBtn_Up;
                Controls.Add(newButton);
        }
private async void **button5_Click**(object sender, EventArgs e)
  • //当点击时,循环遍历所有的MousePositions并点击那里。*
{

            for (int i = 0; i < xInput.Count; i++)
            {
                    ClickAtClass.ClickAt(xInput[i], yInput[i]);
                        await Task.Delay(1000);

            }

我尝试了一个星期的ChatGPT和YouTube,但没有任何工作的方式,我想要的。

a6b3iqyw

a6b3iqyw1#

我想出来了!您需要使用列表简答>索引(列表中的按钮索引,您想要存储的内容:(MyMousePosition.X和MousePosition.Y))和RemoveAt((Button)发送方,它具有来自按钮列表的单击按钮信息)。**完整答案:*1)**您需要一个Button List List来存储按钮,并将它们作为自己的Separate对象,以便您稍后可以使用(Button)sender (单击的按钮) 和2个列表来存储MousePosition.X和MousePosition. Y。所以列出xInput; List yInput;**2)**然后你需要在你的MouseButtonUp(object sender,MouseEventArgs e)方法的Method中创建一个Button对象,它等于(Button)sender。然后在你创建的那个(Button)sender的同一个方法中,把它赋值给一个变量,并使用你的buttons和indexOf的List(其中的变量表示你赋值的Clicked button(sender))。**3)**它应该是这样的:

private void addBtn_Up(object sender, MouseEventArgs e)
        {
            Button clickedButton = (Button)sender; //what button we clicked
            int listBtnIndex= listButtons.IndexOf(clickedButton);//assign index to the list
            int xMouse = MousePosition.X;//I put the information in a variable
            int yMouse = MousePosition.Y;
            
                xInput.Insert(listBtnIndex, xMouse);//Then put the xMouse cords to the index of the Clicked button in the list
                yInput.Insert(listBtnIndex, yMouse);// And yMouse to the same index of the button in the list of yInput.
        }

之后,我使用了一个for循环来循环所有的xInput List和yInput List的MousePositions循环相同的索引的aka xInput[i],yInput[i]//基本上是1,1然后2,2,3,3等等,你创建的按钮有多少就有多少循环:

for (int i = 0; i < xInput.Count; i++)
            {
                
                    *//MethodToClickAtThePositions*(xInput[i], yInput[i]);
                        await Task.Delay(1000);

            }

若要在再次使用同一按钮时删除Cords,并使用RemoveAt(clickedButton)放置新的一次,请执行以下操作:

Button clickedButton = (Button)sender;//Create another object sender for when you hold the button down to remove the previous List of xInput and yInput based on the ClickedButton
        int listBtnIndex = listButtons.IndexOf(clickedButton);
            xInput.RemoveAt(listBtnIndex);*//Remove both x and y input at the same index of the clicked button*
            yInput.RemoveAt(listBtnIndex);*//After that add the same logic from the insert method from Above after this code so it first removes the old cords and adds the new onces.*

**4)**就是这样!希望对某人有帮助!WinForms VS 2022

相关问题