在 vb.net 中将变量从一种形式传递到另一种形式 [英] Passing variable from one form to another in vb.net

查看:31
本文介绍了在 vb.net 中将变量从一种形式传递到另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经看了 10 遍了,但每个答案都太针对问题了.

I've looked this question up 10 times but each answer is too specific to the question.

我有两个公共课程,每个表格一个.

I have two public classes, one per form.

第一个表单有一个文本框和两个按钮:

The first form has a textbox and two buttons:

Public Class frmAdd
    Public addvar As String
    Public Sub UltraButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles btnAddNote.Click

        If txtAdd.Text = String.Empty Then
            MsgBox("Please complete the notes textbox!")
        Else
            addvar = txtAdd.Text
            MsgBox(addvar)
            Close()
        End If
    End Sub

    Public Sub UltraButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class

在第二种形式中,我想采用那个 addvar 变量并说

In the second form I want to take that addvar variable and say

Public Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click

frmAdd.show()

me.textbox1.text = addvar

如何让它在 vb.net 中工作?

How do I get this to work in vb.net?

推荐答案

您需要从 frmAdd 值中读取字段

You need to read the field out of the frmAdd value

Me.textbox1.Text = frmAdd.addvar

请注意,在表单完成并关闭 (Me.close) 之前,此值将不可用.因此,您想使用 ShowDialog(在表单关闭前不返回)与 Show(在显示表单后立即返回).

Note that this value won't be available until the form has completed, and is closed (Me.close). Hence you want to use ShowDialog (doesn't return until form is closed) vs. Show (which returns immediately after displaying the form).

frmAdd.ShowDialog()
Me.textbox1.Text = frmAdd.addvar

这篇关于在 vb.net 中将变量从一种形式传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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