tags:

views:

89

answers:

3

I'm creating a form with a few buttons and a combobox at runtime.

dim f as new form

(blah blah)

then the buttons acceptDescription and rejectDescription are set up...
then the combobox descriptionCombo is set up...

then...

AddHandler acceptDescription.Click, AddressOf handleAcceptedDescription
AddHandler rejectDescription.Click, AddressOf handleRejectedDescription

then I have these two methods to catch the click events... but can't figure out how to reference the other runtime generated controls. (combobox if accepted, form if rejected)

Private Sub handleAcceptedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
  'stub 
  'will need to reference the chosen combobox value here 
  dim acceptedDescription as string = descriptionCombo.selectedValue .tostring  
End Sub
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
  'I want to close the runtime created form here, but can't reference it
  f.close()
  'and return user to main form
  Me.Focus()
End Sub
A: 

Why can't you reference it? Just save it as a module/form-level variable and you're set.

Stu
+1  A: 

If the code for generating the form is in your main form, then declare the Form variable at the class level of the main form class so you can access it from the event handlers. Same goes for your combobox and text field- you need to make sure the variables are declared outside the scope of the handlers so you can reference them within the handlers.

Dave Swersky
That should work - but I was thinking that it must be possible without declaring more global variables.Thanks.
42
If the form is generated the same way every time, you might consider writing a new Form subclass with the combobox and textbox declared public.
Dave Swersky
A: 

In order to avoid global definitions, the best answer is

Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'sender is a button in this case.
    'get the button
    dim b as new button
    b = ctype(sender,button)
    'now get the button's parent form
    dim f as new form
    f = ctype(b.parent, form)
    'now close the form
    f.close()
End Sub
42