PC Review


Reply
Thread Tools Rate Thread

How to clear comboBoxes

 
 
Keith Smith
Guest
Posts: n/a
 
      9th Feb 2005
Is there an easier way to clear all of the comboBoxes on my form? I am
currently using this code...

autoCompleteComboBox1.Text="";
autoCompleteComboBox1.SelectedText="";

autoCompleteComboBox2.Text="";
autoCompleteComboBox2.SelectedText="";

autoCompleteComboBox3.Text="";
autoCompleteComboBox3.SelectedText="";

autoCompleteComboBox4.Text="";
autoCompleteComboBox4.SelectedText="";

autoCompleteComboBox5.Text="";
autoCompleteComboBox5.SelectedText="";

autoCompleteComboBox6.Text="";
autoCompleteComboBox6.SelectedText="";


.....But it would be nice if I could do something like this:

AllControls.Clear;


 
Reply With Quote
 
 
 
 
Chris, Master of All Things Insignificant
Guest
Posts: n/a
 
      9th Feb 2005
Sorry, this is in VB code and I don't know the for each equivalent off the
top of my head.

For Each ctl In Form.Controls
If TypeOf ctl Is ComboBox Then
ct1.Text = ""
ct1.SelectedText = ""
End If
Next

"Keith Smith" <(E-Mail Removed)> wrote in message
news:eyrOd.22893$uc.16994@trnddc09...
> Is there an easier way to clear all of the comboBoxes on my form? I am
> currently using this code...
>
> autoCompleteComboBox1.Text="";
> autoCompleteComboBox1.SelectedText="";
>
> autoCompleteComboBox2.Text="";
> autoCompleteComboBox2.SelectedText="";
>
> autoCompleteComboBox3.Text="";
> autoCompleteComboBox3.SelectedText="";
>
> autoCompleteComboBox4.Text="";
> autoCompleteComboBox4.SelectedText="";
>
> autoCompleteComboBox5.Text="";
> autoCompleteComboBox5.SelectedText="";
>
> autoCompleteComboBox6.Text="";
> autoCompleteComboBox6.SelectedText="";
>
>
> ....But it would be nice if I could do something like this:
>
> AllControls.Clear;
>



 
Reply With Quote
 
Keith Smith
Guest
Posts: n/a
 
      9th Feb 2005
[In VB:]
> For Each ctl In Form.Controls
> If TypeOf ctl Is ComboBox Then
> ct1.Text = ""
> ct1.SelectedText = ""
> End If
> Next


I have tried but can't seem to find the proper C# code. The closest I can
come up with is...

foreach(Control x in Form1)
{
}

Which obviously is not complete. Any ideas?


 
Reply With Quote
 
Paul E Collins
Guest
Posts: n/a
 
      9th Feb 2005
"Keith Smith" <(E-Mail Removed)> wrote:

> foreach(Control x in Form1)


How about this?

foreach (Control c in Form1.Controls)
{
if (c is ComboBox) { c.Clear(); }
}

(The Clear method actually removes all the items from the list. You
might just want to clear the Text property. Obviously, you can do what
you like with c inside that block.)

P.


 
Reply With Quote
 
Paul E Collins
Guest
Posts: n/a
 
      9th Feb 2005
Uh, so I need to check my code before I post it!

foreach (Control c in this.Controls)
{
if (c is ComboBox)
{
((ComboBox) c).Items.Clear(); // or .Text = "", or whatever
}
}

P.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a Clear button to clear unprotected cells Jcraig713 Microsoft Excel Programming 2 26th Nov 2007 03:55 PM
clear the clear the web page email attachment lines =?Utf-8?B?TUNyaWRlcg==?= Microsoft Excel Misc 0 11th Nov 2007 10:05 PM
Clear Comboboxes & Textboxes MBlake Microsoft Excel New Users 3 2nd May 2005 10:29 PM
Cannot clear Address Bar -even using Tools /options/clear history Hennie Windows XP Internet Explorer 1 24th Sep 2003 06:46 AM
no tab to clear temporary internet files, clear history Ron Hein Windows XP Internet Explorer 1 9th Sep 2003 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:29 PM.