需要帮助从字符串中获取控件的值 [英] Need help in fetching value of control from string

查看:58
本文介绍了需要帮助从字符串中获取控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串"Form4.Textbox2.Text",我想做一些读取此字符串并从指定位置获取值的操作.

因此,我的代码将读取字符串abcd,其中包含"Form4.Textbox2.Text",并将尝试获取Form4.Textbox2.Text的值.

假设,

I have string "Form4.Textbox2.Text" and i want to do something that reads this strings and gets value from specified location.

So my code will read string abcd which contains "Form4.Textbox2.Text" and will try to fetch value of Form4.Textbox2.Text.

suppose,

string func(string abcd)

{ 

string result;

result  = getvalue(abcd);

return(result);

}





我称它为





I will call it as

func("Form4.Textbox2.Text")





请帮忙....





please help....

推荐答案

那是行不通的:TextBox2引用不能是静态的,因此您必须指定哪个实例您正在谈论的Form4.由于它们都是在内存中动态创建的,因此您无法轻松地搜索它们,尤其是因为Windows本身不知道实例名称-它与Window Handle一起使用-您可以从Window Title(而不是实例)中获取该名称名称.
我在这里谈论的实例名称是:
That is''nt going to work: the TextBox2 reference can''t be static, so you would have to specify which instance of Form4 you are talking about. Since they are all created dynamically in memory you can''t easily search for them, particularly as Windows itself does not know the instances name - it works with the Window Handle - which you can get from the Window Title, but not from the instance name.
The instance name I am talking about here is:
Form4 f4a = new Form4();
f4a.Show();
Form4 f4b = new Form4();
f5b.Show();

您甚至无法通过创建它们的方法之外的名称f4a和f4b访问它们的实例,除非它们是类级别的变量,即使那样,您仍然需要知道父实例的哪个实例您要参考的课程.

您想做什么,您认为这是最好的进行方式?也许有更好的方法.

You can''t even access they instances via the names f4a and f4b outside the method that created them unless they are class level variables, and even then you need you know which instance of the parent class you are referring to.

What are you trying to do, that you think this is the best way to proceed? There may be a better way.


首先,您尝试在哪里读取该值.如果代码与文本框位于同一窗体上,则可以简单地引用实例.例如:
First of all, where are you trying to read the value. If the code is located on the same form as the text box you can simply refer to the instance. For example something like:
string val = this.Textbox2.Text;


您似乎在尝试引用Form4类,而不是实例.如果这是正确的,并且您正在运行的代码在Form4类之外,请将实例存储在某个地方(或将其作为参数传递给适当的方法),然后使用该实例获取文本框的值.

但是,请注意,在表单外部引用表单的对象(TextBox2)是一个坏习惯,因此请考虑例如为表单创建一个属性,该属性在需要时返回值.


It looks a bit that you''re trying to refer to the class Form4, not the instance. If that''s correct and the code you''re running is outside the Form4 class, store the instance somewhere (or pass it as a parameter to proper method) and using that instance get the value of the text box.

However, note that referring to form''s objects (TextBox2) outside the form is a bad habit, so consider for example creating a property to the form which returns the value when needed.


这篇关于需要帮助从字符串中获取控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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