Trying To Add Element To Array - vb.net

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.

Related

Text from listbox into array, text not making it into my array unless I click an item in the listbox first

I am trying to capture the text all of the items in a listbox into an array. When I click the button to do this, it does correctly count and add the right number of items into the array but the text of the values in the listbox get carried over as nothing (""). This happens UNLESS I click on just a single item in the listbox first. Then all the items from the listbox make it into the array with their text values.
ReDim Components(lbUse.ListCount - 1) As String
For i = 0 To lbUse.ListCount - 1
lbUse.Selected(i) = True
Components(i) = lbUse.Text
Next
Don't use the Text property. Use List. Try it like this instead:
ReDim Components(lbUse.ListCount - 1) As String
For i = 0 To lbUse.ListCount - 1
Components(i) = lbUse.List(i)
Next
Also, depending on why you need this array, you might not, as the data you are accessing is already in a property array as you can see. You could just reference that instead of copying it to your own array.
The "other bug" you were experiencing was most likely coming from the use of setting the ListIndex using Selection unnecessarily.

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 add a blank item at the start of a list in a combobox?

I am using this loop to generate a list of months
i = 1
Do While i <= 12
ListedMonths.Add(New ListedMonth(i, MonthName(i)))
i = i + 1
Loop
Return ListedMonths
but i also want to show a blank option at the top before January.
i have tried adding ListedMonths.Add(New ListedMonth("","Select")) before the while loop
but when running my vb.net application, i get nothing listed in the combo box. when i remove the above line, it lists Jan-Dec fine
You create a ListedMonth item by passing an Integer and a String to its constructor. So I think the first Parameter expects an Integer, but you pass an empty String in your attempt. That's why you should try the following statement to add your Select item:
ListedMonths.Add(New ListedMonth(0, "Select"))
If it does not work: it would be helpfull to see your ListedMonth type and know the technology you use (WPF, WinForms, ...).

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.

Passing value from array to label controls in visual basic

I'm working on an assignment that takes in 20 answers from user using 20 textboxes, grade the answers and output the correct answer choices to 20 label boxes. The input answers and the answer keys are store in array. What's the best method to pass values from the answer key array to all appropriate labels in the form? Right now I'm using this method:
For i As Integer = 21 To intLblNum
For intCount = 0 To (strAnswers.Length() - 1)
gradeResult.Controls("Label" & i.ToString).Text = strAnswers(intCount)
Next
Next
But as the result, all of my label boxes contain only the last element from the answer key array. What'd I do wrong?
One problem that I see is that you loop through all 20 answers for each label, you need to have one loop and offset the value to account for your Label names. Something like this should work for you.
Dim maxEntrys As Integer = 19
For i = 0 To maxEntrys
gradeResult.Controls("Label" & (21 + i)).Text = strAnswers(i)
Next
Assuming you are in WinForms, each controls has a Tag property of type object that can be used to store custom data. It is not considered good design to use control naming as a means of associating data with controls.
Consider using a class to hold your question/answer data, make an array or preferably collection of them and set each control's Tag property to the appropriate instance.