I've got an UltraComboBox
control on my form, which uses the following code.
Private Sub cmbType_ValueChanged(sender as Object, e as EventArgs) Handles cmbType.ValueChanged
If cmbType.Value <> "" Then
If cmbType.Value = "Custom Template" Then
Dim da As New OleDbDataAdapter("SELECT * FROM [System Settings]", con)
Dim ds As New DataSet
Da.Fill(ds)
If ds.Tables(0).Rows(0).Item("enableTemplate") = False Then
MessageBox.Show("Custom Templates have not been enabled at system level.", "Cannot Add Custom Template", MessageBoxButtons.OK, MessageBoxIcon.Information)
cmbType.Value = ""
End If
ElseIf cmbType.Value = "Default Template"
' ...
End If
End If
End Sub
The issue I'm getting is that after closing the MessageBox
, it sets the value to "", then iterates the code, then, for some reason, does it again but the value is back to "Custom Template", so the MessageBox
shows for a second time.
Is there something wrong with the code, or is there an easier way of doing this?
EDIT
The following has not affected it, so I'm assuming that I've not interpreted @Steve's suggestion correctly.
If ds.Tables(0).Rows(0).Item("enableTemplate") = False Then
MessageBox.Show("Custom Templates have not been enabled at system level.", "Cannot Add Custom Templates", MessageBoxButtons.OK, MessageBoxIcon.Information)
RemoveHandler cmbType.ValueChanged, AddressOf cmbType_ValueChanged
cmbType.Value = ""
AddHandler cmbType.ValueChanged, AddressOf cmbType_ValueChanged
End If
EDIT 2
As per @HansPasssant's suggestion:
If ds.Tables(0).Rows(0).Item("enableTemplate") = False Then
Me.BeginInvoke(Sub() MessageBox.Show("Custom Templates have not been enabled at system level.", "Cannot Add Custom Templates", MessageBoxButtons.OK, MessageBoxIcon.Information))
RemoveHandler cmbType.ValueChanged, AddressOf cmbType_ValueChanged
cmbType.Value = ""
AddHandler cmbType.ValueChanged, AddressOf cmbType_ValueChanged
End If
This still shows the MessageBox
twice.
If it is a bug in UltraCombo
, as appears to be the case, you have 2 options:
1) Use a regular ComboBox
to do the same thing - Although, it will look different so the end user may not be totally happy with this.
2) Alternatively, if you don't have lots of items in the DropDownList
, you could use RadioButtons
in a GroupBox
to do the same thing.
Then, if the Custom Template RadioButton.Checked = True
, run the same checks and output the MessageBox
.
Without upgrading the controls, there really isn't a lot you can do about this, so I guess these are your only two options.