is it possible to loop through all comboboxes with specific name. For example I have 25 comboboxes in a groupbox i need to loop through 20 of them (each of this 20 have name special_combo_1,special_combo_2 and etc. but another 5 have another names so i need to leave as they are)and change their width at once or change the text or anything else.
You can use Control.Controls to get all the controls the GroupBox contains.
Then, you should cast each Control object to the ComboBox type by using TryCast(Object, Object).
You can check the prefix with String.StartsWith(String).
For Each Item As Control In GroupBox1.Controls
Dim ComboBoxItem As ComboBox = TryCast(Item, ComboBox)
If ComboBoxItem IsNot Nothing Then
If ComboBoxItem.Name.StartsWith("special_combo_") Then
' Code here
End If
End If
Next
Use the OfType to make a collection of comboboxes from the control collection. No TryCast needed so increase in preformance. Then filter them with the Where clause to drill it down even further. Now you iterate only a small collection of controls.
Dim spComboboxes =
GroupBox1.Controls.OfType(Of Combobox)().
Where(Function(cb) cb.Name.StartsWith("special_combo_")).ToList()
Iterate the comboboxes:
For Each cb In spComboboxes
'do something
Next
Related
I have 3 ListViews in a UserForm and also a few other items. Currently when I expand the UserForm, the ListViews and other items expand in relation to the increase in size of the UserForm.
What I'm after doing is also expanding the size of the Columns within the ListViews by cycling through all Controls on the UserForm and checking if it is a ListView, then cycling through all the columns and extending as required.
This is where I am currently at...
For Each Ctrl As Control In Me.Controls
If (TypeOf Ctrl Is ListView) Then
' This is where I'm not sure what to do!
' I want to loop through this Ctrl and view its columns
End If
Next
Anyone any ideas?
If Plutonix' suggestion of enabled auto-resizing columns doesn't work, then read on...
To access the control's properties, you'll need to cast it to ListView first (so far you've only checked to see if it is a ListView. Then, you can loop through the rows and columns:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is ListView Then
Dim currentListView As ListView = DirectCast(Ctrl, ListView)
' Loop over the rows (items) in the view
For Each item As ListViewItem In currentListView.Items
Next
End If
Next
You might also end up needing a recursive control search -- your current loop will only look for controls that are a direct child of Me.
Another idea: simply return controls of the correct type in the outer for-loop:
For Each listViewControl As Control In Me.Controls.OfType(Of ListView)()
For Each item As ListViewItem In listViewControl.Items
Next
Next
I am working on an assignment that requires a user to answer 20 questions( multiple choice ). I am using the DropDownList property so the user cannot input anything other than A, B, C, or D.
Basically, I have 20 comboboxes and I have a button that clears them, but the code I should obviously be a loop, but I am not sure how to do that.
As of now, my code looks like this:
cboQuestion1.Items.Clear()
cboQuestion2.Items.Clear()
...
cboQuestion20.Items.Clear()
If anyone could shed some light on this, I will be grateful.
All controls reside in the form's Controls collection, so one way would be to iterate that (assumes these CBOs are the only ones you wish to clear):
For Each cbo As ComboBox In Controls.OfType(Of ComboBox)
cbo.Items.Clear
Next
Another way is to store the names of the target controls in a List(of String). Think of this as a shopping list of the controls you wish to track or treat in some special way:
Private myCBONamesList As List(of String)
'...
myCBONamesList.Add("cboQuestion1")
' etc
' add many/all at once:
myCBONamesList.Addrange(New String(){"cboQuestion1", "cboQuestion2" ...etc})
The New String() creates a temp array containing the literal values listed (in {}) and the whole thing is passed to your List to populate it. To use it:
For Each s As String in myCBONamesList
Controls(s).Items.Clear
Next
This method allows you to target certain CBOs and leave others alone.
It may or may not be the best way, but you could add all of your combo boxes to a List and then iterate over the list to clear them all.
Just iterate over the Form's Controls collection.
Here is an example of iterating over the Forms Controls collection with filtering to make sure you don't accidentally clear a non-question ComboBox:
For Each cbo As ComboBox In Me.Controls.OfType(Of ComboBox)
If cbo.Name Like "cboQuestion*" Then
cbo.Items.Clear()
End If
Next
Edit: Or if you're into one-lining things:
For Each cbo As ComboBox In Me.Controls.OfType(Of ComboBox).Where(Function(x) x.Name Like "cboQuestion*")
cbo.Items.Clear()
Next
I have 8 comboboxes on my form that will all hold the same values - Yes and No.
Is there a quicker way than having to do combox1.items.add("Yes") etc?
I have the following but I cant seem to find anything to do with adding the items.
Dim cmb As Control
For Each cmb In Panel1.Controls
If TypeOf cmb Is ComboBox Then
'cmb. isnt beinging anything up for adding items?
End If
Next
Cheers
You can use Enumerable.OfType:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.Items.Add("Yes")
Next
I would create a DataSource containing {Yes,No} values, for example as a List and then just do this:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.DataSource = myYesNoDataSource
Next
Later if you need to accept Y and N in place of Yes and No, you can convert to Dictionary and set ValueMember and DisplayMember accordingly. Plus your list of available values is only initialized once. So your solution becomes flexible.
This Code will be useful, if you didnt use panel.
If you have 5 comboboxes give count 1 to 5 and name of those comboboxes like ComboBox1, ComboBox2, comboBox3 etc.
For count = 1 To 5
Dim combobox = DirectCast(Me.Controls("ComboBox" & n & ""), ComboBox)
combobox.Items.Add("Ok")
Next
Hope this code also helps you.
in VB.NET i have 2 custom controls, one is a TextBox and second one is a ComboBox.
These have custom values like Bool _IsHidden and are added on runtime to a form.
Now, at some point in the code I want to check if the _IsHidden is set to True or False and display that information. Since the user can edit this values when creating the control these are not set on creation.
So what I tried is:
(all of this is on MDI Forms)
For Each frm as CustomForm in Main.MdiChildren
If frm.MyName = calledBy Then 'this part is just to know which form called the form to create the object
For Each cntrl as CustomTextBox in frm.Controls
'DO Something
Next
End if
Next
Now.. if the first control is a custom ComboBox it thorws an error since it sees that it does not match the custom TextBox control..
how do i get around this? By my understanding it should just go through all of the controls on the said form and just check those who match CustomTextBox control ?
Thank you
For Each x As T In collection does not filter your collection items to those of type T. It tries to convert every item in collection to T and throws an exception if that fails.
Thus, you have the following options:
Do the check yourself, for example, using the code provided by RB.
Alternatively, you could filter your list first, and then loop through the items. Here, LINQ can help:
For Each cntrl In frm.Controls.OfType(Of CustomTextBox)()
... ' Do this for all CustomTextBoxes
Next
For Each cntrl In frm.Controls.OfType(Of CustomComboBox)()
... ' Do this for all CustomComboBoxes
Next
You don't need the As CustomTextBox clause here, since frm.Controls.OfType(Of CustomTextBox)() returns an IEnumerable(Of CustomTextBox), so For Each can infer by itself that cntrl must be of type CustomTextBox.
By my understanding it should just go through all of the controls on
the said form and just check those who match CustomTextBox control ?
That's not correct I'm afraid. You'll need to implement that check yourself, e.g:
For Each cntrl as object in frm.Controls
If TypeOf cntrl Is CustomTextBox Then
With CType(cntrl, CustomTextBox)
.DoSomethingWithControl()
.DoSomethingElseWithControl()
End With
End If
Next
i have used many of the controls in vb.net forms, including a textbox and combobox, but I want to clear all my text boxes at once, while not typing textbox1.clear() for each one.
Is there any other way to clear all my textboxes?
If I understand you question right, you should be able to loop through all the controls on your form and check to see what Control type they are. Based on their type, either set the textbox Text property to String.Empty, or your ComboBox to the index of a blank ListItem (presumably item zero).
Something like:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
Else
' do something similar for your ComboBox
End If
Next
You can parse through each control on your form and test what type of control that is and handle each type separately, but it is easier to simply set each control manually.
Consider writing a ClearForm routine that does all of this, that way all you have to do is call the method.