Home > Back-end >  VB.NET reference control in programmatically created sender form
VB.NET reference control in programmatically created sender form

Time:02-04

I am trying to reference a a combobox_projecttype in the sender user form. It's a programmatically created form to which I add handle

'Declare variables
Dim NewForm_SmartHUB_Projects As New Form_SmartHUB_Projects

With NewForm_SmartHUB_Projects
    .Name = "Form_SmartHUB_" & sender.text & "_" & iNewForm_SmartHUB_Projects
    .Text = sender.text
    AddHandler NewForm_SmartHUB_Projects.Activated, AddressOf DynamicForm_NewForm_SmartHUB_ProjectsActivated
    iNewForm_SmartHUB_Projects  =1
End With

and then the sub looks like this

Sub DynamicForm_NewForm_SmartHUB_ProjectsActivated(ByVal sender As Object, ByVal e As EventArgs)
    Debug.Print(sender.Name)

    strProjectTypeFolderName = sender.combobox_projecttype.selecteditem
End Sub

Debug.Print does return the correct name of the sender form however it seems that it doesn't see combobox_projecttype in the sender object.

iNewForm_SmartHUB_Projects is a Long that I just 1 each next time to make different names for each userform.

What am I doing wrong and how do I reference control in programmatically created sender form?

CodePudding user response:

strProjectTypeFolderName = CType(DirectCast(sender, Form).Controls.Find("combobox_projecttype_Name", True)(0), ComboBox).SelectedItem

where 'combobox_projecttype_Name' is yout combobox Name.

You can't access Form controls directly from sender (or I don't know how).

detailed steps :

Dim Frm As Form = DirectCast(sender, Form)
Dim Ctrl As Control = Frm.Controls.Find("combobox_projecttype_Name", True)(0)
Dim CB As ComboBox = CType(Ctrl, ComboBox)
              
strProjectTypeFolderName = CB.SelectedItem
  • Related