简体   繁体   中英

C# removing item from listbox while updating textboxes

I'm writing a C# app, which is supposed to keep fields: first name, surname, date of birth and sex. If I select a person in the listbox, it coppies the values from the listbox into corresponding textboxes. I should then be able to: add a new record into listbox, modify the record in listbox, or to delete the actually selected record. My problem is, that in both function the app crashes with index set to "-1".

My code:

    class Person
    {
        public string Name{ get; set; }
        public string Surname{ get; set; }
        public DateTime DateOfBirth { get; set; }
        public bool Sex{ get; set; }

        public override string ToString()
        {
            return Name
            + " " + Surname
            + ", nar." + DateOfBirth.ToShortDateString()
            + " (" + (Sex? "male" : "female")
            + ")";
        }
    }

    private void personListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        Persond = (Person) personListBox.Items[personListBox.SelectedIndex];
        NameTextBox.Text = d.Name;
        SurnameTextBox.Text = d.Surname;
        DateOfBirthPicker.Value = d.DateOfBirth;
        FemaleRadioButton.Checked = d.Sex;
    }

    private void modifyRecordButton_Click(object sender, EventArgs e)
    {

            Person d = (Person)personListBox.Items[personListBox.SelectedIndex];
            d.Name= NameTextBox.Text;
            d.Surname= SurnameTextBox.Text;
            d.DateOfBirth= DateOfBirthPicker.Value;
            d.Sex= FemaleRadioButton.Checked;
            detiListBox.Refresh();        
    }

    private void DeleteButton_Click(object sender, EventArgs e)
    {
        personListBox.Items.Remove(detiListBox.SelectedItem);
        personListBox.Refresh();
        Person d = (Person)personListBox.Items[personListBox.SelectedIndex];
        d.Name = NameTextBox.Text;
        d.Surname= PrijmeniTextBox.Text;
        d.DateOfBirth= DateOfBirthPicker.Value;
        d.Sex= FemaleRadioButton.Checked;

    }
}

Defensive programming requires that you check the value of SelectedIndex before using it. Don't be surprised but you get a SelectedIndexChanged event when the Listbox has not item selected thus SelectedIndex is -1

private void personListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if(personListBox.SelectedIndex != -1)
    {
        Persond = (Person) personListBox.Items[personListBox.SelectedIndex];
        NameTextBox.Text = d.Name;
        SurnameTextBox.Text = d.Surname;
        DateOfBirthPicker.Value = d.DateOfBirth;
        FemaleRadioButton.Checked = d.Sex;
    }
}

The same check should be applied to the modifyRecordButton_Click and deleteRecordButton_Click handlers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM