Validating Combo boxes in WinForms

K

Ken Loomis

Hello:

Using WinForms, certain users may change the value in a combobox,
others may not.

Some of these boxes open with a default value.

I need to check the user's rights before committing his or her
changes in the box and restore the previous value if he or she is not
allowed (or not let the value be changed in the first place).

I can get the permissions and check them OK, but I don't know how to
manage the combobox. I'm not sure which events I should use.
SelectedIndexChanged isn't much help because it fires off when the
form opens because the value is being set programatically.

Any help would be much appreciated. Thanks.

Ken
 
K

Ken Loomis

Using WinForms, certain users may change the value in a combobox,
others may not.

Some of these boxes open with a default value.

I need to check the user's rights before committing his or her
changes in the box and restore the previous value if he or she is not
allowed (or not let the value be changed in the first place).

I can get the permissions and check them OK, but I don't know how to
manage the combobox. I'm not sure which events I should use.
SelectedIndexChanged isn't much help because it fires off when the
form opens because the value is being set programatically.

OK. I found "Validating." It works well. Now the only thing I need
to be able to do is reset the original value and allow the user to
continue. Right now it's stuck on the error.

Can I get the value somewhere and resent it?? Then I suppose I change
the e.Cancel to false??

My code:

private void cmbStatus_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
bool blnOk = false;
foreach (DataRow permissionsDataRow in tblPermissions.Rows)
{
int theRole =
Convert.ToInt32(permissionsDataRow["RoleID"]);
if (theRole == (int)frmMain.Role.Manager )
{
blnOk = true;
}
}
if (blnOk == false)
{
e.Cancel = true;
this.errorProvider1.SetError(cmbStatus, "You do not
have permissions to change this field.");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top