title: Access a button from the form in other class in C#
body: <p>I want to know if a button is pressed in C# from another class. I have my standard class Form and another class Device. Now I want to access the button from InitializeComponent in Form in my class Device. Does anyone know a good way to do this?</p>
  
  <p>EDIT: If I press on the btnInitialise I want to show a messagebox (with text "test") to start with. I want to use this button in the class Device. I don't rely know how I can reference the button btnInitialise that is automatically made in my form to the class Device. </p>
  
  <pre><code>public class Form1 : System.Windows.Forms.Form
  {
  
      #region "Windows Form Designer generated code"
  
      /// &lt;summary&gt;
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
  
      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;
      }
      /// &lt;summary&gt;
      /// Clean up any resources being used.
      /// &lt;/summary&gt;
      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              if (device != null)
              {
                  device.Dispose();
              }
          }
          base.Dispose(disposing);
      }
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      ///
      //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");
      }
  }
  </code></pre>
  
  <p>}</p>