What event can I use that will:

Retain value when leaving field: Lostfocus, leave, enter, etc, Value works with
the exception of Tab, also fires with every keystroke, lostfocus & leave will
not work with all ways of leaving field.

Help

Dani AI

Generated

Short answer: handle validation on focus loss (Validated/Validating) and read the control's Text — not its Value/SelectedRow — then explicitly commit the binding. That avoids ValueChanged firing on every keystroke and prevents the control from falling back to the first grid row when the typed text doesn't match an item.

Why: LostFocus/Leave can fire before the editor has committed its pending edit or before data-binding has pushed the typed text into the data source; ValueChanged fires on every key press. Validating/Validated run as the control is losing focus and let you (a) inspect the final typed text, (b) cancel if invalid, or (c) explicitly commit the edit to the data source.

Practical pattern (VB.NET):

  • Use Validated (or Validating if you need to cancel) to run your “after-leave” logic.
  • In that handler use the control's .Text to get exactly what the user typed.
  • If the combo is bound through a BindingSource call EndEdit() so the data source keeps the value.
  • If you must detect Tab/Enter immediately, handle KeyDown and call the form's Validate() before focus moves.

Example (standard WinForms ComboBox — adapt to your Infragistics combo editor by using its Text/Selected API and the same binding commit idea):

Private Sub combo_Validated(sender As Object, e As EventArgs) Handles combo.Validated
    Dim cb = TryCast(sender, ComboBox)
    Dim typed = If(cb?.Text, String.Empty).Trim()
    Dim idx = If(cb IsNot Nothing, cb.FindStringExact(typed), -1)

    If idx >= 0 Then
        cb.SelectedIndex = idx
    Else
        cb.SelectedIndex = -1    ' preserve typed text, don't auto-select first row
    End If

    Dim bs = TryCast(cb.DataSource, BindingSource)
    If bs IsNot Nothing Then bs.EndEdit()

    AfterComboLeave(typed) ' your post-leave routine
End Sub

Private Sub combo_KeyDown(sender As Object, e As KeyEventArgs) Handles combo.KeyDown
    If e.KeyCode = Keys.Tab OrElse e.KeyCode = Keys.Enter Then Me.Validate()
End Sub

Notes: this follows 's idea of using a single handler for multiple events and answers 's hint about binding/selection behavior — read Text, avoid relying on Value for user-typed, non-matching input, and commit edits explicitly for bound controls.

Recommended Answers

All 10 Replies

I think you need to explain the problem in more detail; I don't understand the question.

Yeah, If you could help me with a clearer picture... or even zip and attach the project so we can see what you are doing, that would be outstanding!

Control is an Infragistics combobox with grid.
I'm using VB.net

Objective: Run sub after leaving combobox.

Problem: Lostfocus & Enter - does not hold combobox value when leaving field
Leave - Works if value is in grid, otherwise no
ValueChanged - works the best, but fires every time, Tab key will pull
first row when value in combo is not contained in
grid, Enter key will work

Ok, I'm Moving this thread to VB.Net

You can set up one sub and then set it to handle all of the events such as on lostfocus on mouseleave, etc. Then you could assign the row value to a variable possibly...

Chester

What event can I use that will:

Retain value when leaving field: Lostfocus, leave, enter, etc, Value works with
the exception of Tab, also fires with every keystroke, lostfocus & leave will
not work with all ways of leaving field.

Help

Wait on are you using radio buttons and check boxes? If you are it should be rad or chk in a combo box.

Objective: Run sub after leaving combobox.

Problem: Lostfocus & Enter - does not hold combobox value when leaving field
Leave - Works if value is in grid, otherwise no
ValueChanged - works the best, but fires every time, Tab key will pull
first row when value in combo is not contained in
grid, Enter key will work

Ok so let me see if I get this? You have a combobox with data values in it, and when the user selects a value you want it retained, but it is not being retained?

From what you have stated, the value grabbed when the "tab" key is pressed is the first row? If that because the user has not selected the value in the combobox, and it is left blank? Cause if the user just enters the combobox and does not select a value, and tabs out of the combobox, this could be true that first row "by default" is grabbed, as it depends on how you setup your code/data binding.

But what you have stated isnt very clear. If we could see some of the code around this combobox setup it would certainly helps us find a solution to you.

And goaldrush:

What do you mean rad or chk in combobox? Is there something that you are aware of that we are not?

As well how does Radio buttons or check boxes change the original question consideration here.

Wait on are you using radio buttons and check boxes? If you are it should be rad or chk in a combo box.

What event can I use that will:

Retain value when leaving field: Lostfocus, leave, enter, etc, Value works with
the exception of Tab, also fires with every keystroke, lostfocus & leave will
not work with all ways of leaving field.

Help

how to write code for combobox

how to write code in combobox

>how to write the code in combobox
:D

well you have to go to the code editor's window and type combobox. (and you will see a lot of intellisense or memberlist listing out. Use it and see). This mite be the answer for your question.

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.