What is the best way to clear all controls on a form C#?

前端 未结 8 2164
情书的邮戳
情书的邮戳 2020-11-27 18:00

I do remember seeing someone ask something along these lines a while ago but I did a search and couldn\'t find anything.

I\'m trying to come up with the cleanest wa

相关标签:
8条回答
  • 2020-11-27 18:23

    What I have come up with so far is something like this:

    public static class extenstions
    {
        private static Dictionary<Type, Action<Control>> controldefaults = new Dictionary<Type, Action<Control>>() { 
                {typeof(TextBox), c => ((TextBox)c).Clear()},
                {typeof(CheckBox), c => ((CheckBox)c).Checked = false},
                {typeof(ListBox), c => ((ListBox)c).Items.Clear()},
                {typeof(RadioButton), c => ((RadioButton)c).Checked = false},
                {typeof(GroupBox), c => ((GroupBox)c).Controls.ClearControls()},
                {typeof(Panel), c => ((Panel)c).Controls.ClearControls()}
        };
    
        private static void FindAndInvoke(Type type, Control control) 
        {
            if (controldefaults.ContainsKey(type)) {
                controldefaults[type].Invoke(control);
            }
        }
    
        public static void ClearControls(this Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                 FindAndInvoke(control.GetType(), control);
            }
        }
    
        public static void ClearControls<T>(this Control.ControlCollection controls) where T : class 
        {
            if (!controldefaults.ContainsKey(typeof(T))) return;
    
            foreach (Control control in controls)
            {
               if (control.GetType().Equals(typeof(T)))
               {
                   FindAndInvoke(typeof(T), control);
               }
            }    
    
        }
    
    }
    

    Now you can just call the extension method ClearControls like this:

     private void button1_Click(object sender, EventArgs e)
        {
            this.Controls.ClearControls();
        }
    

    EDIT: I have just added a generic ClearControls method that will clear all the controls of that type, which can be called like this:

    this.Controls.ClearControls<TextBox>();
    

    At the moment it will only handle top level controls and won't dig down through groupboxes and panels.

    0 讨论(0)
  • 2020-11-27 18:26

    Below are methods I use to clear text from a type of control that implements ITextBox.

    I noticed in the example default boolean values are set. I'm sure you can modify it to set default values of boolean components.

    Pass the Clear method a control type (TextBox, Label... etc) and a control collection, and it will clear all text from controls that implement ITextBox.

    Something like this:

    //Clears the textboxes
    WebControlUtilities.ClearControls<TextBox>(myPanel.Controls);
    

    The Clear method is meant for a Page or Masterpage. The control collection type may vary. ie. Form, ContentPlaceHolder.. etc

            /// <summary>
        /// Clears Text from Controls...ie TextBox, Label, anything that implements ITextBox
        /// </summary>
        /// <typeparam name="T">Collection Type, ie. ContentPlaceHolder..</typeparam>
        /// <typeparam name="C">ie TextBox, Label, anything that implements ITextBox</typeparam>
        /// <param name="controls"></param>
        public static void Clear<T, C>(ControlCollection controls)
            where C : ITextControl
            where T : Control
        {
            IEnumerable<T> placeHolders = controls.OfType<T>();
            List<T> holders = placeHolders.ToList();
    
            foreach (T holder in holders)
            {
                IEnumerable<C> enumBoxes = holder.Controls.OfType<C>();
                List<C> boxes = enumBoxes.ToList();
    
                foreach (C box in boxes)
                {
                    box.Text = string.Empty;
                }
            }
        }
    
        /// <summary>
        /// Clears the text from control.
        /// </summary>
        /// <typeparam name="C"></typeparam>
        /// <param name="controls">The controls.</param>
        public static void ClearControls<C>(ControlCollection controls) where C : ITextControl
        {
            IEnumerable<C> enumBoxes = controls.OfType<C>();
            List<C> boxes = enumBoxes.ToList();
    
            foreach (C box in boxes)
            {
                box.Text = string.Empty;
            }
        }
    
    0 讨论(0)
提交回复
热议问题