如何在面板中访问动态加载的用户控件的公共子组件 [英] How to access public subs of dynamically loaded user control in a panel

查看:14
本文介绍了如何在面板中访问动态加载的用户控件的公共子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有一个面板和一个按钮以及 2 个用户控件,我在面板中动态加载了第一个用户控件,然后在 userControl1 中我有一个方法,当我单击表单中的按钮时我想访问它然后在面板中将显示的用户控件更改为userControl2,我该怎么做?

I have a panel and a button in a form and 2 user controls, I dynamically loaded the first user control in the panel then inside the userControl1 I have a method that I want to access when I clicked the button in the form and then change the displayed user control to userControl2 in the panel, how should I do that?

form1 代码:

Public Class form1

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Dim control1 = New UserControl1

           Panel1.Controls.Add(control1)
           control1.Location = New Point(0, 0)
           control1.Size = New Point(1351, 533)
    End Sub

End Class

UserControl1 代码:

UserControl1 Code:

Public Class UserControl1

    Public Sub doSomething()
           'Do something'
    End Sub

End Class

推荐答案

好的,我会尽可能多地从你的问题中提取并给你看一个例子,你可以有很多不同的方法来做到这一点,因此,可能不会 100% 回答你的问题,但会给你足够的东西来得到你想要的.

OK, I'll take as much as I can from your question and show you an example, there are many different ways you could do this, thus, may not answer your question 100% but will give you enough to get what you want.

我假设您只有一个 control1 和一个 control2.

I'm making an assumption that you only have one control1 and one control2.

我的示例将交替进行,并在每次单击主窗体按钮时访问活动(显示的)用户控件中的子例程.

My example will alternate, and access a sub routine in the active (shown) usercontrol on each click on the main form button.

我会放在一个模块中:

Public control1 As New UserControl1
Public control2 As New UserControl2

在 UserControl1 中放置:

In UserControl1 put:

Public Sub DoSomething()
    Me.BackColor = Color.Black
End Sub

在 UserControl2 中放置:

In UserControl2 put:

Public Sub DoSomething()
    Me.BackColor = Color.White
End Sub

在您的 FormLoad 事件中放置:

In your FormLoad event put:

    control1.Location = New Point(0, 0)
    control1.Size = New Point(1351, 533)
    Panel1.Controls.Add(control1)

在您的 Button1 点击事件中放置:

In your Button1 click event put:

    Select Case Panel1.Contains(control1)
        Case True
            'Remove UserControl1 - Add UserControl2
            Panel1.Controls.Remove(control1)
            control2.Location = New Point(0, 0)
            control2.Size = New Point(1351, 533)
            Panel1.Controls.Add(control2)
            control2.DoSomething()
        Case False
            'Remove UserControl2 - Add UserControl1
            Panel1.Controls.Remove(control2)
            control1.Location = New Point(0, 0)
            control1.Size = New Point(1351, 533)
            Panel1.Controls.Add(control1)
            control1.DoSomething()
    End Select

上面是检查面板中的哪个 UserControl 并交替使用它并调用DoSomething".这只是给你一个想法的例子.您想要的可能会有所不同,您的第二个 UserControl 中可能有一个按钮,如果有,请修改开关代码以适应.

The above is checking which UserControl is in the panel and alternating it and calling the 'DoSomething'. This is just an example to give you an idea. What you want may be different, you may have a button in your second UserControl and if so, amend the switch code to suit.

这篇关于如何在面板中访问动态加载的用户控件的公共子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆