将.Text信息从一种形式转换为另一种形式 [英] Taking .Text information from one form to another form

查看:108
本文介绍了将.Text信息从一种形式转换为另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



很简单,我想以另一种形式从文本框中获取.text信息,并将其作为变量存储在新的形式中.

到目前为止,我拥有的代码是:

Hi,

Quite simply I want to take the .text information from a text box in another form and store it in as a variable in a new one.

So far the code I have is:

string userName;

frm_LogonScreen Logon = new frm_LogonScreen();

userName = Logon.tbx_UserName.Text;



但它不会接.任何帮助将不胜感激:)

谢谢.



but it wont pick it up. Any help would be most appreciated :)

Thanks.

推荐答案

根据您提供的代码,您没有显示该对话框,因此文本框中没有任何文本.没有什么可获取"的.
According to the code you''ve provided, you''re not showing the dialog, thus no text is being put into the textbox. There''s nothing to "get".


好吧,您甚至还没有显示子窗体.因此,不太可能已经填写了文本框(或什至尚未创建).

另外,不要直接访问该窗体中的子控件,而要公开一个公共属性,该属性从文本框中返回文本.显示表单后,访问表单的公共属性以获取所需的文本,然后将其分配给userName 字段.

在子对话框中,如下显示文本:

Well you have not even shown the child form yet. So it''s unlikely that the textbox has been filled in (or if it has even been created yet).

Also, instead of accessing the child control in that form directly, expose a public property that returns the text from the textbox. After you show the form, access the form''s public property to get the text you want and then assign it to the userName field.

In the child dialog, expose the text as follows:

public string Username
{
    get
    {
        return textBoxUsername.Text;
    }
}



现在在父对话框中执行以下操作:



Now in the parent dialog do this:

string userName;

var dialog = new ChildForm();
if (dialog.ShowDialog() == DialogResult.OK)
{
    userName = dialog.Username;
}


这不起作用.您正在做的是创建表单的新实例并尝试从中读取值为空(如果默认答案为空)的值.

请在此处.这应该对您有帮助.
This won''t work. What you are doing is creating a new instance of the form and trying to read the values from it which would be empty (if the default answer is empty).

Have a look here. This should help you.


这篇关于将.Text信息从一种形式转换为另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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