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

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

Related

Compare the values of two listboxes and add the non-equivalent values to a third listbox

My knowledge of programming is not too extensive but I have just finished my second year of higher education in computer engineering and have taken a few low level programming courses.
In Visual Basic, I am having trouble comparing the values of two ListBox controls and putting the values that aren't the same into another ListBox.
I need to compare the items of ListBox2 to the items of ListBox1 and if there are any items in ListBox2 that are not in ListBox1, add them to ListBox3. I do not need to find the items in ListBox1 that are not in ListBox2. I cannot use a loop to compare their values based on index because these lists are of names which will be constantly added to and removed from. I also cannot sort these ListBoxes.
There was an example for C# that I found here that used LINQ (I don't really know what that is) to compare the lists and then add the result to a TextBox control. However, I need to know how to add them to a ListBox and not a TextBox.
[EDIT] The example that I have tried is this:
Dim result As List(Of String) = (From s1 As String In Me.ListBox1.Items Where Not Me.ListBox2.Items.Contains(s1) Select s1).ToList()
Me.TextBox1.Text = String.Join(Environment.NewLine, result)
Dim one As ListBox = New ListBox()
Dim two As ListBox = New ListBox()
Dim three As ListBox = New ListBox()
Dim unique As Boolean = True
For i As Integer = 0 To one.Items.Count
For j As Integer = 0 To two.Items.Count
If (one.Text = two.Text) Then
unique = False
Else
two.SelectedIndex = j
End If
Next
If (unique) Then
three.Items.Add(one.Text)
Else
one.SelectedIndex = i
unique = True
End If
Next
I believe this may be what you're looking for. What it does is compares each value in Listbox one to all values in Listbox two and if a value is seen to be a duplicate the Boolean flag unique is switched to false and the item is not added to Listbox three.
I think i found the solution:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox3.Items.Clear()
Dim result As List(Of String) = (From s1 As String In Me.ListBox2.Items Where Not Me.ListBox1.Items.Contains(s1) Select s1).ToList()
For Each l In result
ListBox3.Items.Add(l)
Next
End Sub
or
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result As List(Of String) = (From s1 As String In Me.ListBox2.Items Where Not Me.ListBox1.Items.Contains(s1) Select s1).ToList()
For Each l In result
If Not ListBox3.Items.Contains(l) Then
ListBox3.Items.Add(l)
End If
Next
End Sub
Try which works best for you :)

How can I mirror checked items from one CheckedListBox to another?

I have 2 checklistbox controls and want the items in the second control to mirror the checked state of those in the first. For example:
Checklistbox1 = APPLE, MANGGO, BANANA, STRAWBERRY, GRAPE
Then i checked manggo and grape.
checklistbox2 = 0,1,0,0,1
How do I go about this?
This should accomplish what you want. Note that if you have a CheckedListBox2_SelectedIndexChanged event, you could get unexpected results, as this code will trigger it.
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim i As Integer
For i = 0 To CheckedListBox2.Items.Count - 1
CheckedListBox2.SetItemChecked(i, False)
Next
For Each i In CheckedListBox1.CheckedIndices
CheckedListBox2.SetItemChecked(i, True)
Next
End Sub
If you have a large list, this might be a bit more efficient, but you end up with the same result.
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim i As Integer
For i = 0 To CheckedListBox2.Items.Count - 1
CheckedListBox2.SetItemChecked(i, CheckedListBox1.GetItemCheckState(i))
Next
End Sub
Also you might want to have the checkonclick property of your listboxes set to true to save you having to click the item twice - and it yields more consitent results with both my code and the code from #josh , but if you need to do anything else when you're selecting an item, you might want it turned off

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

Delete line of structure array and update it in tab vb.net

So I have a listbox, filled with informations from a structure tab as follows :
Private Sub Modifier_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To frmConnecter.TabPolyFlix.Length - 1
ListBox1.Items.Add(frmConnecter.TabPolyFlix(i).strTitre)
Next
End Sub
And I want the user to choose to delete the TabPolyFlix(ListBox1.SelectedIndex) and it has to get updated in the original Tab and thus in the Listbox
P.S I tried this but it only updates it in the listbox, not in the original tab
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Temp As New List(Of ListViewItem)
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedIndices.Contains(i) = False Then
Temp.Add(ListBox1.Items(i))
End If
Next
ListBox1.Items.Clear()
For i As Integer = 0 To Temp.Count - 1
ListBox1.Items.Add(Temp(i))
Next i
End Sub
Arrays have a fixed size. Better use a List(Of ListViewItem) for TabPolyFlix. Then assign the List directly to the DataSource of the listbox:
'Fill the listbox
ListBox1.DisplayMember = "strTitre" 'You can also set this in the property grid.
ListBox1.DataSource = frmConnecter.TabPolyFlix
You can also override the ToString method of ListViewItem in order to display anything you want in the listbox and keep ListBox1.DisplayMember empty.
Now you can directly delete the item in the list:
frmConnecter.TabPolyFlix.Remove(ListBox1.SelectedItem)
or
frmConnecter.TabPolyFlix.RemoveAt(ListBox1.SelectedIndex)
or if you want to delete several items at once
For Each i As Integer In ListBox1.SelectedIndices.Cast(Of Integer)()
frmConnecter.TabPolyFlix.Remove(ListBox1.Items(i))
Next
and re-display the list:
ListBox1.DataSource = Nothing
ListBox1.DataSource = frmConnecter.TabPolyFlix
The moral of the story is: don't use controls (the ListBox in this case) as your primary data structure. Use it only for display and user interaction. Perform the logic (the so called business logic) on display-independent data structures and objects (so called business objects) and when finished, display the result.

Getting row number in a DataGridView

How do you get row number DataGridView cell? Specifically, if a user has selected a single cell, how can you get that row number? It needs to access a particular cell based on what the user has selected.
I know that the RemoveAt method can be used to remove at the Focus, but you cannot get the row number at focus apparently?
Thanks for the help!
You can simply use RowIndex on the current cell:
var row = dataGridView1.CurrentCell.RowIndex;
this one works fine .
Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If e.RowIndex >= 0 Then
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If
End Sub
It is nearly the same but you may also use this solution:
var row = dataGridView1.CurrentRow.Index
Another way if you need to track user interaction with a DataGridView:
In my case there is extra processing in some generic functions that use the Me.I_SelCol and Me.I_SelRow column and row coordinates, but I haven't shown that because it's not relevant to the OP.
Best regards,
Rob
Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged
If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub
' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the
' new cell as per user navigation using mouse or cursor keys. We just need to store the current
' co-ordinates for the currently selected cell.
Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y
Exit Sub
If you in event procedure of datagridview you will found "e As DataGridViewCellEventArgs" you can get row number with e.RowIndex and column number with e.ColumnIndex
Private Sub myDGV_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles myDGV.CellLeave
Dim myRow As Integer = e.RowIndex
End Sub
And if you in normal procedure you can get row number like this:
Private Sub btnGetRow_Click(sender As Object, e As EventArgs) Handles btnGetRow.Click
dim myRow1 as Integer = 0
dim myRow2 as Integer = 0
myRow1 = myDGV.CurrentCell.RowIndex
myRow2 = myDGV.CurrentRow.Index
End Sub