There is slight problem with the above code, as it does not clear any conrols embeded within other container.
So adding extra two line and making it Recursive would fix it.
e.g
Just thougt some one else might find it useful, i use it quite a lot to clear my Form values.Code:public void ClearControlValues(System.Windows.Forms.Control Container) { try { foreach(Control ctrl in Container.Controls) { if(ctrl.GetType() == typeof(TextBox)) ((TextBox)ctrl).Text = ""; if(ctrl.GetType() == typeof(ComboBox)) ((ComboBox)ctrl).SelectedIndex = -1; if(ctrl.GetType() == typeof(CheckBox)) ((CheckBox)ctrl).Checked = false; if(ctrl.GetType() == typeof(Label)) ((Label)ctrl).Text = ""; if(ctrl.GetType() == typeof(DateTimePicker)) ((DateTimePicker)ctrl).Text = ""; if(ctrl.Controls.Count>0) ClearControlValues(ctrl); } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }





Reply With Quote