Visual C sharp Windows Forms Tutorial

Lesson 1 : How to add a button and textbox

  1. Add a project
  2. Change the form caption: click the form head and go to properties list. Find text properties. Change this text.
  3. Change the size of form: click the form head and go to properties list. Find size properties. Change width and height properties.
  4. Add the button: View/ToolBox/Drag the button to form.
  5. Add the textbox: VIEW/ToolBox/Drag the textbox to form.
  6. Double click the button and enter in button1_click event: string tt = textBox1.Text; MessageBox.Show(tt, "Caption");
  7. Run application: Debag/Start Debug. Enter text in textbox. Click on the button. Now you must see text of textbox in dialog window.

Lesson 2: How to open Dialog Box

  1. Add new form: Project/Add new element/Form.
  2. Change the form 2 caption: click the form head and go to properties list. Find text properties. Change this text.
  3. Change the size of form 2: click the form head and change width and height properties.
  4. Go to Form1 constructor window.
  5. Double click on the button, delete operators from lesson 1 and enter in button1_click event: Form2 newForm = new Form2(); newForm.Show();
  6. Run application: Debag/Start Debug. Click on the button. Now you must see Form2 dialog window.

Lesson 3: ListBox

  1. Add the ListBox to Form: View/ToolBox/Drag the ListBox to form.
  2. Fill ListBox Values:Click the Listbox, find Items property in property list and click "...". Enter the values one on a line.
  3. Click the events of ListBox1. Double click SelectedValueChanged event and enter: if (listBox1.SelectedIndex != -1) { textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString(); }
  4. Run application: Debag/Start Debug. Click the item in ListBox. Now you must see it's value in editbox.

Lesson 4: ListBox - Add and Delete Values Dinamically

  • Fill ListBox Values:Click the Listbox, find Items property in property list and click "...". Delete all values.
  • Enter Values: Double click on Form1 Header, find InitializeComponent();, enter after it: string[] countries = { "Country1", "Country2", "Country3", "Country4", "Country5" }; listBox1.Items.AddRange(countries);
  • Run application: Debag/Start Debug. Click the item in ListBox. Now you must see it's value in editbox.
  • Deleting items: Add the button: View/ToolBox/Drag the button to form. Click the button, go to properties and change Text property to "Delete".
  • Code for Button: Double click the button and enter: if(listBox1.SelectedIndex != -1) listBox1.Items.RemoveAt(listBox1.SelectedIndex);
  • Run application: Debag/Start Debug. Click the item in ListBox. Click the 'Delete' button. Selected item is deleted.