Eduards
Eduards

Reputation: 68

VB.NET Reference the user form in which the sender control is located

I wonder how do I reference the user form in which the sender control is located? I have a programmatically created button control and user form in which it's located and when clicking on it I need to reference the user form to grab a value from a combobox in that form to use it as a variable and I can do it descending i.e. get the sub control of the sender but how do you get the control above the sender?

Sub DynamicForm_NewForm_SmartHUB_ProjectsActivated(ByVal sender As Object, ByVal e As EventArgs)

        strProjectTypeFolderName = '(form in which the sender is located).combobox_ProjectType.selecteditem

    End Sub

Upvotes: 0

Views: 277

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

You'd first need to cast the sender as Control at least. You can then access the Parent property, although that property is type Control and may not be a form, if the sender is in a Panel, GroupBox or some other container. You ought to call the FindForm method instead. It will return a Form reference and it will also get the containing form no matter how deeply the sender is nested.

If you have Option Strict On, which you probably don't but you definitely should, even a Form reference won't be sufficient. The Form class has no combobox_ProjectType field, so you'd need to cast it as it's actual type - Form1 or whatever - in order to access that field without using late binding.

Upvotes: 1

Related Questions