0

I've a number of Devexpress controls on my form under layoutcontrol1. I used the code below to iterate through each control and clear the existing data when users click on "Clear" button. However, it doesn't work on LookupEdit (set the EditValue to 0) and CheckedListBoxControl (uncheck all the selected items).

foreach (Control c in layoutControl1.Controls)
{
   if (c.GetType() == typeof(TextEdit) || c.GetType()==typeof(MemoEdit))
       c.Text = String.Empty;

   if (c.GetType() == typeof(LookUpEdit))
       c.EditValue = 0; //doesn't have EditValue property

   if (c.GetType() == typeof(CheckedListBoxControl))
       c.CheckedItems = CheckState.Unchecked; //doesn't have such property
}

Any suggestion?

aby
  • 810
  • 6
  • 21
  • 36
  • 1
    refer to this question: [how can we clear the all form controls on winform?][1] or [What is the best way to clear all controls on a form C#?][2] maybe are useful [1]: http://stackoverflow.com/questions/14620375/how-can-we-clear-the-all-form-controls-on-winform [2]: http://stackoverflow.com/questions/297526/what-is-the-best-way-to-clear-all-controls-on-a-form-c – Hamid Talebi Feb 10 '14 at 09:04

1 Answers1

1

Just try the following:

foreach(Control c in layoutControl1.Controls) {
    var edit = c as DevExpress.XtraEditors.BaseEdit; // base class for DX editors
    if(edit != null)
        edit.EditValue = null;
    var listBox = c as DevExpress.XtraEditors.CheckedListBoxControl;
    if(listBox != null) 
        listBox.UnCheckAll();
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Thank you for the response. It worked like a charm. However, it throws exception when I apply it to GridLookUpEdit. Any reason? – aby Feb 11 '14 at 13:58
  • @aby It is very strange... what the exact exception you've got? Anyway, you can contact DevExpress [directly](https://www.devexpress.com/Support/Center) to make a final decision – DmitryG Feb 12 '14 at 04:24
  • It just throws the exception "Sequence contains no elements" but there are data bind to the GridLookUpEdit – aby Feb 12 '14 at 08:23
  • and at times "Object reference not set to an instance of an object" – aby Feb 12 '14 at 08:32