如何将控件投射到按钮,文本框或标签. [英] how to cast a control to button, textbox, or label.

查看:121
本文介绍了如何将控件投射到按钮,文本框或标签.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我想为控件创建一个属性,使其可以添加不同类型的控件,例如文本框,标签等.我使用的是collectionBase来保存我的收藏,但是索引器的返回类型只能是一种控件.
让我们说标签.它只能创建标签,仅此而已.我该如何更改仅具有另一个属性而创建的类型.

Good day,

i wanted to create a property for my control which lets it add different types of controls, like textbox, label and etc.. I am using a collectionBase to hold my collections, but the return type of the indexer could only be one kind of control.
let''s say label. it can only create label and nothing more. how could i change the type that is created just by having another property.

public Label this[int index]
        {
            get
            {
                return (Label)List[index];
            }
        }



这段代码只会创建一个标签,我该如何更改类型以创建其他类型的控件.还是有人可以帮助我找到标签按钮文本框的通用类型?

谢谢



this code, will only create a label, how could i change the type to create other kinds of control. Or can someone help me find a generic type for label buttons textbox?

thank you

推荐答案

这些控件背后的常见类型是"Control".因此,请使用List<control></control>而不是CollectionBase.
那么,您的财产是:
The common type behind those controls is "Control". Consequently, use a List<control></control> instead of CollectionBase.
Your property is then:
Public Control this[int index]


您可以添加特殊功能,例如


You may add specialised functions, e.g.

Public TextBox GetTextBoxAt(int index)
{
    return this[index] as TextBox;
}


请注意,如果this[index]不是TextBox,它将返回null.


Note that it will return null if this[index] is not a TextBox.


您无法更改控件的类型"-如果您声明了TextBox,那么它是一个TextBox-您可以将其投射到其派生的事物链中较低的任何位置,但不能横向投射.在这种情况下,Label和TextBox都是从Control派生的,因此您可以很高兴地说:
You can''t "change the type" of a control - if you declare a TextBox, then it is a TextBox - you can cast it to anything lower down in the chain of things it is derived from but you can''t cast sideways. In this case, both Label and TextBox are derived from Control, so you can happily say:
TextBox t = new TextBox();
Label l = new Label();
Control c = (Control) t;
c = (Control) l;

发生这种情况时,您甚至不需要强制转换,因为它可以很好地运行.
但随后您不能说

As it happens you don''t even need to caste this as it will work perfectly well.
But you can''t subsequently say

c = t;
l = (Label) c;

,因为它将抱怨没有从TextBox到Label的转换.

如果您要执行的操作是拥有一个索引器,该索引器根据所使用的上下文返回两种不同的类型,则您不能-C语言中没有(可以理解的)机制可以执行此操作-就像没有索引器一样C#中用于两种方法的机制,仅在返回类型上有所不同.

您可以使其通用( MSDN索引器 [ ^ ]包含一个通用示例),但您无法完全做到自己想要的.

Because it will complain that there is no conversion from a TextBox to a Label.

If what you are trying to do is have an indexer that returns two different types depending on the context it is used in, then you can''t - there is (understandably) no mechanism in C# to do that - just like there is no mechanism in C# for two methods differing only in return type.

You could make it generic (MSDN indexers[^] includes a generic example) but you can''t do what you want exactly.


更通用的
public Control this[int index]
{
  get
  {
    return (Control)List[index];
  }
}


然后可能会强制转换为属性之外的特定控件类型.


And then cast to the specific control type outside of the property maybe.


这篇关于如何将控件投射到按钮,文本框或标签.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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