开发者

Calling objects from dynamic UserControls VB.NET

开发者 https://www.devze.com 2023-04-10 12:19 出处:网络
Love these forums, I am a beginner when it comes to VB.NET and have run into some trouble. Here is my Code snippet

Love these forums, I am a beginner when it comes to VB.NET and have run into some trouble.

Here is my Code snippet

    'decleare variables
    Dim vmcount As Integer
    Dim tabcount As Integer
    Dim userControl As Control
    Dim UserControlName As String

    vmcount = combo_vmcount.SelectedItem

    tabcount = 1

    tab_con_vm.TabPages.Clear()


    While (tabcount <= vmcount)

        Dim tabname As New TabPage

        'Load variables
        userControl = New calc_usercontrol_vm

        tabname.Text = "VM" & tabcount

        tabname.Name = "VM" & tabcount

        UserControlName = "UCVM" & tabcount

        userControl.Name = UserControlName

        'actions
        tab_con_vm.TabPages.Add(tabname)


        tabname.Controls.Add开发者_如何学C(userControl)

         'next
        tabcount = tabcount + 1


    End While
End Sub

The trouble I'm having is working out a way to be able to call the objects in the dynamically created usercontrols. I thought a list maybe an option but I am struggling to get the syntax/get it working. Wondering if anyone has some ideas or different approaches..

Thanks Guys Richard


If you know the index of the tab you wish to work with

Dim calc_usercontrol As calc_usercontrol_vm = TabPages(index).userControl

Or if you don't know the index you can use the IndexOfKey method where the key is the Name of the tabcontrol

Dim index as Integer = TabPages.IndexOfKey("TabControlName")


While this is most likely not the best way to solve the problem, I ended up creating an global array and build the user controls off that.

' USERCONTROL ARRAY
Public UCVMARRAY(30) As calc_usercontrol_vm






Dim tabcount As Integer

    Dim UCVMARRAYindex As Integer

    Dim userControl As Control 'control variable

    Dim UserControlName As String


    vmcount = combo_vmcount.SelectedItem

    tabcount = 1

    UCVMARRAYindex = 0

    tab_con_vm.TabPages.Clear()


    While (tabcount <= vmcount)

        Dim tabname As New TabPage ' Relook at this to improve the method used. Issue was that new page was not generated on loop.

        'Load variables

        userControl = New calc_usercontrol_vm
        ' loads UC

        tabname.Text = "VM" & tabcount

        tabname.Name = "VM" & tabcount

        UserControlName = "UCVM" & tabcount

        userControl.Name = UserControlName

        UCVMARRAY(UCVMARRAYindex) = userControl 'places it back 

        'actions
        tab_con_vm.TabPages.Add(tabname)

        tabname.Controls.Add(userControl)


        'next
        tabcount = tabcount + 1
        UCVMARRAYindex = UCVMARRAYindex + 1


    End While
End Sub

It's very basic but I got it working, the answers above are most likely a good solution but I my knowledge of vb.net were not up to scratch.


Call the Page.LoadControl method and then add it to your page, preferrebly in the Init portion of the Page lifecycle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号