向 WinForm 添加重载构造函数 [英] Adding An Overloaded Constructor To A WinForm

查看:26
本文介绍了向 WinForm 添加重载构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个作为 winform 的对话框,并像这样调用该 winform:

I have created a dialog as a winform and am calling that winform like this:

 Dim dlgEditChangeOrder As New dgEditChangeOrder
        Dim dlgResult As DialogResult

        dlgResult = dlgEditChangeOrder.ShowDialog

...很简单.我希望能够在调用 win 窗体时在 win 窗体上设置控件的可见性.我想作为一个构造函数来做这个,所以我可以写以下....

...pretty simple. I want to be able to set the visiblility of a control on the win form when the win form is called. I would like to do this as a constructor so I could write the following....

  Dim dlgEditChangeOrder As New dgEditChangeOrder(visibleIsTrue)

有人可以给我构造器代码来实现这一点吗?我担心的原因是我不知道在 winform 中这样做是否合法,因为 winform 是通过预先创建的 IntializeComponent() 函数加载的

Can someone give me the contructor code to make this happen? The reason I am concerned is I dunno if its legal to do this inside a winform since the winform is loaded bby the precreated IntializeComponent() function

推荐答案

重载构造函数是完全可以接受的.只需确保在重载的构造函数中调用 InitializeComponent(),因为它是设置表单所必需的.

It is perfectly acceptable to overload constructors. Just make sure to call InitializeComponent() in your overloaded constructor, as it's required to setup the form.

然而,只要你这样做,你就可以自由地重载构造函数(或者只是改变那里的那个).

However, as long as you do this, you are free to overload the constructor (or just change the one that's there).

只需在后面的代码中添加:

Just add this in your code behind:

Public Sub New(ByVal isVisible As Boolean)
    ' This call is required by the designer.
    InitializeComponent()

    ' Do what you want with isVisible here

End Sub

如果你也想有一个默认构造函数,你可以添加它(但这不是必需的).

If you want to have a default constructor, too, you can add that (it's not required, however).

这篇关于向 WinForm 添加重载构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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