using a loop to input arraylist value into checkbox, gives wrong value - vb.net

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

Related

Write individual listbox items into different text boxes and repeat until all text boxes are full

I'm programming in Visual Basic.
I have one form.
Form 1 contains:
nameTextBox
addNameButton
namesListBox
generateButton
week1TextBox
week2TextBox
week3TextBox
week4TextBox
The user needs to go to Form 1 and type a name in the text box, then add the name to the List Box. The user will add 4 names to the List Box. So, the ListBox will contain the names: Adam, Brenda and Carol.
When the generateButton is clicked, the 3 names have to be written to the text boxes in that order. So week1TextBox should contain "Adam", week2TextBox should contain "Brenda", etc... but once the last name (in this case "Carol") is written into the text box, the loop should start over. Ultimately, there may be up to 50 week text boxes (so week50TextBox). So the loop needs to repeat over and over.
As there is a lack of source code in your question, I'm really not sure exactly how the layout should look, I can only offer some advice/suggestions.
I would recommend creating your listbox control, input textbox, and button to add names to the listbox. In addition to these, though, also add a scrollable panel. (Not sure what the exact term for that control is in VB.net; it's been a long time since I've worked with that language.) Because it sounds like there might be a variable number of items on the panel, when the user goes to generate the list of names, I would use the following rough pseudocode:
Dim OutputTexts As New ArrayList ' This is only here if you want to work with these textboxes later
Private Sub CreateOutput() Handles btnGenerate.Click
pOutputPanel.Controls.Clear()
OutputTexts.Clear()
Dim NextX As Integer = 0 ' Pretty much unnecessary value, but included in case you want to mess with this
Dim NextY As Integer = 0
For i As Integer = 0 To Convert.ToInt32(txtWeekCount.Text)
Dim txtName As New TextBox
txtName.Text = lbNameList.Item(i Mod lbNameList.Items.Count)
txtName.Location = new Point(NextX, NextY) ' Play with this as necessary
NextY += 50 ' Play with this as necessary
OutputTexts.Add(txtName)
pOutputPanel.Controls.Add(txtName)
Next
End Sub
Again, this is very much pseudocode, so I would not encourage copying and pasting, but give it a read, make sure you understand all of it, and then try implementing something similar. There might be an easier way to do it, but I have not programmed in VB.NET in probably over 2 years (at least). Nonetheless, the most important thing in here is the following line: lbNameList.Item(i Mod lbNameList.Items.Count). By Mod-ing your indexing variable, you will be accessing items sequentially, and then repeating from the start of the ListBox items collection once i is out of range.
I would also encourage you to dynamically generate your TextBox controls as needed rather than manually adding in 50 or more TextBox controls.

Visual Basic 2008

My previous question is not clear. So I'm going to repeat it.
The label on the right part are the transmuted grade and the textbox is the raw score percentage.
How do I do a short way for this codes (where I will not repeat it again in other textboxes)
Dim grade as Integer
This codes will be in the button so when pressed, the raw percentage will be transformed to transmuted grade.
If MathTextbox.Text = "100" then MathLabel.Text= "1"
Codes like this, my prob is how do I avoid repeating it in each texboxes.
If the problem is that you want one button to perform the same action to multiple controls then you can do something like this:
For Each ctrl1 As Control In Me.Controls
If TypeOf (ctrl1) Is TextBox Then
For Each ctrl2 As Control In Me.Controls
If TypeOf (ctrl2) Is Label AndAlso _
Microsoft.VisualBasic.Strings.Left(ctrl1.Name, 4) _
= Microsoft.VisualBasic.Strings.Left(ctrl2.Name, 4) Then
ctrl2.Text = ctrl1.Text
Exit For
End If
Next
End If
Next
Note that there are much better ways for identifying which label pairs with which textbox (I prefer the Tag property myself) and that if you're going to perform a numerical operation on a string (such as a textbox's Text property) you should validate it first, e.g. with IsNumeric.

Can't clear a DataGridView combobox without 100s of errors

I'm using VB.NET to make a stand-alone executable containing an unbounded DataGridView.
One of its columns is a combobox. I add rows to the combobox with some simple code
like this:
For r As Int16 = 0 To eachLine.GetUpperBound(0)
Dim dgvcbc As DataGridViewComboBoxColumn = grd.Columns(col)
' Errors: dgvcbc.Items.Clear()
dgvcbc.Items.Add(eachLine(r))
Next r
Works great the first time, but when I try to clear it, in order to add some different
items in the combobox, I get this error 100s of times:
> DataGridViewComboBoxCell value is not valid
Any ideas on how to fix this and why it occurs?
The issue is that, if you clear the Items in that column, every cell in that column becomes invalid. Every value in that column has to match one of the items in the drop-down list and, if there's no items, no value can be valid. It doesn't make sense to clear the drop-down list. You must leave at least the items that match the values in the cells of that column.

vb.net listbox- Remove ALL items that DON'T contain specific text

I'm working with a listbox in vb.net and am trying to remove all items from the listbox that don't contain specific text at the click of a button. Here's my code:
Dim i As Integer
For i = 0 To ListBoxPrePublish.Items.Count - 1
If InStr(ListBoxPrePublish.Items(i), "-8-") > 0 = False Then
ListBoxPrePublish.Items.RemoveAt(i)
Exit For
End If
Next
This only removes 1 item at a time though. How can I tweak this to remove all items that don't contain "-8-" at once?
EDIT: in case anyone asks, the listbox items list is growing rather large so I'm adding a sort feature so users can widdle down their options if they want to. That's why I'm not filtering anything before adding to the listbox
Here is the complete code for backward loop I mentioned in the comments - it should work:
For i as Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
If Not ListBoxPrePublish.Items(i).Contains("-8-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
No, I do not know of any RemoveRange type functionality. And be advised that you will need to loop through the listbox Items collection backwards as you remove items or you will get index exceptions, because once you remove something it will mess up the index values of all the remaining items in the iterator.

Trying To Add Element To Array

Using 3.5 VB1 framework.net
I'm trying to add an element to an array
i would like to clear the listbox and display the array contents in listbox.
then add another button, then add an element to the array, from the textbox.
Ive created this painstakingly for the past 6 hours
Call clearout() ''===== Clears listbox
Dim MyNumbers(4) As Integer
Dim i As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5
For i = 0 To 4
ListBox1.Items.Add(MyNumbers(i))
Next i
That part works great!
It plops it right into listbox and deletes any previous entries into listbox
What Ive studied so far after all these hours to make the next button is to use UBound function to find the highest element then add one to it and ReDim it with that value
Problem is I'm not sure how to write that correctly under the second button
Any help?
I'm not quite clear what you are trying to achieve, but this is my interpretation:
Button1: clears the listboxpopulates the array with numbers 1 to 5populates the listbox with these numbers
Button 2: adds a number to the array from a textboxadds this new number from the array to the listboxORsimply increments the values in the array by 1 and append this new value to the listbox?
In any case, you need to share the array between the buttons so it must be declared with module level scope. That is, outside the button click routines.Also, in order to preserve the values already in the array, you need to use ReDim Preserve MyNumbers(newUBound)Hopefully the above tips will help!PS. Does your clearout() method simply call ListBox1.Items.Clear() ? If it does, it's best just to call this inline rather than create a new method to do this.