“要清除所有控件" [英] "To clear all controls "

查看:107
本文介绍了“要清除所有控件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
在我的表格中,我使用了多个容器,例如面板,组框.并且根据我的要求,我必须清除驻留在容器中的所有控件.为此,我编写了如下代码:-

hello
In my Form I had used multiple container such as panel,group box.and as per my requirement i have to clear all the controls which are reside in container. For this i have wrote a code as follows:-

foreach (Control  x in ControlContainer.Controls)//Error
           
            {
                

                if (x is System.Windows.Forms.TextBox)
                    ((TextBox)x).Text  = String.Empty;

                else  (x is System.Windows.Forms.ComboBox)
                {
                    ((ComboBox)x).SelectedIndex = -1;
                    ((TextBox)x).Text = String.Empty;
                }


但是它不能正常工作....我收到如下错误:对象"不包含控件"的定义

请帮助

内联代码转换为代码块-OriginalGriff [/edit]


But its not working properly.... i''m receiving error like:''object'' does not contain a definition for ''Controls''

please help

[edit]Inline code converted to code block - OriginalGriff[/edit]

推荐答案

这很有意义,具体取决于您使用的对象类型.如果ControlContainer不是从Control派生的对象的实例,则它将不包含Controls列表. ControlContainer听起来像是一个类,而不是一个实例?
试试:
That makes sense , depending on what kind of object you are using. If ControlContainer is not an instance of an object that derives from Control, then it will not contain a Controls list. ControlContainer sounds like a class, rather than an instance?
Try:
GroupBox gb = new GroupBox();
...
foreach (Control x in gb)
   {
   ...
   }


在这种情况下,您需要使用递归:

You need to use recursion in this case:

static void CleanupTextBox(TextBox textBox) {
   //whatever cleanup you need
}
static void CleanupComboBox(ComboBox textBox) {
   //whatever cleanup you need
}
static void CleanupBoxes(Control parent) {
   TextBox textBox = parent as TextBox;
   ComboBox comboBox = parent as ComboBox;
   if (textBox != null)
      CleanupTextBox(textBox);
   else if (comboBox != null)
      CleanupComboBox(comboBox);
   foreach(Control control in parent.Controls)
      SetupTextBoxes(control);
}

//...

CleanupBoxes(myFormInstance);
or, in the method of your form:
CleanupBoxes(this);



—SA



—SA


这篇关于“要清除所有控件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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