Here are some quick references for naming conventions and common controls in WinForms.
Button btn
Label lbl
TextBox txt
ListBox lst
GroupBox grp
PictureBox img
RadioButton rad
CheckBox chk
Timer tmr
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).
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.
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:
lstThings.SelectedIndex //Provides the index of the selected item in the list
lstThings.SelectedItem //Provides the value of the selected item in the list
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
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
imgTrilobyte.Image = Properties.Resources.itemFromResources;
Project->Add Form-> Give the form a name: FormAboutExample
FormAboutExample frmAboutExample = new FormAboutExample();
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.
frmAboutExample.Hide();
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):
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
}
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;