在编辑按钮上激活MouseeEvent单击C#

在编辑按钮上激活MouseeEvent单击C#,c#,winforms,C#,Winforms,我有带文本框的表单,它们有MouseEvent(MouseMove,MouseDown),它们在表单加载时被启用,但我的问题是当我单击编辑按钮时如何调用它们,这样就可以移动文本框了 我的代码: private void textBox_MouseMove(object sender, MouseEventArgs e) { TextBox txt = sender as TextBox; foreach (TextBox text in textBoxs

我有带文本框的表单,它们有MouseEvent(MouseMove,MouseDown),它们在表单加载时被启用,但我的问题是当我单击编辑按钮时如何调用它们,这样就可以移动文本框了

我的代码:

 private void textBox_MouseMove(object sender, MouseEventArgs e)
    {
        TextBox txt = sender as TextBox;
        foreach (TextBox text in textBoxs)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (txt.Name == text.Name)
                {
                    txt.Left = e.X + txt.Left - MouseDownLocation.X;
                    txt.Top = e.Y + txt.Top - MouseDownLocation.Y;
                }
            }
        }
    }

    private void textBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

   private void btnEdit_Click(object sender, EventArgs e)
    {
        btnEdit.Visible = false;
        btnPrint.Visible = false;
        btnSave.Visible = true;

        //Want to call mouse function here.

    }

有什么建议吗?

如果我理解您的帖子,您希望在单击
btnEdit
按钮后文本框功能变为“活动”吗

您可以在
btnEdit\u单击中设置一个标志,如果该标志为真,则仅处理其他函数中的功能

或者,可以在
btnEdit\u单击功能中添加事件,例如

private void btnEdit_Click(object sender, EventArgs e)
{
    btnEdit.Visible = false;
    btnPrint.Visible = false;
    btnSave.Visible = true;

    //Want to call mouse function here.

    textBox.MouseDown += new MouseEventHandler(textBox_MouseDown);

}

但是,请从代码中当前存在的位置删除该额外行。

不要从Visual Studio designer挂接事件,而应通过添加以下行手动挂接
btnEdit\u Click
处理程序方法中的事件:

textboxname.MouseMove += new MouseEventHandler(textBox_MouseMove);
然后,在单击“保存”按钮时(我假设您有某种方法
btnSave\u Click
),通过执行以下操作来取消对事件的锁定:

textboxname.MouseMove -= new MouseEventHandler(textBox_MouseMove);

您的
MouseDown
活动也是如此。

感谢您的帮助,除了@JurgenCamilleri answer外,它工作得非常好。