且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将文本值传递给另一个表单文本框

更新时间:2023-02-11 19:21:34

您的代码与您说的相反.您的代码将其他窗体的文本框中的文本分配给当前窗体的文本框中(但是,如果我理解正确的话,您将尝试执行相反的操作).

通常,直接访问另一种形式的控制变量不是一个好主意.***公开一种公共方法,例如另一种形式的 SetSomeText(string text),该方法会在内部将文本分配给正确的文本框.
Your code''s doing the reverse of what you say you need to do. Your code assigns the text from the other form''s textbox to your current form''s textbox (but you are trying to do the opposite if I understand it right).

In general it''s not a good idea to access another form''s control variables directly. It may be better to expose a public method, something like SetSomeText(string text) in this other form that will internally assign the text to the right textbox.


添加公共property形成表格b.然后您可以执行以下操作:
Add a public property to form b. Then you can do something like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     formB.PassedText = formmail.txtto.Text
 End Sub


这假定Form b上的属性称为PassedText,并且Form b由Form A创建.如果不是,则请描述您的应用程序的结构.

然后在您财产的二传手中


This assumes that the property on Form b is called PassedText and that Form b is created by Form A. If it is not then please describe the structure of your application.

Then in the Setter of your property

txtAto.Text = Value



希望对您有所帮助.



Hope this helps.


您认为这更复杂.

为此,您必须有权访问两个表单实例:不是定义,而是访问正在运行并保存所需信息的表单的实际实例.

使用您的名字:

假设您的按钮"button1"位于"formb"上,那么如果"formmail"包含"forma"的实例,那么您可以这样做.

首先要检查的是:您是否在复制正确的方法?您要在TextBox中复制的文本是"txtto",还是要在另一表格上放置"txtAto"的地方?

在复制指令上放置一个断点,然后检查.

尝试访问其他表单控件的内容不是一个好主意:它将两个表单紧密地捆绑在一起.您不能不检查就更改一个不会影响另一个.
而是使用属性:
This is more complex that you think.

In order to do this, you must have access to both form instances: not the definitions, but the actual instance of the form that is running, and holds the information you want.

Using your names:

Assuming that your Button "button1" is on "formb", then if "formmail" contains the instance of "forma" then you can do it.

The first thing to check is: are you copying the right way? Is the text you want to copy in the TextBox "txtto" and the place you want to put it "txtAto" on the other form?

Put a breakpoint on the copy instruction, and check.

It is not a good idea to try accessing the content of other form controls: it ties the two forms too tightly together. You can''t change one without checking is doesn''t affect the other.
Instead, use a property:
Public Property ToAddress() As String
    Get
        Return tbToAddress.Text
    End Get
    Set
        tbToAddress.Text = value
    End Set
End Property

,然后访问它.