multidimension Arrays and Listboxes (VB.Net) - vb.net

I'm new to VB.Net and feel a bit confused about something.
I want to have two list boxes, in each ones I already have items. In my first list box I have 4 items, in my second List box I have 5 items. I have also added a text box, for the value I want to be stored in the array.
For example: if I select the first value of the first text box, and the second value of the second text box and type "5" in the text box, 5 will be stored in (0,1) of the array.
I then want all the values for each item of my first list box to be displayed in a label, same for the second items, third item, and fourth item. I suppose I would need a loop for this.
I know how to create an array, and how to store values in an array, but I can't seem to figure out how to get it to work using list boxes and a text box.

I've created a form with the following controls:
ComboBox1
ComboBox2
Button1
TextBox1
I've added code to the Form_Load and Button1_Click events and created a single ComboBox_SelectedIndexChanged event handler to deal with both combo boxes index changes.
Public Class Form1
Private _array(,) As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReDim _array(0 To ComboBox1.Items.Count, 0 To ComboBox2.Items.Count)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex)
Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex)
Debug.Print(String.Format("Set ({0},{1}) to {2}", c1, c2, TextBox1.Text))
_array(c1, c2) = TextBox1.Text
End Sub
Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex)
Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex)
Debug.Print(String.Format("Get ({0},{1}) to {2}", c1, c2, TextBox1.Text))
TextBox1.Text = _array(c1, c2)
End Sub
End Class
What I'm demonstrating is that:
1. the array is resized when the form is loaded to match the number of elements in your comboboxes.
2. the data is loaded into the array on an event (in this case the button click event).
3. Retrieving the data again as either of the combo boxes changes.
Hope that helps.

Related

Multi select listbox's last clicked item to textbox

I want to show the last clicked item on my ListBox that has multi-select. It only shows the first item on the list selected when I use the following:
Textbox1.text = listbox1.text
I think your only option would be to store a list of the selected indices and add/remove to/from it whenever the selection changes.
Something like this should do the job (assuming WinForms):
Private selectedIndices As New List(Of Integer)
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
' Add the newly selected items.
selectedIndices.AddRange(
ListBox1.SelectedIndices.Cast(Of Integer).
Where(Function(i) Not selectedIndices.Contains(i)))
' Remove the unselected items.
selectedIndices.RemoveAll(Function(i) Not ListBox1.SelectedIndices.Contains(i))
' Update the TextBox text. You can move this code to a different method
' if you want to trigger it using a button or something.
If selectedIndices.Count = 0 Then
TextBox1.Text = String.Empty
Else
Dim lastIndex As Integer = selectedIndices.Last()
TextBox1.Text = ListBox1.GetItemText(ListBox1.Items(lastIndex))
End If
End Sub
See it in action:

Copy values of cells from a DataGridView column to an ArrayList

I am using a DataGridView with an ArrayList. When a user clicks a button, I want the values of the first column (there will be 4 columns total, each having its own ArrayList) of the DataGridView to be written to an ArrayList. I am using an ArrayList because the number of rows in the Datagridview can vary, so the array size cannot be static. The code that I have written, actually searched and got help online with is "almost" working. It will write the values of the first column to the arraylist, but I have to click on the button twice. It will only update the ArrayList on the second click of my button. What needs to change with my code? Thanks from a newbie!
Private Sub btnPrintArray_Click(sender As Object, e As EventArgs) Handles btnPrintArray.Click
Dim message = String.Join(Environment.NewLine, lftMtrAccelRates.ToArray())
lftMtrAccelRates.Clear() 'clears ArrayList
rchTxtBox.Clear() 'Clears rich text box that has array element values in it
For Each d In LftMtr_Data_Grid.Rows.Cast(Of DataGridViewRow)()
Dim num As Integer
If Int32.TryParse(d.Cells(0).Value, num) Then
lftMtrAccelRates.Add(num)
End If
Next
rchTxtBox.Text = message
End Sub`
One possibility:
Private values As New List(Of Integer)
Private Sub btnPrintArray_Click(sender As Object, e As EventArgs) Handles btnPrintArray.Click
values.Clear()
Dim num As Integer
For Each d As DataGridViewRow In LftMtr_Data_Grid.Rows
If Int32.TryParse(d.Cells(0).Value, num) Then
values.Add(num)
End If
Next
rchTxtBox.Lines = values.ConvertAll(Function(x) x.ToString).ToArray
End Sub

vb: How to select multiple items in a Listbox at once?

I have a Listbox with a list of numbers from 1 to 10.
Now I want to program to select those numbers greater than 5. But I also want to trigger the SelectedIndexChanged event only once.
I know I can add multiple items into Listbox at once by using addrange() method.
But it seems there isn't a similar solution for select multiple item at once ?
How can i do this ?
youre question is a litle unclear but ...
First you need to set your Listbox SelectionMode to MultySimple.
Then you use ListBox1.SelectedItems.Count < 2 So it trigger the SelectedIndexChanged event only once at the start of the selecting.
Of course you can edit the code to fit your needs and let it trigger whenever you want.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count < 2 Then
MsgBox("one")
End If
End Sub
And to select everything above 5 you need to make a list of integers.
Then use a for each loop to get the items in the list of integers with the values you want.
Then use the list of integers in a for loop to select the items in the listbox.
Dim l As New List(Of Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Item As Integer In ListBox1.Items
If Item > 5 Then
l.Add(ListBox1.FindString(Item))
End If
Next
For SetItem As Integer = 0 To l.Count - 1
For i = 0 To ListBox1.Items.Count - 1
If i = l.Item(SetItem) Then
ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
End Sub

Visual Basic - Grouped radio buttons

Basically what i'm wanting to work is for the text of the selected radio button to be inserted into a ListView that i already have.
Here is my code (Listview):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AddButton.Click
Dim Col1 As String = ComboBox1.Text
Dim Col2 As String =
Dim Col3 As String = ComboBox3.Text
Dim order As New ListViewItem
order.Text = Col1 'Adds to First column
order.SubItems.Add(Col2) 'Adds to second column
order.SubItems.Add(Col3) ' Adds to third column
ListView1.Items.Add(order)
End Sub
If i put RadioButton1.Text In DIM Col2 and select it when it runs then it displays it but i would like it so that it finds out which radio button is selected and displays the correct text. I Have four radio buttons all in a group called GroupBox1 and each radio button is RadioButton1, 2, 3 etc
The Combo boxes are getting used as the same but i need some sort of radio button in my program. Any help is appreciated.
With two RadioButtons inside one GroupBox, and one TextBox:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
If CType(sender, RadioButton).Checked Then
Dim rButton As RadioButton =
GroupBox1.Controls.
OfType(Of RadioButton)().
Where(Function(r) r.Checked = True).
FirstOrDefault()
Me.TextBox1.Text = CType(sender, RadioButton).Text
End If
End Sub
When a RadioButton is clicked, the TextBox text gets updated. I don't understand how you want to insert into the ListView, so just take the text and put it where you need it.

[VB.NET]How to add a string item, returned from another form, to a list box at a specific spot in place of another?

The idea is that there's a list-box with items, and say you want to modify an item in the middle of the list. You select that item and click "Modify" button and a new form appears with the previously selected item data from first form ready to be modified in a text-box. After modifying and clicking Ok the second form suppose to return that modified string to the first form and insert the modified string into the same spot instead of the originally selected item, so it looks like it was edited to the user.
Edit:
Translated the pseudo code to actual VB.NET code to refresh my own memory :D
string = InputBox("Enter text")
// Do whatever you want with the string
x = listBox.SelectedIndex
listBox.Items(x) = string
You can try Content in place of Text too.
Make sure that the form that pops up is Modal. Here is a simple example of what you can do. (This assumes your listbox items are strings and is an example for editing up to three listbox items only. If the list is going to be much larger you will want to pursue a different architecture.)
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
TextBox1.Text = Form1.ListBox1.Items(i)
Case 1
TextBox2.Text = Form1.ListBox1.Items(i)
Case 3
TextBox3.Text = Form1.ListBox1.Items(i)
End Select
intTextboxCounter += 1
Next
End Sub
When this loads it will spin through the selected list items and put its value in a textbox. To update the values...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
Form1.ListBox1.Items(i) = TextBox1.Text
Case 1
Form1.ListBox1.Items(i) = TextBox2.Text
Case 2
Form1.ListBox1.Items(i) = TextBox3.Text
End Select
Next
End Sub