Populate multiple comboboxes with the same values - vb.net

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.

Related

using a loop to input arraylist value into checkbox, gives wrong value

I have a form I would like to fill out with the following code. The purpose is to fill out the CheckBoxes, which are placed inside panels, and then placed in some TabPages. This code worked well to grab the value of the CheckBoxes, but for some reason it reads my ArrayList wrong. For example, if the ArrayList is filled with "1, 1, 0, 0, 0..." it will read every row as "1" and set the CheckBoxes accordingly.
I also tried placing an integer to see if it repeated the process multiple times (the ArrayList contains 16 rows) and the integer turned out to be several times the 16 rows. I did try to restrain the loop with an if sentence, and even though it stopped after a given number, it still produces the wrong answers.
I've come to a stop, and can't figure out why this code won't do the trick. Help would be greatly appreciated.
I should probably mention that 'tabell' is the ArrayList which I try to pull the data out of.
(Also, if this has been asked before, I am sincerely sorry for repeating the question..)
For Each rad In tabell
For Each tb In TabControl1.Controls.OfType(Of TabPage)()
For Each pnl In tb.Controls.OfType(Of Panel)().OrderBy(Function(c) c.TabIndex)
For Each cb In pnl.Controls.OfType(Of CheckBox)()
If rad = 1 Then
cb.Checked = True
End If
Next
Next
Next
Next
You are looping through all of the checkboxes for each value in tabel1, and since you never uncheck the boxes, the first 1 value will check all the boxes and that is how they will stay.
I am guessing that you want to use the corresponding value from tabel1 based on the order that the checkboxes are found (which I think does not necessarily have to match the order that they appear on the screen, so you may have to sort the checkboxes too):
Dim idx = 0
For Each tb In TabControl1.Controls.OfType(Of TabPage)()
For Each pnl In tb.Controls.OfType(Of Panel)().OrderBy(Function(c) c.TabIndex)
For Each cb In pnl.Controls.OfType(Of CheckBox)()
cb.Checked = tabel1(idx) = 1
idx += 1
Next
Next
Next

How to set the items of a ComboBox into another one

I'm populating a VBA ComboBox with some data from a Workshet. As it is populated depending on the choice of the user, sometimes some levels are should not be populated, because they do not have data. When such a thing happens, I want to populate the items of a ComboBox with the same of another. How can a do that?
I think if somebody teaches me how to loop through the items of a ComboBox, I will the able to do what I want.
Here is how you loop through a combobox.
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
MsgBox Me.ComboBox1.List(intComboItem)
Next

loop through all comboboxes with specific name

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

Clearing combobox dropdownlists from application

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

Date Range to save database using Listview

Any one help me,
I have to DateTimePicker i need loop condition fill listview two DateTimePicker. and save them database using Selected Checkbox (Listview checkbox) and one textbox.
http://i.stack.imgur.com/YTsQW.jpg
Thanks
Try This Logic...
For count = 0 To DataGridView1.RowCount - 1
If DataGridView1.Rows(count).Cells(0).Value = True Then
'SQL INSERT COMMAND HERE
End If
Next
Since it is a ListView, you need to iterate the CheckedItems collection:
For Each lvi As ListViewItem in myLv.CheckedItems
' SQL operation using lvi.SubItems for the values
Next
This will work on all the items checked (as per the question) not just the one singled out in the image. you could also iterate CheckedIndices as an index into Items().