- open a modal or modeless dialog;
- share data between the main form and the secondary form.
Activity
Full source code available, check the
ListViewDialogSample
sample.
-
Create a copy of the “ListViewBasicSample” project and name it "ListViewDialogSample"
-
Create the following UI
-
Name the “Edit” button
btnEdit
and the “Delete” buttonbtnDelete
-
Modify the
DisplayParticipants
method in theMainForm
class in order to set theTag
property for theListViewItem
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); } }
-
Handle the
Click
event for thebtnDelete
button as followsif (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(); }
-
Add a new Form to the project and name it
EditForm
-
Create the following UI
-
Rename the controls as
tbLastName
,tbFirstName
anddtpBirthDate
-
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; }
-
Set the
DialogResult
for the “Cancel” button as “Cancel” -
Rename the “Ok” button as
btnOk
-
Set the
DialogResult
for the “Ok” button as “OK” -
Handle the
Click
event for thebtnOk
button as follows_participant.LastName = tbLastName.Text; _participant.FirstName = tbFirstName.Text; _participant.BirthDate = dtpBirthDate.Value;
-
Handle the
Click
event for the “Edit” button in theMainForm
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();
-
For the
EditForm
set theAcceptButton
property to the name of the "Ok" button, in order to allow the user to trigger theClick
event of the "Ok" button by pressing theEnter
key. Also set theCancelButton
property to the name of the "Cancel" button in order to allow the user to trigger theClick
event of the "Cancel" button by pressing theEscape
key. -
Make the
EditForm
appear in the center of the parent form when opened, by setting theStartPosition
property toCenterParent
. Also set the propertyShowInTaskbar
toFalse
in order not to display theEditForm
in the Windows taskbar.
-
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. -
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. -
Remove the
GroupBox
previously used for adding new participants from theMainForm
. Replace it instead with a button that will use theEditForm
in order to add new participants. -
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 theParticipant
class. Afterwards, try a more complicated implementation by defining aRace
class. TheParticipant
class will include in this case a property of the typeRace
. -
Replace the
ListView
control used in theMainForm
with aDataGridView
control. Make sure that the edit and delete functionalities work correctly.Hint: you can use the
Rows
property of theDataGridView
instead ofItems
property of theListView
.Full source code available, check the
DataGridViewDialogSample
sample.