using System.Windows.Forms; public class MyForm : Form { private Button myButton; public MyForm() { myButton = new Button(); myButton.Text = "Click me!"; myButton.Click += MyButtonClick; this.Controls.Add(myButton); } private void MyButtonClick(object sender, EventArgs e) { MessageBox.Show("Hello, world!"); } }
using System.Windows.Forms; public class MyForm : Form { private Button myButton; private TextBox myTextBox; public MyForm() { myButton = new Button(); myButton.Text = "Toggle text box"; myButton.Click += MyButtonClick; this.Controls.Add(myButton); myTextBox = new TextBox(); myTextBox.Visible = false; this.Controls.Add(myTextBox); } private void MyButtonClick(object sender, EventArgs e) { myTextBox.Visible = !myTextBox.Visible; } }This example creates a new form with a button and a text box control. When the button is clicked, the text box is shown or hidden, depending on its current visibility. Package library: Windows Forms