将值传递到文本框 [英] passing value to textbox

查看:93
本文介绍了将值传递到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有formA和formB.

在formA中有textboxA,而在formB中有textboxB.
FormA还具有一个名为buttonA的按钮,单击按钮A时,我将调用formB.

formB.ShowDialog()

在formB中,将在关闭formB时将textboxB的值传递给textboxA.

我的问题是,即使在调试状态下,textboxA仍将空白值传递给textboxA.
但是,它不显示任何值.

在此先感谢

I have formA and formB.

In formA there is textboxA and in formB there is textboxB.
FormA also has a button, named buttonA, and upon clicking buttonA I am calling formB.

formB.ShowDialog()

In formB the value of textboxB will be passed to textboxA upon closing formB.

My problem is textboxA remains blank even though in my debug states that it''s passing a value to textboxA.
However, it''s not displaying any value.

Thanks in advance

推荐答案

做到这一点的方法是在FormB中创建一个公共属性,该公共属性将加载TextBox :.然后,您可以在调用ShowDialog之前加载它,并在表单关闭后重新获取它:
The way to do this is to create a public property in FormB which loads the TextBox:. You then load it before the call to ShowDialog, and retrieve it after the form has closed:
Public Property myValue() As String
    Get
        Return textboxB.Text
    End Get
    Set
        textboxB.Text = value
    End Set
End Property





Dim fb As New FormB()
fb.myValue = textboxA.Text
fb.ShowDialog()
textboxA.Text = fb.myValue


这才有效.尝试一下

Form1

This just works.. try it

Form1

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    DialogResult res = f2.ShowDialog();

    if (res == DialogResult.OK)
    {
        textBox1.Text = f2.TextBoxValue;
    }
}



Form2



Form2

private void button1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}

public string TextBoxValue { get { return textBox1.Text; } }



button1和textBox1是Form1和Form2上的控件.

PS:控件名称仅供参考.

不知道为什么会这样.



button1 and textBox1 are controls on the Form1 and Form2.

PS: Control names are for ref only.

Not sure why this was an issue.


这是非常简单的东西.请尝试一下,


您需要在FormA中的Button click事件上编写此代码. txtB是FormB的文本,而txtA是FormA的文本框.

This is very easy stuffs. Please try with that,


you need to write this code on the Button click event in FormA. And txtB is the text of FormB where as txtA is the text box of FormA.

private void btnOK_Click(object sender, EventArgs e)
        {
            FormB frmB = new FormB();
            DialogResult result= frmB.ShowDialog();

            if (result == DialogResult.Cancel)
            {
                this.txtA.Text = frmB.txtB.Text;
            }
        }



在这里,您需要使txtB的访问修饰符为公共.也就是说,



Here you need to make the access modifier of txtB is public. That is,

public System.Windows.Forms.TextBox txtB;


这篇关于将值传递到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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