Visual C sharp Windows Forms Tutorial
Lesson 1 : How to add a button and textbox
- Add a project
- Change the form caption: click the form head and go to properties list. Find text properties. Change this text.
- Change the size of form: click the form head and go to properties list. Find size properties. Change width and height properties.
- Add the button: View/ToolBox/Drag the button to form.
- Add the textbox: VIEW/ToolBox/Drag the textbox to form.
- Double click the button and enter in button1_click event:
string tt = textBox1.Text; MessageBox.Show(tt, "Caption");
- 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
- Add new form: Project/Add new element/Form.
- Change the form 2 caption: click the form head and go to properties list. Find text properties. Change this text.
- Change the size of form 2: click the form head and change width and height properties.
- Go to Form1 constructor window.
- Double click on the button, delete operators from lesson 1 and enter in button1_click event: Form2 newForm = new Form2();
newForm.Show();
- Run application: Debag/Start Debug. Click on the button. Now you must see Form2 dialog window.
Lesson 3: ListBox
- Add the ListBox to Form: View/ToolBox/Drag the ListBox to form.
- Fill ListBox Values:Click the Listbox, find Items property in property list and click "...". Enter the values one on a line.
- Click the events of ListBox1. Double click SelectedValueChanged event and enter: if (listBox1.SelectedIndex != -1)
{
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
}
- 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.