WinForms References
Here are some quick references for naming conventions and common controls in WinForms.
Prefixes
Control Prefix
Button btn
Label lbl
TextBox txt
ListBox lst
GroupBox grp
PictureBox img
RadioButton rad
CheckBox chk
Timer tmr
If I don't have a control listed here, make up your own appropriate prefix.
Lists and ListBoxes
Depending on your needs, you may use a ListBox to maintain a collection of data, or you may use a list to maintain the actual data behind the scenes and just keep the ListBox updated based on the underlying list (see setting the DataSource below).
You can use a ListBox to store a number of items.
If you add an item to a ListBox, it's ToString() method will be called and its string representation will be what is displayed in the ListBox.
Add an item:
The ListBox will display whatever ToString() returns for the item added to the list.
lstThings.Items.Add("Hello"); //Adds to the end of the list
lstThings.Items.Insert(2, "Hello"); //Inserts item at a specific index
You can add an entire list (eg. myList) to a ListBox as well:
lstThings.Items.AddRange(myList.ToArray());
This can also be done by setting the ListBoxes DataSource to your list, see below:
Get Items:
lstThings.SelectedIndex //Provides the index of the selected item in the list
lstThings.SelectedItem //Provides the value of the selected item in the list
Remove Items:
lstThings.Items.RemoveAt(0); //Removes whatever is at index 0
lstThings.Items.RemoveAt(lstThings.SelectedIndex); //Removes selected item by its index
lstThings.Items.Remove(lstThings.SelectedItem); //Removes selected item by its value
lstThings.Items.Remove("Hello"); //Removes "Hello" from ListBox
lstThings.Items.Clear(); //Removes everything from ListBox
You can also bind a list to a ListBox.
This may be better if you are maintaining Lists behind the scenes with your data.
List<string> names = new List<string>();
... //Add some names to the list
lstThings.DatSource = null; //Clears any previous DataSource
lstThings.DataSource = names; //Populates the ListBox with items from names.
//ToString() will be used to populate the ListBox
PictureBoxes
Changing the Image in a PictureBox
imgTrilobyte.Image = Properties.Resources.itemFromResources;
Adding Forms
Project->Add Form-> Give the form a name: FormAboutExample
To make a new Form:
FormAboutExample frmAboutExample = new FormAboutExample();
To Show The Form:
frmAboutExample.Show(); // User will be able to switch between forms
//Inside the new Form:
this.Close(); // Will allow the Form to close itself
or
frmAboutExample.ShowDialog(); // Program halts here until the Form is closed
//Inside the new Form:
this.Dispose(); // Will allow the Form to close itself.
To Hide a Form:
frmAboutExample.Hide();
MessageBoxes
The easiest way to pop up a simple OK MessageBox to convey a message is:
MessageBox.Show("MessageBox Text Goes Here");
For more like using different types of MessageBoxes and detecting which Button was clicked see here (or do an internet search yourself):
Open and Close FileDialog Boxes
Create a DialogBox either in the designer or using code:
OpenFileDialog FileDialogName = new OpenFileDialog(); //Same with SaveFileDialog
Use an If statement so that it only attempts file access if the user clicks OK:
if (FileDialogName.ShowDialog() == DialogResult.OK)
{
//Code to read/write file goes here, see FILEIO.
//You can get the name of the file selected by accessing the FileName property:
//FileDialogName.FileName
}
Properties
Here are some initial properties you may wish to set:
//Allows you to specify a starting directory
FileDialogName.InitialDirectory = "c:\\";
//Allows you to specify different filter settings that can be selected in the Dialog box.
FileDialogName.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
//Determins which filter that you provided is the default filter
FileDialogName.FilterIndex = 2; //All files filter would be default in this case
FileDialogName.FilterIndex = 1; //txt files filter would be default in this case
//Opens dialog box to whatever previous directory was
FileDialogName.RestoreDirectory = true;