简体   繁体   English

如何在运行时移动新的(自定义)按钮?

[英]How to move a new (custom) button at run time?

I have a problem with my winforms C# project.我的 winforms C# 项目有问题。

I want to move a new (custom) button around the form (at run time).我想在表单周围移动一个新的(自定义)按钮(在运行时)。 How can I do that?我怎样才能做到这一点?

    Button[] buttons = new Button[1000];
        int counter = 0;
        Button myText = new Button();
private void button2_Click(object sender, EventArgs e)
        {
                Button myText = new Button();
                myText.Tag = counter;
                myText.Location = new Point(x2,y2);
                myText.Text = Convert.ToString(textBox3.Text);
                this.Controls.Add(myText);
                myText.MouseMove += new MouseEventHandler(myText_MouseMove);
                myText.MouseDown += new MouseEventHandler(myText_MouseDown);
                buttons[counter] = myText;
                counter++;
        }
 public void myText_MouseMove(object sender, MouseEventArgs e)
        {
            int s = e.GetHashCode();
            int check = 0;
            for (int i = 0; i < counter; i++)
            {
                if (buttons[i].GetHashCode() == s)
                    check = i;
            }
            if (e.Button == MouseButtons.Left)
            {
                buttons[check].Left += e.X - move.X;
                buttons[check].Top += e.Y - move.Y;
            }
        }
        void myText_MouseDown(object sender, MouseEventArgs e)
        {
            move = e.Location;
        }

I use the code above to create the new button and I am trying to move it around the form.我使用上面的代码来创建新按钮,并试图在表单中移动它。

If I code for just 1 button I am able to move it but, I want to be able to do this for more buttons as well.如果我只为 1 个按钮编码,我可以移动它,但是,我也希望能够为更多按钮执行此操作。

Try this试试这个

public void myText_MouseMove(object sender, MouseEventArgs e)
    {
        Button button = (Button)sender;
        if (e.Button == MouseButtons.Left)
        {
            button .Left += e.X - move.X;
            button .Top += e.Y - move.Y;
        }
    }

I've come across this thread while doing something similar.我在做类似的事情时遇到了这个线程。
Even though the thread is quite old;即使线程很旧; I'm sharing the way I've achieved this.我正在分享我实现这一目标的方式。

Notes: Unlike the operator question above;注意:与上面的操作员问题不同; in the example bellow;在下面的例子中; I'm not using a Button[] (array) with 1000 buttons;我没有使用带有 1000 个按钮的 Button[](数组); (but this can be easily changed). (但这可以很容易地改变)。

In the example bellow I have: A Form with 1 Button on it.在下面的示例中,我有:一个带有 1 个按钮的表单。 When this button is clicked;单击此按钮时; it will create a new (Custom) Button (hence; having two button click events in the code simply for the test purpose).它将创建一个新的(自定义)按钮(因此;代码中有两个按钮单击事件只是为了测试目的)。

Bellow you can find: A way to generate the button;波纹管你可以找到:一种生成按钮的方法; and so how to drag it around the form.以及如何在表单周围拖动它。

/* Variables */

// Used to Store the Mouse Location on the Screen when Button is Clicked
private Point mouseDownLocation;

// Used to know if the Button can be Moved.
private bool isMoveable = false;

// Used to prevent clicking the button when being dragged
private bool clickEnabled = true;


/// <summary> Occurrs when the visible button on the Form is clicked. </summary>
private void Button_Click(object sender, EventArgs e)
{
    // Create a variable to set the Size of the new Button.
    Size size = new Size(100, 100);

    // Create the Button. Specifying the Button Text and its Size.
    CreateButton("Button Text", size);
}

/* Custom Button : Create & Configure the Button */
private void CreateButton(string btnText, Size btnSize)
{
    // Create the Button instance:
    Button btn = new Button();

    // Custom Button Style Configuration Code.
    // i.e: FlatStyle; FlatAppearance; BackColor; ForeColor; Text; Size; Location; etc...
    btn.FlatStyle = FlatStyle.Standard;
    btn.FlatAppearance.BorderColor = Color.White;
    btn.BackColor = Color.Purple;
    btn.ForeColor = Color.White;
    btn.Text = btnText;
    btn.Size = btnSize;
    btn.Location = new Point(100, 100);

    // Custom Button Event.
    btn.Click += new EventHandler(CustomButton_Click);
    btn.MouseDown += new MouseEventHandler(CustomButton_MouseDown);
    btn.MouseMove += new MouseEventHandler(CustomButton_MouseMove);
    btn.MouseUp += new MouseEventHandler(CustomButton_MouseUp);

    // Add Button to Control Collection.
    Controls.Add(btn);

    // Show Button.
    btn.Show();
}


/* Custom Button Events */

/// <summary> Occurrs whenever the Custom Button is Clicked </summary>
private void CustomButton_Click(object sender, EventArgs e)
{
    if (clickEnabled)
    {
        // Note: The "for" loop bellow lets you get the current button; so you can drag it.

        Button btn = (Button)sender;

        // Iterate over the Form Controls
        for (int i = 0; i < Controls.Count; i++)
        {
            // If the Button Clicked Corresponds to a Found Index in Control Collection...
            if (i == Controls.IndexOf(btn))
            {
                // Perform Corresponding Button Action.
                MessageBox.Show(btn.Name + " Clicked!");
            }
        }
    }
}

/// <summary> Occurrs whenever Left Mouse Button Clicks the Button and is Kept Down. </summary>
private void CustomButton_MouseDown(object sender, MouseEventArgs e)
{
    // Check if Left Mouse Button is Clicked.
    if (e.Button == MouseButtons.Left)
    {
        // Set mouseDownLocation (Point) variable, according to the current mouse location (X and Y).
        mouseDownLocation = e.Location;

        // Enable the hability to move the Button.
        isMoveable = true;
    }
}

/// <summary> Occurrs whenever Left Mouse Button is Down and Mouse is Moving the Button. </summary>
private void CustomButton_MouseMove(object sender, MouseEventArgs e)
{
    Button btn = (Button)sender;

    // If the hability to move the button is "true" and the mouse button used to drag the button is the left mouse button:
    if (isMoveable)
    {
        if (e.Button == MouseButtons.Left)
        {
            // Set the Button location X and Y...
            btn.Left = e.X + btn.Left - mouseDownLocation.X;
            btn.Top = e.Y + btn.Top - mouseDownLocation.Y;
        }
    }

    // Disable Click hability (See: Note at the end of this post).
    clickEnabled = false;
}

/// <summary> Occurrs whenever Left Mouse Button is not longer being hold down (Left Mouse Button is Up). </summary>
private void CustomButton_MouseUp(object sender, MouseEventArgs e)
{
    // Disables the hability to move the button
    isMoveable = false;
    clickEnabled = true;
}
Note: The Custom Button Click interferes with the MouseDown Event. In a "simple" manner: 
I updated the code by including a boolean value to prevent clicking the button when actually it is being dragged.
Hope this helps you out understanding how to accomplish this feature.

Hope this helps people out achieving this goal.希望这可以帮助人们实现这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM