0

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?

Calvin
  • 631
  • 5
  • 17
  • 38

2 Answers2

0

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
OCDan
  • 1,103
  • 1
  • 10
  • 19
0

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
NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • I've updated a little bit more info. Can you clearify which to put on the main usercontrol and what to put on the user control ctlWithDropDown. I'm at lost here. – Calvin Jul 04 '13 at 02:39
  • Your edit has confused me some. Please explain your issue and code structure a little better and I will edit. – NoAlias Jul 04 '13 at 11:14
  • it's more like a user control on a user control. The first user control holds a second user control which has a drop down list. The first user control inherits the parent which has an event call SetFlag(boolean). On the second user control, when the drop down list's event "SelectionChanged" is fired, I want to raise the event SetFlag(boolean). How would I do this? – Calvin Jul 05 '13 at 12:23
  • never mind, your suggestion worked. I never used Raising Events before so I was just a bit confuse. But it's all working now. thanks so much for the help! – Calvin Jul 05 '13 at 12:56