表格中存在清晰控件的问题 [英] Problem with clear controls in form

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

问题描述

我正在make循环中在窗体内放置一些控件...所有工作正常,期望combox coz我需要处理大小写...

ClearControlsItems从comboxs中获取arraylist ...如果当前的combox像数组中的combox,我需要什么...我将显示"Choose a Item",否则我将清空数据源...

但是结果与我想要的不符吗?

I''m make loop to rest some controls inside form ... all work correctly expect combox coz i need to handle case ...

ClearControlsItems take arraylist from comboxs ...what i need if current combox like the combox in array ... I will display "Choose an Item" else i will empty datasource ...

But result not match what i want ?

private void ClearControlsItems(Control c, ArrayList comboxNames)
       {
           foreach (Control control in c.Controls)
           {
               if (control is TextBox)
               {
                   control.Text = "";
               }
               else if (control is ComboBox)
               {
                   ComboBox combox = control as ComboBox;
                   int arrayLength = comboxNames.Count;
                   for (int i = 0; i < arrayLength; i++)
                   {
                       if (combox.Name == ((ComboBox)comboxNames[i]).Name)
                       {
                           ((ComboBox)comboxNames[i]).Text = "Choose an Item";
                           ((ComboBox)comboxNames[i]).Items.Remove(comboxNames[i]);
                           arrayLength = comboxNames.Count-1;
                       }
                       else
                       {

                           ((ComboBox)comboxNames[i]).DataSource = null;
                       }
                   }
               }

               else
               {
                   if (control.Controls.Count > 0)
                   {
                       ClearControlsItems(control, comboxNames);
                   }

               }

           }
       }

推荐答案

我会这样:

I would do it like this:

private void ClearControls(Control parent)
{
    foreach (Control ctrl in parent.Controls)
    {
        if (ctrl.IsContainer)
        {
            ClearControls(ctrl);
        }
        else
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = "";
            }
            else if (ctrl is ComboBox)
            {
                // repeatedly casting is ineffecient. Cast it once.
                ComboBox combo = ctrl as ComboBox;
                switch (combo.Name)
                {
                    case "combo1" :
                    case "combo2" :
                        // do something in the combobox - it's not clear what 
                        // you're trying to achieve, maybe selecting the first
                        // item in the Items collection for the current combobox?
                        combo.SelectedIndex = 0;
                        break;
                    default:
                        combo.DataSource = null;
                        break;
                }
            }
        }
    }
}


这里是您想要的解决方案...完美的工作:)

Here''s the solution as you want ... work perfect :)

private void ClearFormControls(Control c, List<ComboBox> comboxNames)
{
    foreach (Control ctrl in c.Controls)
    {
        if (ctrl is TextBox)
        {
            ctrl.Text = "";
        }
        else if (ctrl is ComboBox)
        {
            int len = comboxNames.Count;
            for (int i = 0; i < len; i++)
            {
                if (ctrl.Name == (comboxNames[i].Name))
                {
                    ctrl.Text = "Choose an item";
                    comboxNames.RemoveAt(i);
                    len = len - 1;
                    break;
                }
                else
                {

                    if (!comboxNames.Contains(ctrl))
                    {
                        // if you bind from array use this.
                        (ctrl as ComboBox).Items.Clear();
                        (ctrl as ComboBox).Text = "";

                        // If you bind from data source use this.
                        //  (ctrl as ComboBox).DataSource = null;
                    }
                }


            }
        }

        else
        {
            if (ctrl.Controls.Count > 0)
            {
                ClearFormControls(ctrl , comboxNames);
            }

        }

    }
}




呼叫




calling

List<ComboBox> seq = new List<ComboBox>();
           seq.Add(comboBox_AccountCategory);
           seq.Add(comboBox_CompanyName);
           seq.Add(comboBoxAge);
           seq.Add(comboBoxSalary);
           ClearFormControls(this, seq);


您的解决方案意味着需要递归解析传入参数Control''c的容器对象.
而且,您在ArrayList参数''comboxNames中传递的内容不是ComboBoxes的名称(这表示字符串),而是对ComboBox实例的引用的集合.

因此,如果您事先拥有所有需要通过此处显示的代码进行处理的ComboBox的引用,则无需进行递归处理.

另一方面,显然,您要求将每个TextBox的Text属性设置为空字符串的要求似乎需要递归.

因此,我将其分解为两个解决方案:一种非递归解决方案,它仅迭代您的ComboBox引用集合并执行您需要做的事情...

第二种递归解决方案是重置所有TextBoxes.

现在,如果您遇到一堆文本框都具有相同名称,而全部都位于不同容器对象中的情况,那么.NET(2.0及更高版本)将为您提供Controls.Find(string"ControlName," bool SearchAllChildren)方法返回一个数组,显然,它可以为您进行递归搜索.

如果您确实有一堆TextBox,那么很多在同一容器中(显然它们必须具有唯一的名称),或者分散在许多容器中,嵌套到任意深度...

我会考虑在Form Load EventHandler中对表单进行递归遍历",在其中构建List< TextBox>以后可以再次使用它们进行重置,而不必重复多次.

但是,如果要在运行时添加控件,则意味着在创建新控件时,需要将它们添加到ComboBoxes或TextBoxes的列表中……如果它们需要按此处显示的方式进行处理.而且,如果可以在运行时将其删除:好吧,您可以找出其中的一个:)

最好,比尔
Your solution implies a need for a recursive parsing of the container object passed in to parameter Control ''c.

And, what you pass in here in the ArrayList parameter ''comboxNames is not the names of the ComboBoxes (that would imply strings), but a collection of references to the instances of ComboBox.

So, if you have, in advance, all the references to the ComboBoxes that require handling by the code you show here, I see no need for recursion to handle them.

On the other hand, your requirement, evidently, to set every TextBox''s Text property to an empty string, appears to require recursion.

So I would decompose this into two solutions: a non-recursive one that just iterates your collection of ComboBox references and does what you need to do ...

And a second, recursive, solution to reset all the TextBoxes.

Now, if you had a situation where a bunch of TextBoxes all had the same name, but were all in different container objects, then .NET (2.0 and later) gives you the Controls.Find(string "ControlName," bool SearchAllChildren) method to return an array, and, obviously it can do a recursive search for you.

If you really had a whole bunch of TextBoxes, many in the same container (where they obviously must have unique names), or scattered across many containers, nested to whatever depth ...

I''d consider doing a recursive "walk" of the Form in the Form Load EventHandler where I built up a List<TextBox> to use later on for resetting them again, without possibly having to recurse more than once.

However, if you are adding controls at run-time, then that means that, as you create the new Controls, you need to add them to the list of either ComboBoxes, or TextBoxes ... if they require being handled as you show here. And, if they can be deleted at run-time: well, you figure that one out :)

best, Bill


这篇关于表格中存在清晰控件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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