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

问题描述

我正在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);
                   }

               }

           }
       }

推荐答案

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;
                }
            }
        }
    }
}


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);



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

11-03 01:21