替换动态创建的按钮上的 MousePosition 线以列出表单

问题描述 投票:0回答:1

我正在 Win Forms c# 中创建一个程序,您可以在释放鼠标按钮 1 时动态添加存储 MousePosition.X 和 .Y 的按钮。 问题是,当我创建了超过 1 个按钮并使用这些按钮在 x 和 y 的 2 个列表中输入 MousePositions 时,按钮在单击它们时只会不断添加新位置,并且一旦它们不断添加,就不要用新的电线替换电线新的 MousePositions 到我的 x 和 y 的 2 个列表中。 之后,我有另一个按钮,单击该按钮时会遍历所有 MousePositons 并一一单击该位置。 基本上我想要一个独立于从一个按钮到另一个按钮的代码的按钮,这样它就可以独立地更改鼠标位置,或者跟踪单击了哪个按钮,然后在旧鼠标位置的列表中删除并添加新的鼠标位置,这样它就不会继续添加每次都有新电线。

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) 

//单击时循环浏览所有鼠标位置并单击那里。

{

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

            }

我尝试了 ChatGPT 和 youtube 一周,但没有任何效果符合我想要的效果。

c# list winforms button
1个回答
0
投票

我想通了! 您需要使用列表简答> INSERT(列表中创建的按钮索引,您要存储的内容:(My MousePostion.X 和 MousePostion.Y))和RemoveAt(具有单击按钮的(按钮)发送者来自带有按钮的列表的信息)。 完整答案: 1) 您需要一个按钮列表列表来存储按钮并将它们作为自己的单独对象,以便稍后可以使用(按钮)发送者(单击的按钮) 和 2 个列表来存储 MousePosition 跟踪它们.X 和 MousePosition.Y。所以列出xInput;列出 y 输入; 2) 然后您需要在您拥有的 MouseButtonUp(object sender, MouseEventArgs e) 方法的方法中创建一个等于 (Button)sender 的 Button 对象。然后在您创建该 (Button) 的同一方法中发送者将其分配给一个变量并使用按钮列表和indexOf(以及如何分配您分配的单击按钮(发送者)的变量)。 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 循环循环遍历 MousePositions 的所有 xInput 列表和 yInput 列表,循环相同的索引,即 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);

            }

如果您再次使用相同的按钮并使用RemoveAt(clickedButton)放置新的按钮,则要删除电线:

Button clickedButton = (Button)sender;//创建另一个对象sender,用于当按住按钮时根据ClickedButton移除之前的xInput和yInput列表 int listBtnIndex = listButtons.IndexOf(clickedButton); xInput.RemoveAt(listBtnIndex);//删除与单击按钮相同索引处的x和y输入 yInput.RemoveAt(listBtnIndex);//在此代码后面添加与上面的插入方法相同的逻辑,因此它首先删除旧的线并添加新的一次。

4) 就是这样!希望它对某人有帮助!这就是 WinForms VS 2022 的全部

© www.soinside.com 2019 - 2024. All rights reserved.