如何通过从C#中的用户输入调用控件的属性来控制它的属性 [英] how to control a control's property by calling it from a user input in C#

查看:156
本文介绍了如何通过从C#中的用户输入调用控件的属性来控制它的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题适用于Windows表单应用程序



我想在表单中启用一个按钮默认情况下,在运行时设置为禁用。



表单中有n个按钮。用户必须在提供的文本框中键入要禁用的按钮的名称。



一旦按钮名称由用户提供,我必须启用那个特定的控件马上。



i不想循环遍历表单中所有按钮类型的控件,其中包含用户给出的名称不适合我的项目标准。



The question is for Windows form application

I want to enable a button in a form which is set to disable by default, in the run time.

There are "n" number of buttons in the form. User has to type the name of the button he want to disable, in a text box provided.

Once the button name is given by the user, I have to enable that particular control right away.

i don't want to loop through all the controls which are of button type in the form with the name given by the user as it do not fit my project criteria.

button.enable = true;



以上代码没有帮助,因为在用户在运行时提供输入之前我不知道按钮的名称。



我尝试过拳击和拆箱选项,但是我无法获得一个明确的代码来使其正常运行。



提前致谢


The above code do not help as I will not be knowing the name of the button until the user has give the input at the run time.

I tried "boxing" and "unboxing" option, but I could not get a clear code to make it function.

Thanks in advance

推荐答案

请阅读我对该问题的评论。





是的,谢尔盖是对的!

这是个坏主意。 用户不应该输入任何按钮名称,甚至不知道任何按钮名称。



我留下我的答案用于任何其他用途。



谢谢谢尔盖。



[/编辑]



WinForms:

Please read my comment to the question.


Yes, Sergey is right!
It's bad idea. The user should not type any button names or even know any.

I leave my answer for any other usage.

Thank you, Sergey.

[/EDIT]

WinForms:
String sInput = textBox1.Text;
Button btn = (Button)this.Controls[sInput];
btn.Enabled = true;





在上面的例子中我正在使用转换和类型转换(C#编程指南) [ ^ ]。每个Windows控件都继承自控制类 [ ^ ]。

注意:我强烈建议使用尝试...捕获 [ ^ ]阻止处理错误时所需名称的控件不存在。



您可以使用 foreach [ ^ ]循环。



WebControls:

请参阅:如何:按名称查找元素 [ ^ ]



In above example i'm using Casting and Type Conversions (C# Programming Guide)[^]. Each Windows control inherits from Control class[^].
Note: i strongly recommend to use try ... catch[^] block to handle the error when controls on desired name does not exists.

You can loop through the collection of controls using foreach[^] loop.

WebControls:
Please see: How to: Find an Element by Its Name[^]


这是一个完全糟糕的设计理念。用户不应键入任何按钮名称,甚至不知道任何按钮名称。首先,你永远不应该使用属性 Control.Name 。它是为设计师的功能而保留的(这本身并不是一个好主意)。只有当您假设名称是随机值时,您才能将UI代码更正。如果你的意思是 Button.Text ,那就更关心了。您可以使用它,但它使项目不受支持。 UI设计确实需要您更改屏幕上的任何可见文本而不会对其功能产生任何影响。



您应该按功能对按钮进行分组。例如,您可以将一组按钮一起启用和禁用;然后你可以循环完成。 在最坏的情况下,将属性 Control.Tag 分配给对您的功能有帮助的对象。 (我再说一遍:在最坏的情况下。)这样,你将所有与演示相关的字符串信息与UI功能隔离开来。



如果你需要遍历所有控件,从一些父控件( Form 或任何其他容器)开始,请记住你应该使用 Control.Controls 收集来追踪某些控制的孩子。



-SA
This is the utterly bad design idea. The user should not type any button names or even know any. First of all, you should never use the property Control.Name. It is reserved for the functionality of the designer (which is itself not a good idea). You UI code it correct only if you assume that the name is a random value. It's more concerned if you mean Button.Text. You can use it, but it makes the project unsupportable. UI design really requires that you change any visible text on the screen without any effect on its functionality.

Instead of doing such weird thing, you should group buttons by functionality. For example, you can have an array of the buttons to be enabled and disabled together; then you can do it in a loop. In worst case, assign the property Control.Tag to some object helpful for your functionality. (I repeat: in worst case.) This way, you isolate all string information related to presentation from UI functionality.

And if you ever need to traverse all your controls, starting with some parent control (Form or any other container), remember that you should do it recursively, using Control.Controls collection to traverse children of some control.

—SA


这篇关于如何通过从C#中的用户输入调用控件的属性来控制它的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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