从C#中其他类的表单访问按钮

时间:2018-11-19 09:54:06

标签: c#

我想知道是否在C#中按下了另一个类中的按钮。我有我的标准班级表格和另一个班级设备。现在,我想从设备类的Form中的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");
    }
}

0 个答案:

没有答案
相关问题