Avatar of ISC
ISC
 asked on

Trapping new item clicked combo box

I have a menu option on the main form  that when selected creates a new child midi child form (in code) and on the midi form is a datagrid and two combo boxes.

The user may select this menu option many times therefore there maybe many midi forms displayed each with there own datagrid and two combo boxes.

I need to create a event handler where if a combo box item is selected a function is called...

How do I do this..?
Visual Basic.NET

Avatar of undefined
Last Comment
Jorge Paulino

8/22/2022 - Mon
Jorge Paulino

You can use SelectedIndexChanged event
Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
        ' your code
    End Sub

Open in new window

ISC

ASKER
Hi,

I tried adding the event handler 'l_cmbFund_SelectedIndexChanged' but when I select a new item the event handler is not called...

Thanks Ian


Private Function PopulateGrid(ByVal a_enumRecType As BAA_ENUM_Record_Types) As Boolean
 
        Dim l_strCaption As String = ""
        Dim l_dgrid_baa_records As New System.Windows.Forms.DataGridView
        Dim l_cmbFunds = New System.Windows.Forms.ComboBox
        Dim l_cmbAccounts = New System.Windows.Forms.ComboBox
        Dim l_grpbBottomPanel As New System.Windows.Forms.GroupBox
        Dim l_lblFunds As New System.Windows.Forms.Label
        Dim l_lblAccounts As New System.Windows.Forms.Label
 
        Try
            CType(l_dgrid_baa_records, System.ComponentModel.ISupportInitialize).BeginInit()
            l_grpbBottomPanel.SuspendLayout()
            Me.SuspendLayout()
 
            ' Create a new instance of the child form.
            Dim ChildForm As New System.Windows.Forms.Form
            ' Make it a child of this MDI form before showing it.
            ChildForm.MdiParent = Me
            ChildForm.AutoScroll = True
            ChildForm.AutoSize = True
            ChildForm.WindowState = FormWindowState.Maximized
            ChildForm.BackColor = Color.LightYellow
            m_ChildFormNumber += 1
            ChildForm.Name = m_ChildFormNumber.ToString
 
            '-------------------------------------------------------------------------------
 
            l_dgrid_baa_records.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
            l_dgrid_baa_records.Location = New System.Drawing.Point(2, 3)
            l_dgrid_baa_records.ScrollBars = ScrollBars.Both
            l_dgrid_baa_records.Name = "grid_records"
            l_dgrid_baa_records.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken
            l_dgrid_baa_records.AutoResizeColumns()
            l_dgrid_baa_records.Size = New System.Drawing.Size(639, 333)
            l_dgrid_baa_records.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            l_dgrid_baa_records.TabIndex = 0
 
            ' Alternate row colours from light gray to light yellow.
            l_dgrid_baa_records.RowsDefaultCellStyle.BackColor = Color.LightGray
            l_dgrid_baa_records.AlternatingRowsDefaultCellStyle.BackColor = Color.LightYellow
 
            '-------------------------------------------------------------------------------
 
            ' Create group box going across the bottom.
            l_grpbBottomPanel.Controls.Add(l_lblFunds)
            l_grpbBottomPanel.Controls.Add(l_lblAccounts)
            l_grpbBottomPanel.Controls.Add(l_cmbFunds)
            l_grpbBottomPanel.Controls.Add(l_cmbAccounts)
            l_grpbBottomPanel.Location = New System.Drawing.Point(2, 341)
            l_grpbBottomPanel.Name = "bottom_panel"
            l_grpbBottomPanel.Size = New System.Drawing.Size(638, 83)
            l_grpbBottomPanel.TabIndex = 1
            l_grpbBottomPanel.BackColor = Color.LightGray
 
            '-------------------------------------------------------------------------------
 
            ' Create group box going across the bottom.
            l_lblFunds.Text = "Funds:"
            l_lblFunds.Location = New System.Drawing.Point(20, 15)
            l_lblFunds.Size = New System.Drawing.Size(60, 20)
 
            l_lblAccounts.Text = "Accounts:"
            l_lblAccounts.Location = New System.Drawing.Point(305, 15)
            l_lblAccounts.Size = New System.Drawing.Size(60, 20)
 
            '-------------------------------------------------------------------------------
 
            ' Funds drop down list box.
            l_cmbFunds.Name = "cmb_funds"
            l_cmbFunds.FormattingEnabled = True
            l_cmbFunds.Location = New System.Drawing.Point(20, 35)
            l_cmbFunds.Size = New System.Drawing.Size(244, 25)
            l_cmbFunds.TabIndex = 2
 
            ' Accounts drop down list box.
            l_cmbAccounts.Name = "cmb_accounts"
            l_cmbAccounts.FormattingEnabled = True
            l_cmbAccounts.Location = New System.Drawing.Point(305, 35)
            l_cmbAccounts.Size = New System.Drawing.Size(312, 25)
            l_cmbAccounts.TabIndex = 3
 
            '-------------------------------------------------------------------------------
            ChildForm.Text = l_strCaption & "  " & m_ChildFormNumber
 
            ' Populate the grid with data.
            GetRecords(l_dgrid_baa_records, a_enumRecType)
 
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(646, 432)
 
            'Add the grid to the midi form.
            ChildForm.Controls.Add(l_grpbBottomPanel)
            ChildForm.Controls.Add(l_dgrid_baa_records)
 
            CType(l_dgrid_baa_records, System.ComponentModel.ISupportInitialize).EndInit()
            l_grpbBottomPanel.ResumeLayout(False)
            Me.ResumeLayout(False)
 
            ChildForm.Show()
 
            PopulateGrid = True
 
        Catch ex As Exception
 
            m_objAppLog.WriteErrorLineToAppLogFile(ex)
 
        Finally
 
        End Try
 
    End Function
 
 Friend WithEvents l_cmbFund As System.Windows.Forms.ComboBox
    Friend WithEvents l_cmbAccounts As System.Windows.Forms.ComboBox
    Friend WithEvents l_dgrid_baa_records As System.Windows.Forms.DataGridView
    Friend WithEvents l_grpbBottomPanel As System.Windows.Forms.GroupBox
 
    Private Sub l_cmbFund_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles l_cmbFund.SelectedIndexChanged
        MessageBox.Show("Hi")
    End Sub

Open in new window

ISC

ASKER
Hi,

I think what I need to do is declare a procedure level variable WithEvents and then attach an event handler..?

Do you know if this can be done and how to do it...?

Thanks ian
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Jorge Paulino

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.