2

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?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Eduards
  • 68
  • 2
  • 20
  • Show us EXACTLY how you created the form object in the first place. I'd wager that you never actually set the `Name` property so it should be no surprise that you don't get anything from it later. As I said in your previous question, you obviously have `Option Strict Off` and that's bad. You should turn it `On` immediately and fix all the errors that will no doubt be flagged. That will help you write better code from the get-go. – jmcilhinney Jan 27 '23 at 09:16
  • Edited the OP ``iNewForm_SmartHUB_Projects`` is a ``long`` that I just + 1 each next time to make different names for each userform – Eduards Jan 27 '23 at 09:20
  • Does this answer your question? [VB.NET Reference the user form in which the sender control is located](https://stackoverflow.com/questions/75255575/vb-net-reference-the-user-form-in-which-the-sender-control-is-located) – ClearlyClueless Jan 27 '23 at 14:19
  • No, not at all ( – Eduards Jan 27 '23 at 14:53
  • 1
    You mean like `DirectCast(sender, Form_SmartHUB_Projects).combobox_projecttype` a.k.a `CType(sender, Form_SmartHUB_Projects).combobox_projecttype`?.. – GSerg Jan 29 '23 at 11:11
  • 1
    Does this answer your question? [Access properties of controls on a programatically created user control VB .NET](https://stackoverflow.com/q/8197035/11683) – GSerg Jan 29 '23 at 11:17
  • @gserj No not that but instead ``DirectCast(sender, Form_SmartHUB_Projects_2).combobox_projecttype`` where itš the second copy of ``Form_SmartHUB_Projects`` – Eduards Jan 30 '23 at 06:35
  • Also the other thread isn't really helping too because ``Dim NewForm_SmartHUB_Projects As New Form_SmartHUB_Projects = CType(sender, Form)`` is not a valid piece of code, because of that "New" – Eduards Jan 30 '23 at 06:39
  • The actual programmatically created form would be called ``NewForm_SmartHUB_ActiveProjects_`` followed by a number of the instance, for example ``NewForm_SmartHUB_ActiveProjects_1`` and there can be infinite number of such userform, and I need to grab the active one upon the ``.activate`` event – Eduards Jan 30 '23 at 06:50
  • 1
    Do you understand the difference between the name of the type and the `.Name` property of an instance of that type? All your infinitely many userforms have the type of `Form_SmartHUB_Projects`. Or are you actually [defining classes dynamically](https://stackoverflow.com/q/3862226/11683) in your assembly? – GSerg Jan 30 '23 at 09:32
  • I guess no, not really... All the code regarding the thing is in the OP – Eduards Jan 30 '23 at 09:44
  • referencing ``Form_SmartHUB_Projects`` doesn't work and I figure it's because it's not the form that's actually is opened because the opened one is `NewForm_SmartHUB_Projects` which is ``Dim`` and not ``Public`` so I can't access it from anywhere and it's ``Dim`` because I need to create it as new form each time – Eduards Jan 30 '23 at 09:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251490/discussion-between-eduards-and-gserg). – Eduards Jan 30 '23 at 09:47

1 Answers1

1
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
tuyau2poil
  • 767
  • 1
  • 2
  • 7