vb.net 另一种形式的VB.NET Call Sub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25928442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:14:40  来源:igfitidea点击:

VB.NET Call Sub of another form

vb.netformssubform

提问by Oak_3260548

I know, that there are many questions related to this, but still I cannot find a workable solution.

我知道,有很多与此相关的问题,但我仍然找不到可行的解决方案。

Usually, it would work like this: A form creates an instance of another form in it's container like this:

通常,它会这样工作:一个表单在它的容器中创建另一个表单的实例,如下所示:

Dim PolInstIn As New SubForm1

Private Sub LoadDetail()

    PolInstIn.TopLevel = False
    PolInstIn.Name = "Sub From" 
    PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    PolInstIn.Dock = DockStyle.Fill

    Me.GroupBox6.Controls.Add(PolInstIn)
    PolInstIn.Show()

End Sub

Then it's easy to call a Public Sub from the sub form like this:

然后很容易从子表单调用公共子,如下所示:

    Call PolInstIn.MyPublicSubInSubForm1()

However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidentalinstance.

但是,在这种情况下,这对我不起作用。当我运行 MyPublicSubInSubForm1() 时,它不会抛出任何错误,但不会执行任何操作。如果我向 SubForm1 文本框写入一个值并读回它,它会读取,但我没有在屏幕上看到它,所以我怀疑它被写入了其他一些意外实例。

I suspect it is because my parent form is also an instanceof an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).

我怀疑这是因为我的父表单也是以与 SubForm1 非常相似的方式创建的表单的一个实例。基本上,ParentForm 是一个加载到 tabPage 中的表单,而 SubForm1 是一个加载到 ParentForm 中的模块。它可以存在于多个副本(选项卡)中。

Could you point to any simple solutions?

你能指出任何简单的解决方案吗?

Regards,

问候,

Libor

伦敦银行同业拆借利率

采纳答案by Oak_3260548

I see this question got a lot of views, so here is an answer.

我看到这个问题有很多观点,所以这里是一个答案。

1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).

1) 没有子窗体的视觉响应(只有结果)- 如果我创建了 1 个以上的窗体实例,这可能会发生。该示例只是一个示例,但如果以这种方式(意外地)使用它,则可能会导致每次都对子表单进行新定义(以及随后出现的症状,如所描述的症状)。在实践中,我将表单加载与将数据加载到表单中分开(由该表单中的公共子完成)。

2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set

2)如果您还需要反向引用(即父网格表单),请将 Public ParentFormGrid 定义为 GridName(注意 ParentForm 是保留名称)并在加载子表单时设置

PollInstIn.ParentFormGrid = Me

This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.

通过这种方式,您始终可以访问父表单,即当您在子表单中编辑的行上保存更改时重新加载网格。

回答by Pamungkas Jayuda

make Private Sub LoadDetail()to a public :

Private Sub LoadDetail()到市民:

Public Sub LoadDetail()

It work on my project. Hopely its what you want

它适用于我的项目。希望它是你想要的

回答by heysulo

Dim PolInstIn As New SubForm1

Private Sub LoadDetail()

    PolInstIn.Name = "Sub From"
    PolInstIn.Show()
    PolInstIn.TopLevel = False
    PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    PolInstIn.Dock = DockStyle.Fill
    PolInstIn.Update()
    PolInstIn.Refresh()

    Me.GroupBox6.Controls.Add(PolInstIn)


End Sub