从其他形式的 VB.NET 访问控制 [英] Access control from other form VB.NET

查看:19
本文介绍了从其他形式的 VB.NET 访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

Visual Studio 没有抛出任何错误,但实际上 Form1 中的 ListView 控件没有任何反应.我尝试将修饰符从 Friend 更改为 public,但没有成功.还有什么我想念的吗?

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?

推荐答案

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

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.

    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

这篇关于从其他形式的 VB.NET 访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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