Member Avatar for Member #545322

Hi I need a help in choosing a combo box event for placing this code. I have tried with Lostfocus,leave,mousefocuschanged,displaymemberchanged like this but nothing works and Lable26(Default I set Visible:false ) is visible even after I select an Item.

If combotype.SelectedIndex = -1 Then
            Label26.Visible = True
            Label26.ForeColor = Color.Red
            Label26.Text = "(Select the Job Category )"
        Else
            Label26.Visible = False
        End If

Dani AI

Generated

SelectedIndexChanged fires only when the selected index actually changes. That explains why saw nothing when leaving the ComboBox: if the selection was already the same, SelectedIndexChanged will not run. and were correct that SelectedIndexChanged is the right event to respond immediately after a user picks a different item, but it does not cover the “left the control with no selection” case.

For validation-on-leave, the Validating event is the most appropriate choice. Validating runs when focus is about to leave the control, integrates with CausesValidation, and allows cancelling the leave via e.Cancel (so focus can be kept until a valid value is entered). LostFocus is a lower-level event and can fire when the whole application loses focus (as noted by ), so it is less suitable for input validation. Leave is fine for non-cancelable UI changes, but Validating is preferable for enforcing required input.

Example pattern (validate on leave, optionally block leaving until valid):

Private Sub ComboType_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboType.Validating
    If String.IsNullOrWhiteSpace(ComboType.Text) Or ComboType.SelectedIndex = -1 Then
        Label26.Visible = True
        e.Cancel = True   ' keep focus until a valid selection/text is entered; remove to allow leaving
    Else
        Label26.Visible = False
    End If
End Sub

Additional tips: set Label26.Visible = True initially (form load) or validate again on form submit/button click so errors aren't shown prematurely. If selection must be restricted, set ComboBox.DropDownStyle = ComboBoxStyle.DropDownList so typing cannot create a non-selected value. For data-bound combos, prefer checking SelectedValue/SelectedItem. Consider using an ErrorProvider for clearer, non-intrusive feedback instead of only a visible label.

Recommended Answers

All 5 Replies

The below code will trap the event you are after. However, once an item is selected, it can't be deselected without selecting another value. In other words, SelectedIndex will never again = -1. It would seem that you need to set Label26.visible = true during load and then let this function turn it off after the user selects a value.

Private Sub ComboType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboType.SelectedIndexChanged
        If ComboType.SelectedIndex = -1 Then
            Label26.Visible = True
            Label26.ForeColor = Color.Red
            Label26.Text = "(Select the Job Category )"
        Else
            Label26.Visible = False
        End If
    End Sub
commented: Good +2

write your code on combobox_SelectedIndexChanged event

Member Avatar for Member #545322

thanks smith now the code works fine but I placed the code in lostfocus event

Be aware that lost focus also fires when the app looses focus, not just when the control looses it. So, if the combobox has focus and they switch apps to read an incoming email, this event will fire.

Member Avatar for Member #545322

Be aware that lost focus also fires when the app looses focus, not just when the control looses it. So, if the combobox has focus and they switch apps to read an incoming email, this event will fire.

Yes its true, but when i place the code in Selectedindexchanged event means nothing happens when I leave the combo box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.