Skip to content

Latest commit

 

History

History
149 lines (110 loc) · 5.71 KB

08 - WinForms - Dialogs.md

File metadata and controls

149 lines (110 loc) · 5.71 KB

Windows Forms – Dialogs

1. Objectives

  • open a modal or modeless dialog;
  • share data between the main form and the secondary form.

2. MessageBox and DialogResult

Activity

:octocat: Full source code available, check the ListViewDialogSample sample.

  1. Create a copy of the “ListViewBasicSample” project and name it "ListViewDialogSample"

  2. Create the following UI

UI Dialog

  1. Name the “Edit” button btnEdit and the “Delete” button btnDelete

  2. Modify the DisplayParticipants method in the MainForm class in order to set the Tag property for the ListViewItem instances, as shown below.

    private void DisplayParticipants()
    {
    	lvParticipants.Items.Clear();
    
    	foreach (Participant participant in _participants)
    	{
    		var listViewItem = new ListViewItem(participant.LastName);
    		listViewItem.SubItems.Add(participant.FirstName);
    		listViewItem.SubItems.Add(participant.BirthDate.ToShortDateString());
    		
    		//add this line
    		listViewItem.Tag = participant;
    		
    		lvParticipants.Items.Add(listViewItem);
    	}
    }
  3. Handle the Click event for the btnDelete button as follows

    if (lvParticipants.SelectedItems.Count == 0)
    {
    	MessageBox.Show("Choose a participant");
    	return;
    }
    
    if (MessageBox.Show("Are you sure?", "Delete participant", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    {
    	_participants.Remove((Participant) lvParticipants.SelectedItems[0].Tag);
    	DisplayParticipants();
    }

3. Secondary Dialog

  1. Add a new Form to the project and name it EditForm

  2. Create the following UI

    UI Dialog Edit

  3. Rename the controls as tbLastName, tbFirstName and dtpBirthDate

  4. Change the EditForm class, so that it is defined as follow

    #region Attributes
    private readonly Participant _participant;
    #endregion
    
    
    public EditForm(Participant participant)
    {
    	_participant = participant;
    	InitializeComponent();
    }
    private void EditForm_Load(object sender, System.EventArgs e)
    {
    	tbLastName.Text = _participant.LastName;
    	tbFirstName.Text = _participant.FirstName;
    	dtpBirthDate.Value = _participant.BirthDate;
    }
  5. Set the DialogResult for the “Cancel” button as “Cancel”

    Dialog Cancel

  6. Rename the “Ok” button as btnOk

  7. Set the DialogResult for the “Ok” button as “OK”

    Dialog OK

  8. Handle the Click event for the btnOk button as follows

    _participant.LastName = tbLastName.Text;
    _participant.FirstName = tbFirstName.Text;
    _participant.BirthDate = dtpBirthDate.Value;
  9. Handle the Click event for the “Edit” button in the MainForm as follows:

    if (lvParticipants.SelectedItems.Count == 0)
    {
    	MessageBox.Show("Choose a participant");
    	return;
    }
    
    EditForm editForm = new EditForm((Participant)lvParticipants.SelectedItems[0].Tag);
    if (editForm.ShowDialog() == DialogResult.OK)
    	DisplayParticipants();
  10. For the EditForm set the AcceptButton property to the name of the "Ok" button, in order to allow the user to trigger the Click event of the "Ok" button by pressing the Enter key. Also set the CancelButton property to the name of the "Cancel" button in order to allow the user to trigger the Click event of the "Cancel" button by pressing the Escape key.

  11. Make the EditForm appear in the center of the parent form when opened, by setting the StartPosition property to CenterParent. Also set the property ShowInTaskbar to False in order not to display the EditForm in the Windows taskbar.

4. Assignments (for you to try)

  1. Besides the “Edit” button, also allow the user to edit the participants by double clicking on them in the ListView.

    Hint: when handling the event, you can call the method that is already used for handling the Click event for the "Edit" button.

  2. Display a contextual menu when the user right clicks on a participant in the ListView. The contextual menu will include the options to edit or delete the participant.

  3. Remove the GroupBox previously used for adding new participants from the MainForm. Replace it instead with a button that will use the EditForm in order to add new participants.

  4. Allow the user to choose the race in which the user is going to participate as shown below. Try a simple implementation in which the race is stored as a string in the Participant class. Afterwards, try a more complicated implementation by defining a Race class. The Participant class will include in this case a property of the type Race. race

  5. Replace the ListView control used in the MainForm with a DataGridView control. Make sure that the edit and delete functionalities work correctly.

    Hint: you can use the Rows property of the DataGridView instead of Items property of the ListView.

    :octocat: Full source code available, check the DataGridViewDialogSample sample.

5. Bibliography