Domanda

I'm using vb.net and winform. I am coming across an issue which I'm pounding my head against for the past few hours.

I have a main usercontrol which I added a groupbox and inside that groupbox, added a control like this:

main usercontrol

Me.GroupBox1.Controls.Add(Me.ctlWithDropDown)

user control ctlWithDropDown

Me.Controls.Add(Me.ddList)

Private Sub ddlList_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlList.SelectionChanged
'some simple logic here to check if value changed        

End Sub

The main usercontrol inherits the base class which has an event to set a value to true or false like so:

 Public Event SetFlag(ByVal value As Boolean)

I want to know how I can trigger/set this boolean value from the dropdownlist when the SelectionChanged event is trigger. Any help on this issue?

È stato utile?

Soluzione

Wire up an event handler for the drop down list:

AddHandler Me.ctlDropDown.SelectedIndexChanged, AddressOf ddlSelectedIndexChanged
Me.GroupBox1.Controls.Add(Me.ctlDropDown)

Make sure you create ddlSelectedIndexChanged in your control and have it fire the SetFlag Event:

Protected Sub ddlSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

   RaiseEvent SetFlag(True)

End Sub

Altri suggerimenti

I presume the me.ctlDropDown is something that you are making programmatically? If so then this sort of thing should work for you.

Public Sub Blah()
    Dim ctlDropDown As New ComboBox
    AddHandler ctlDropDown.SelectedIndexChanged, AddressOf IndexChangedHandler
    Me.GroupBox1.Controls.Add(ctlDropDown)
End Sub

Private Sub IndexChangedHandler()
    'Do whatever you need here.
End Sub

However, if this is not created at runtime should make an event handler like:

Private Sub IndexChangedHandler() Handles Me.ctlDropdown.SelectedIndexChanged
    'Do whatever you need here.
End Sub
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top