来自其他形式的 VB.NET 的访问控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18081978/
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 14:34:05  来源:igfitidea点击:

Access control from other form VB.NET

vb.netwinforms

提问by user2373081

I am working on a vb.net project in VS 2012 that has multiple forms. I have, lets say, Form1 with a ListView and I call a From2 from Form1. I add this code to the Load event of Form2:

我正在 VS 2012 中处理一个具有多种形式的 vb.net 项目。比如说,我有一个带有 ListView 的 Form1,我从 Form1 调用了一个 From2。我将此代码添加到 Form2 的 Load 事件中:

Form1.ListViewTest.Items.Add("test")

The visual studio throws no errors but in fact nothing happens in the ListView control in Form1. I tried to change Modifiers from Friend to public with no success. Is there something else that I miss?

Visual Studio 不会引发任何错误,但实际上 Form1 的 ListView 控件中没有任何反应。我尝试将 Modifiers 从 Friend 更改为 public,但没有成功。还有什么我想念的吗?

采纳答案by Louis van Tonder

You have to access the instance of form1, not the form 1 object itself. Step through all open forms, and get the one you want. I personally use the .Tag object of a form, and just add a string in there to identify it.

您必须访问 form1 的实例,而不是 form 1 对象本身。逐步浏览所有打开的表单,并获取您想要的表单。我个人使用表单的 .Tag 对象,并在其中添加一个字符串来标识它。

    For Each f As Form In My.Application.OpenForms

        If Not f.InvokeRequired Then
            ' Can access the form directly.
            'Get main form , use main form
            If f.Tag = "main" Then
                Dim fcast As New form_form1 '<< whatever your form name
                fcast = f
                Dim xitem As New ListViewItem
                xitem.Text = "blah"
                xitem.SubItems.Add("sub blah")

                fcast.listview1.Items.Add(xitem) '<< use listview name
            End If

        End If

    Next

回答by toalopez

This is really simple, if you going to reference any control from a dialog window, there are two ways to do this, the first one is below:

这真的很简单,如果您要从对话框窗口引用任何控件,有两种方法可以做到这一点,第一种如下:

Part I

第一部分

1 - you make sure that the control that you want to want to access the modifier is set to Public

1 - 您确保要访问修饰符的控件设置为 Public

2 - then in the dialog window, you do this:

2 - 然后在对话窗口中,您执行以下操作:

Dim f as Form1 ' you do not need to use new, because you don't want to use another new fresh version, you want to use the existing one, so you only use the form name as a reference

Dim f as Form1 ' 你不需要使用new,因为你不想使用另一个新的新版本,你想使用现有的,所以你只使用表单名称作为参考

3 - you do this:

3 - 你这样做:

f = Ctype(Me.Owner, Form1) ' you do not need to cycle Through the form collection, because every form name is unique

f = Ctype(Me.Owner, Form1) ' 不需要循环遍历表单集合,因为每个表单名称都是唯一的

f.LightGridControl.Text = "This information"

f.LightGridControl.Text = "此信息"

Part II

第二部分

You can create a private vairiable in the declaration section of the the main window

您可以在主窗口的声明部分创建一个私有变量

Private _mainWin as form1 ' this is the same window unique name

Private _mainWin as form1 '这是相同的窗口唯一名称

then you create a

然后你创建一个

Public Property MainForm as Form1 Get Return _mainWin End Get Set (ByRef value as Form1) _mainForm = value End Set End if

Public Property MainForm as Form1 Get Return _mainWin End Get Set (ByRef value as Form1) _mainForm = value End Set End if

in the Button Click Event, you can write this:

在按钮点击事件中,你可以这样写:

Dim f as New Form2

Dim f as New Form2

_mainForm = Me

_mainForm = 我

f.DialogBox(Me)

f.DialogBox(我)

Then in any event from form2 that you want to access the control from form1 (MainWindow), you just do this:

然后在任何想要从 form1 (MainWindow) 访问控件的 form2 中,您只需执行以下操作:

Dim f as form1

将 f 调暗为 form1

f.MainForm.TextBox1.Text = "My Information"

f.MainForm.TextBox1.Text = "我的信息"

It is that easy my friend!! Good Luck, let me know how did it go.

朋友就这么简单!!祝你好运,让我知道它是怎么回事。

Have a Nice Day!! Hope this can help somebody!!

祝你今天过得愉快!!希望这可以帮助某人!!