我想将字符串转换为toolstripcombobox控件 [英] i want to cast string to toolstripcombobox control

查看:64
本文介绍了我想将字符串转换为toolstripcombobox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为toolstripcombobox控件



i有一个字符串checked_module_name,这是一个名为toolstripcombobox控件

我需要通过它的名称来访问这个控件,但是从字符串

来修改它的selectedindex



ToolStripComboBox x = this.Controls [checked_module_name]作为ToolStripComboBox ;



给我一个错误无法通过引用转换,装箱转换将类型'System.Windows.Forms.Control'转换为'System.Windows.Forms.ToolStripComboBox'取消装箱转换,包装转换或空类型转换

i want to cast string to toolstripcombobox control

i have a string checked_module_name and this is a name of toolstripcombobox control
and i need to access this control by it's name but from the string
to modify it's selectedindex

ToolStripComboBox x = this.Controls[checked_module_name] as ToolStripComboBox;

give me an error Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripComboBox' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

推荐答案

ToolStripComboBox不是Control,但实际上是ToolStripItem,并且这些不能放入ControlCollection中。 ToolStrip具有类型为ToolStripItemCollection的Items属性。这就是你应该看的地方。



Alan。
ToolStripComboBox is not a Control but is in fact a ToolStripItem and these cannot be put into a ControlCollection. A ToolStrip has an Items property of type ToolStripItemCollection. That's where you should look.

Alan.


这不仅是不可能的,它不仅没有意义,它完全错误思考。



我已经有了这种想法。一些,得到错误甚至异常你不能做A和B尝试询问如何做A和B。唯一正确的答案是:不要这样做。怎么也不清楚。



更多关于你的情况和可能的实际需求:当你需要在 System.Windows.Forms 按名称。通过引用访问所有控件。即使您在设计器中添加了控件,设计器也会为您生成一个可用于访问控件实例的成员。如果您编写自己创建控件实例的代码,则必须注意保留对该控件的引用,以便在您使用它时访问该控件;它可以是您需要访问控件的范围中定义的某个变量或类型成员。



如何创建和使用此类变量或名称?什么是范围,如何控制它?回答这样的问题与询问如何编程?几乎相同。从你的问题来看,我觉得这是你真正需要学习的东西。别担心,很多人都这样做了;你也会这样做。







不要与酒店混淆 Control.Name 。在实践中,经验法则是:永远不要依赖这个属性。它可能会也可能不会被赋值给某个值,这个值可能是也可能不是唯一的(是的,如果您只是在设计器中添加一个控件,如果您尝试在设计器的属性网格中修改它,则唯一值是选择;它只是不保证任何东西)。最好完全忽略这个属性。



-SA
It's not just impossible, it's not just makes no sense, it's totally wrong thinking.

I'm already with this kind of thinking. Some, getting the error or even exception "You cannot do A and B" try to ask "How to do A and B". The only right answer is: don't do it. How it cannot be unclear.

More to your case and possible practical needs: there are no cases when you need to find anything in System.Windows.Forms by name. All controls are accessed by reference. Even if you added a control in designer, the designer will generate a member for you which you can use to access a control instance. If you write code which creates a control instance yourself, you have to take care to preserve the reference to this control to be accessed in the point where you use it; it can be some variable or type member defined in the scope where you need to access the control.

How to create and use such variables or names? What is scope, how to control it? Answering such question would be nearly the same as asking "how to do programming?". From your question, I feel this is what you really need to learn. Don't worry, many did it; you will do, too.



Don't be confused with the property Control.Name. In practice, the rule of thumb is: never rely on this property. It may or may not be assigned to some value, this value may or may not be unique (yes, if you simply add a control in a designer, if you try to modify it in the property grid of the designer, the unique value is chosen; it just does not guarantee anything). It would be the best just to ignore this property completely.

—SA


这篇关于我想将字符串转换为toolstripcombobox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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