Vb.Net - 访问另一种形式的控件中的文本 [英] Vb.Net - Accessing text in controls on another form

查看:20
本文介绍了Vb.Net - 访问另一种形式的控件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 vb.net 还很陌生,希望能够从另一个打开的表单访问值(例如文本框上的 .text).在我的应用程序中,我从主窗体打开一个窗体,当我尝试访问主窗体上控件中的文本时,我无法看到控件上的 .text 值.

I am fairly new to vb.net and would like to be able to access the value (such as .text on a textbox) from another an open form. In my application I open a form from my main form, and when I try to access the text in the controls on the main form I am unable to see the .text value on the control.

我可以很好地遍历主窗体上的所有控件,但是当我想查看实际值时,所有控件都是空的.我的控件(如文本框和组合框)位于选项卡控件和组框内.

I can loop through all controls on the main form just fine, but when I want to see the actual values, all controls are empty. My controls such as text boxes and combo boxes are inside of a tab control and group boxes.

有没有办法让打开的表单上的所有 .text 或值从另一个打开的表单可用?

Is there a way to make all .text or values on the open form available from the other open form?

这是我在主窗体上循环控件的方式.

Here is how I am looping through the controls on the main form.

Try

    For Each Tp As TabPage In UserData.UserTabControl.TabPages 

    'Name of Tabcontrol is UserTabcontrol

        For Each gbx As GroupBox In Tp.Controls


            For Each ctrl As Control In gbx.Controls

                    If ctrl.Name = "UserName" Then
                        MsgBox(UserData.UserName.Text) 'Messagebox here is empty
                    End If

            Next ctrl

        Next gbx


    Next Tp


    Me.Close()

Catch ex As Exception
    MsgBox(ex.Message)
End Try

提前致谢.克里斯

推荐答案

如果您想在打开的表单上引用控件,请将其命名为 Form1:首先向调用表单添加一个 Form1 属性或变量:

If you would like to reference controls on an open form, call it Form1: First add a Form1 property or variable to the calling form:

Public Class Form2
    Public Property f1 As Form1

    ...
    Private Sub DoSomething()
        MsgBox("Here's some text from Form1: " & f1.Textbox1.Text)
    End Sub
End Class

在被调用者表单中,将Form2属性设置为表单对象:

In the callee form, set the Form2 property to the form object:

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
       Form2.f1 = Me
       Form2.ShowDialog() ' or Form2.Show()
    End Sub
End Class

然后您可以使用 f1 属性从 Form2 引用所有 Form1 对象.

You can then reference all Form1 objects from Form2 using the f1 property.

这篇关于Vb.Net - 访问另一种形式的控件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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