C#如何迭代窗体的自定义控件 [英] c# How to iterate a form's custom controls

查看:106
本文介绍了C#如何迭代窗体的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#Gurus

我已经创建了自己的自定义Textbox控件,打算在我的应用程序的每个表单中使用它,而不是标准Textbox控件.
应用启动时我要做的一件事是调用一个通用的例程/方法/过程(随便叫它),该例程通过窗体上的所有控件进行迭代,并根据用户选择的配色方案设置其颜色.这对于标准窗口的控件来说一切正常,但对于自定义控件的新属性FocusColor则无效.我正在使用一个非常标准的循环来遍历控件:

Hi C# Gurus,

I have created my own custom Textbox control which I intend to use on every form in my application rather than the standard Textbox control.
One of the things I do when the app starts is to call a common routine/method/procedure (call it what you will) which iterates thru all the controls on the form, settings it''s colours according to the user selected colour scheme. This all works fine for the standard window''s controls, but not for the custom control''s new property: FocusColor. I''m using a pretty standard loop to iterate thru the controls:

foreach(Control c in f.Controls){}

当它碰到自定义文本框控件时,设置Back和ForeColor属性可以正常工作,但是我无法设置新的FocusColour 属性,因为它不会出现在智能感知中并且程序不会如果我手动输入属性,则运行(显示错误),即

When it hits the custom textbox control, setting the Back and ForeColor properties works fine, but I can''t set the new FocusColour property because it doesn''t appear in intellisense and the program won''t run (shows errors) if I manually type the property i.e.

c.FocusColour = Colors.Yellow;

我的问题是:如何遍历表单上的所有自定义控件来设置新属性,或者在上述循环中找到自定义控件后,如何包含自定义属性?

预先感谢,
Walter

My question is: How can I iterate thru all the custom controls on the form to set the new properties, or how can I include the custom properties once a custom control is found in the above loop?

Thanks in advance,
Walter

推荐答案

也许可以帮助您:
Maybe this could help you:
foreach (Control c in this.Controls)
{
   if (c is CustomControl)
      ((CustomControl)c).FocusColour = Color.Yellow;
}


这篇关于C#如何迭代窗体的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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