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

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

问题描述

我在一个表单中有一个面板和一个按钮以及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天全站免登陆