如何在运行时移动按钮?

时间:2012-04-21 13:25:30

标签: c# winforms button move

我的winform c#项目有问题。

我想移动我在运行时围绕表单创建的新按钮。我怎么能这样做?

    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;
        }

我使用该代码制作新按钮,然后移动他。现在我想在表格中移动它。

如果我只是按一个按钮,我可以移动它,但如果它超过一个,那就是一个问题。

如果有人可以提供代码,修理他或者只是帮助我,这对我来说非常好! tnx:)

2 个答案:

答案 0 :(得分:2)

试试这个

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;
        }
    }

答案 1 :(得分:0)

我在执行类似操作时遇到了这个线程。
即使线程很旧;我正在分享实现这一目标的方式。

注释: 与上面的运算符问题不同;在下面的例子中;我没有使用带有1000个按钮的Button [](数组); (但这很容易更改)。

在下面的示例中,我有: 一个带有1个按钮的表单。 单击此按钮时;它将创建一个新的(自定义)按钮 (因此,在代码中仅出于测试目的而具有两个按钮单击事件)。

您会发现以下内容:一种生成按钮的方法;以及如何将其拖到窗体周围。

/* 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.

希望这可以帮助人们实现这一目标。