vendredi 13 mars 2015

Prevent selecting new listbox item if current one has invalid property

My settings form has listbox with objects of custom class in it. The custom class has few properties including an IP address.


All the properties have their own textboxes in the form and their values get saved in the list in textbox' textchanged event. Such as:



private void textBoxIP_TextChanged(object sender, EventArgs e)
{
item item = (item)listBox1.SelectedItem;
if (item != null)
{
item.IP = textBoxIP.Text;
}
listBox1.DataSource = null;
listBox1.DataSource = lista;
listBox1.DisplayMember = "name";
}


Here is code for SelectedIndexChanged event:



private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
item item = (item) listBox1.SelectedItem;
if (item != null)
{
textBoxName.Text = item.name;
textBoxDesc.Text = item.desc;
textBoxIP.Text = item.IP.ToString();
textBoxPort.Text = item.port.ToString();
}
}


Now I need a way to prevent user from selecting a new index in listbox with some feedback if the IP address is invalid. I can't figure out how to do this. I do have a method for checking the validity of IP string that returns true or false.


Aucun commentaire:

Enregistrer un commentaire