C# 从form1 c中的类继承textBox\u MouseMove事件#

C# 从form1 c中的类继承textBox\u MouseMove事件#,c#,C#,我创建了一个类,在这个类中我定义了需要在表单中调用的方法。我需要在很多表单中从类测试中调用这个方法。下面是我的代码,我是如何尝试的,但没有成功。我不知道我错在哪里 //class Test.cs using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Linq; using

我创建了一个类,在这个类中我定义了需要在表单中调用的方法。我需要在很多表单中从类测试中调用这个方法。下面是我的代码,我是如何尝试的,但没有成功。我不知道我错在哪里

  //class Test.cs
 using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Printing;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Xml;
 using System.Xml.Linq;

   namespace Restaurant
  {
public class Test : Form
{

    public  void MouseMove(object sender, MouseEventArgs e)
    {
        TextBox txt = sender as TextBox;
        foreach (TextBox text in this.Controls.OfType<TextBox>())
        {
            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;
                }
            }
        }
    }

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



 // form in which i need to call methods from Test class
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Drawing.Printing;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Xml;
 using System.Xml.Linq;

  namespace Restaurant
  {
public partial class Form1 : Test
{

    public Form1()
    {
        InitializeComponent(); 
    }

    private void textBox_MouseMove(object sender, MouseEventArgs e)
    {
        Test b = new Test();
        b.MouseMove1(sender,e);

    }

    private void textBox_MouseDown(object sender, MouseEventArgs e)
    {
        Test b = new Test();
        b.MouseDown(sender,e);

    }
 }
}
//class Test.cs
使用制度;
使用System.Collections.Generic;
使用系统图;
使用系统、绘图、打印;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Linq;
命名空间餐厅
{
公开课考试:表格
{
public void MouseMove(对象发送器,MouseEventArgs e)
{
TextBox txt=发送者作为TextBox;
foreach(此.Controls.OfType()中的文本框文本)
{
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;
}
}
}
}
public void MouseDown(对象发送器,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
MouseDownLocation=e.位置;
}
}
}
}
//我需要从测试类调用方法的形式
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统、绘图、打印;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Linq;
命名空间餐厅
{
公共部分类表单1:测试
{
公共表格1()
{
初始化组件();
}
私有无效文本框\u MouseMove(对象发送方,MouseEventArgs e)
{
测试b=新测试();
b、 MouseMove1(发送者,e);
}
私有void文本框\u MouseDown(对象发送方,MouseEventArgs e)
{
测试b=新测试();
b、 MouseDown(发送方,e);
}
}
}

由于Form1继承自Test,您应该能够按如下方式更改Form1:

private void textBox_MouseMove(object sender, MouseEventArgs e)
{
    MouseMove1(sender,e);
}

这样,您就可以在所使用的表单上调用该方法,而不是在代码示例中实例化和操作的新表单。

以及如何调用另一个没有返回参数的方法?我将发布一个新问题,说明您正在尝试实现的目标。