If there is content in the TextBox, it will be copied to ListBox when the "Add" button is clicked.
Also, the content in the TextBox will be automatically deleted when it is added to the listBox.
This is done in the event handler for the
Click
event of the Add button:
private void buttonAdd_Click(object sender, EventArgs e)
{
theList.Items.Add(theTextBox.Text);
theTextBox.Text = string.Empty;
}
The "Delete" button will delete the selected line from the listBox, and the "Exit" button will close the application.
This is done by implementing corresponding event handlers as well:
private void buttonDelete_Click(object sender, EventArgs e)
{
theList.Items.RemoveAt(theList.SelectedIndex);
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
I would also add some event handlers to enable or disable buttons based on the content of the textbox and the selected item in the list:
private void theTextBox_Validated(object sender, EventArgs e)
{
buttonAdd.Enabled = !string.IsNullOrEmpty(theTextBox.Text);
}
private void theList_SelectedIndexChanged(object sender, EventArgs e)
{
buttonDelete.Enabled = theList.SelectedIndex != -1;
}