0

Program that has a FrmOptions.VB form with a ComboBox to select a language.

FrmOptions is opened by a OptionsToolStripMenuItem.Click, handled in the FrmMain form.

The code of the Combo in the FrmOptions form:

 Private Sub CmbLang_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbLang.SelectedIndexChanged

    If Not CmbLang.Focused Then 'to avoid unwanted calls
        Debug.Print("Return for ComboBoxLang NOT Focused at " & Now.ToString)
        Return
    End If

    'Some code to change the language

End Sub

The Output when I run the program without to access the OptionsToolStripMenuItem.Click and without to Show FrmOptions:

Return for ComboBoxLang NOT Focused at 21/02/2023 19:58:49

Why?? How is possible that the program fires the event of a Form that is currently Closed and never opened?

ezio
  • 394
  • 3
  • 12
  • 1
    hi, perhaps might be of interest https://stackoverflow.com/questions/13249642/combox-selectedindexchanged-fires-when-close-the-winform – jspcal Feb 21 '23 at 19:23
  • The selection is changed automatically as part of the normal lifecycle of the form/contols. The question should really be what exactly want to do in the SelectedIndexChanged event and is it actually the appropriate place to do so? – Hursey Feb 21 '23 at 21:11
  • @jspca: no, the link you have posted refers to a SelectedChanged event fired WHEN THE FORM CLOSES... but FrmOptions has been NEVER opened, so it can't close. – ezio Feb 21 '23 at 22:28
  • @Hursey: I have written in the code example as commented code: when, once Showed FrmOptions someone select a new language in the ComboBox... some code changes the language of all open forms. The routine works well and the CmbLang.Focused check code avoids wrong calls. The mine in just a curiosity: how can change the selection of a Combo control that is non active, positioned on a form that is not open????? – ezio Feb 21 '23 at 22:35
  • Well, when creating or destroying the form for example, the controls values will get set to some initial default value probably as part of the InitializeComponent method called from constructor methods (Myself, not put too much thought into it). That makes sense that the index is changing, and hence event firing. So, as per 1st comment, you need to ensure you're using the appropriate event or as per your solution is to put mechanisms in to handle the event being triggers outside your intent – Hursey Feb 21 '23 at 22:54
  • The `SelectedIndexChanged` event is raised whenever the `SelectedIndex` property changes. If you're seeing the event raised, obviously the property is changing. The form doesn't need to be open for that to happen. If you want to act only when the user makes a selection via the UI, handle the `SelectionChangeCommitted` event. – jmcilhinney Feb 22 '23 at 00:03
  • As to how it happens, maybe you are referring to the default instance of that form somewhere else and that is causing its creation and population. If you're binding data to that `ComboBox` during that creation, that will change the `SelectedIndex`. Put a breakpoint on the event handler and then you can tell exactly what's happening in the app when the event is raised. – jmcilhinney Feb 22 '23 at 00:07

0 Answers0