C# 从C中其他类中的窗体访问按钮#

C# 从C中其他类中的窗体访问按钮#,c#,C#,我想知道另一个类的C#中是否按下了按钮。我有我的标准班级表格和另一个班级设备。现在我想在我的类设备中以表单的形式从InitializeComponent访问按钮。有人知道这样做的好方法吗 编辑:如果我按下btnInitialise,我想显示一个消息框(文本为“test”)以开始。我想在类设备中使用此按钮。我不知道如何将表单中自动生成的按钮btnInitialise引用到类设备 public class Form1 : System.Windows.Forms.Form { #regio

我想知道另一个类的C#中是否按下了按钮。我有我的标准班级表格和另一个班级设备。现在我想在我的类设备中以表单的形式从InitializeComponent访问按钮。有人知道这样做的好方法吗

编辑:如果我按下btnInitialise,我想显示一个消息框(文本为“test”)以开始。我想在类设备中使用此按钮。我不知道如何将表单中自动生成的按钮btnInitialise引用到类设备

public class Form1 : System.Windows.Forms.Form
{

    #region "Windows Form Designer generated code"

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>

    private void InitializeComponent()
    {
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tabPage1 = new System.Windows.Forms.TabPage();
        this.btnInitialise = new System.Windows.Forms.Button();
        this.cmbdevice = new System.Windows.Forms.ComboBox();
        this.tabControl1.SuspendLayout();
        this.tabPage1.SuspendLayout();
        this.SuspendLayout();
        // 
        // tabControl1
        // 
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Controls.Add(this.tabPage2);
        this.tabControl1.Location = new System.Drawing.Point(42, 41);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(645, 414);
        this.tabControl1.TabIndex = 0;
        // 
        // tabPage1
        // 
        this.tabPage1.Controls.Add(this.grpDevice);
        this.tabPage1.Location = new System.Drawing.Point(4, 22);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(637, 388);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "tabPage1";
        this.tabPage1.UseVisualStyleBackColor = true;
        //
        // btnInitialise
        // 
        this.btnInitialise.Location = new System.Drawing.Point(351, 16);
        this.btnInitialise.Name = "btnInitialise";
        this.btnInitialise.Size = new System.Drawing.Size(96, 25);
        this.btnInitialise.TabIndex = 21;
        this.btnInitialise.Text = "Initialize";
        this.btnInitialise.Click += new System.EventHandler(this.btnInitialise_Click);
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(1005, 532);
        this.Controls.Add(this.tabControl1);
        this.Name = "Form1";
        this.tabControl1.ResumeLayout(false);
        this.tabPage1.ResumeLayout(false);
        this.grpDevice.ResumeLayout(false);
        this.grpDevice.PerformLayout();
        this.ResumeLayout(false);

    }

    private TabControl tabControl1;
    private TabPage tabPage1;
    private Button btnInitialise;

    #endregion "Windows Form Designer generated code"

    #region "Global variables"
    // OpenLayers fields
    ////Encapsulates a DT-open layers deviceand manages and distributes subsystems for the device
    private Device device = null;



    #endregion "Global variables"


    //Automatically to initialize components of form
    public Form1()
    {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();
        //Set the culture to en-US for decimal point instead of decimal comma in results
        CultureInfo english = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentCulture = english;
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (device != null)
            {
                device.Dispose();
            }
        }
        base.Dispose(disposing);
    }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    ///
    //Run application, show error message if appl doesnt run
    [STAThread]
    private static void Main()
    {
        try
        {
            Application.Run(new Form1());
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }
    }
}


public class Device
{
    //When clicking on the initialize button show messagebox 
    private void btnInitialise_Click(object sender, EventArgs e)
    {

        MessageBox.Show("test");
    }
}
公共类表单1:System.Windows.Forms.Form
{
#区域“Windows窗体设计器生成的代码”
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.tabControl1=new System.Windows.Forms.TabControl();
this.tabPage1=new System.Windows.Forms.TabPage();
this.btnInitialise=new System.Windows.Forms.Button();
this.cmbdevice=new System.Windows.Forms.ComboBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
这个.SuspendLayout();
// 
//tabControl1
// 
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location=新系统.图纸.点(42,41);
this.tabControl1.Name=“tabControl1”;
this.tabControl1.SelectedIndex=0;
this.tabControl1.Size=新系统.Drawing.Size(645414);
this.tabControl1.TabIndex=0;
// 
//表1
// 
this.tabPage1.Controls.Add(this.grpDevice);
this.tabPage1.Location=新系统.图纸.点(4,22);
this.tabPage1.Name=“tabPage1”;
this.tabPage1.Padding=new System.Windows.Forms.Padding(3);
this.tabPage1.Size=新系统图尺寸(637388);
this.tabPage1.TabIndex=0;
this.tabPage1.Text=“tabPage1”;
this.tabPage1.UseVisualStyleBackColor=true;
//
//btnInitialise
// 
this.btnInitialise.Location=新系统图纸点(351,16);
this.btnInitialise.Name=“btnInitialise”;
this.btnInitialise.Size=新系统图纸尺寸(96,25);
this.btnInitialise.TabIndex=21;
this.btnInitialise.Text=“初始化”;
this.btnInitialise.Click+=new System.EventHandler(this.btnInitialise\u Click);
// 
//表格1
// 
this.ClientSize=新系统图尺寸(1005532);
this.Controls.Add(this.tabControl1);
this.Name=“Form1”;
this.tabControl1.ResumeLayout(false);
此.tabPage1.ResumeLayout(false);
this.grpDevice.ResumeLayout(false);
此.grpDevice.PerformLayout();
此选项为.resume布局(false);
}
私有TabControl tabControl1;
私有选项卡页选项卡页1;
私人按钮btnInitialise;
#endregion“Windows窗体设计器生成的代码”
#区域“全球变量”
//OpenLayers字段
////封装DT开放层设备,并管理和分发设备的子系统
专用设备=空;
#端域“全局变量”
//自动初始化窗体的组件
公共表格1()
{
//
//需要Windows窗体设计器支持
//
初始化组件();
//在结果中将区域性设置为en US表示小数点,而不是小数点逗号
CultureInfo英语=新的CultureInfo(“美国”);
CultureInfo.DefaultThreadCurrentCulture=英语;
}
/// 
///清理所有正在使用的资源。
/// 
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
如果(设备!=null)
{
device.Dispose();
}
}
基地。处置(处置);
}
/// 
///应用程序的主要入口点。
/// 
///
//运行应用程序,如果应用程序未运行,则显示错误消息
[状态线程]
私有静态void Main()
{
尝试
{
Application.Run(新Form1());
}
捕获(例外情况除外)
{
Show(例如Message.ToString(),“Error”);
}
}
}
公共类设备
{
//单击“初始化”按钮时,显示消息框
私有无效btnInitialise_单击(对象发送者,事件参数e)
{
MessageBox.Show(“测试”);
}
}

}

您必须向其他类公开该按钮。一种方法是使用公共财产。e、 g

 public Button MyButton
 {
   get { return button1; }
 }
然后从Form类中可以将其用作

Device d = new Device();
Button b = d.MyButton; 

注意:这个例子是基于你在文章中提出的问题。然而,现在还不清楚通过这样一个按钮,你到底能实现什么目的!如果您添加一个代码示例和有关您正在开发的应用程序的更多信息,我们可以更好地帮助您

您的
表单
构造函数应该如下所示:

public Form1()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    //Set the culture to en-US for decimal point instead of decimal comma in results
    CultureInfo english = new CultureInfo("en-US");
    CultureInfo.DefaultThreadCurrentCulture = english;
    device = new Device();
    this.btnInitialise.Click += new System.EventHandler(device.btnInitialise_Click);
}
public class Device
{
    //When clicking on the initialize button show messagebox 
    public void btnInitialise_Click(object sender, EventArgs e)
    {

        MessageBox.Show("test");
    }
}
现在在
设备中
您应该将eventhandler
公开
,如下所示:

public Form1()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    //Set the culture to en-US for decimal point instead of decimal comma in results
    CultureInfo english = new CultureInfo("en-US");
    CultureInfo.DefaultThreadCurrentCulture = english;
    device = new Device();
    this.btnInitialise.Click += new System.EventHandler(device.btnInitialise_Click);
}
public class Device
{
    //When clicking on the initialize button show messagebox 
    public void btnInitialise_Click(object sender, EventArgs e)
    {

        MessageBox.Show("test");
    }
}
现在,当您单击按钮时,
设备的btnInitialise\u Click
方法将被触发


编辑:修复了
设备
中的键入错误,请重试。

显示您已尝试的内容。如果您在另一个类(例如设备)的方法中引用了对象(例如表单类)的实例,则您可以访问该对象的公共方法、属性和字段。按钮是表单的属性,但通常是私有属性。你真正想做什么?你试过什么?你为什么要这样做?请提供你的代码给我们。我已经编辑了代码,他需要相反的-从
设备
点击
表单
中的按钮。如果我这样做,我会在表单1中得到2个错误:第一个错误:错误CS7036,没有参数,因为