从类访问文本框 [英] Accessing Textbox From A Class

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

问题描述

所以我在Form1上有一个Textbox1,我想把它传递给一个要处理的类,然后再喷回到表单中。我知道它必须在get和set中,但是我很困惑需要进入get和set以及在Form1中保留了什么以及在课堂上发生了什么。任何建议



P.S我认为TextBox1必须进入集合但不确定。我已经在网上看了一下,但没有人想出一个不错的方法。

So i have a Textbox1 on Form1 and i am wanting to pass it into a class to be processed and then spouted back out into the form. I know it will have to be in a get and set, but i get confused what needs to go in get and set and what bit stays in the Form1 and what goes in the class. Any suggestions

P.S i think the TextBox1 will have to go in the set but not sure. I have had a look round online for this but no one has came up with a decent method for this.

推荐答案

你为什么要通过文本框,而不仅仅是它包含的字符串?让UI完成文本框的工作,让类直接处理数据,而不是UI。



只需将Textbox.Text传递给类,而不是整个文本框。
Why would you pass the textbox around, and not just the string that it contains? Let the UI do the work with the textbox, and let the classes take care of working directly with the data, not the UI.

Just pass the Textbox.Text to the class, not the entire textbox.


你将传递给类的不是文本框本身,而只是引用它。



如果要将其作为类属性传递,可以定义属性,如下所示。我把它命名为MyTextBox:



What you will be passing to the class is not textbox itself but only reference to it.

If you want to pass it as class property, you can define property as shown below. I named it MyTextBox:

TextBox m_TextBox;  //Local reference to passed TextBox
public TextBox MyTextBox
{
   set 
{
    m_TextBox = value;
}

private get 
{
    return m_TexBox;
}


}





原始TextBox保留在原地。你刚刚传递了它的参考。在您的类中,您可以使用MyTextBox属性getter访问原始TextBox实例的所有成员。



Your original TextBox stays wherever it was. You just passed reference to it. In you class you have acess to all members of the original TextBox instance by using MyTextBox property getter.


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

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