listview condition if selected index - vb.net

I have a view list with some items
movies
music
Code:
when I select item 0 it being movies, let me add a new item called The Matrix Resurrections.
For I As Integer = 0 To ListView1.Items.Count - 1
If ListView1.Items(I).Index = 0 Then
ListView1.Items.Add("The Matrix Resurrections")
End if
unfortunately, it doesn't work like a regular listbox.

The ListView have two option to manage the selection:
The SelectedIndices property: gets the indexes of the selected items in the control
The SelectedItems property: gets the items that are selected in the control
Now, we're talking of items and not item because, by default, you can select more than one item in yout listbox. If you want to disable the multi-selection, use this code:
ListView1.MultiSelect = False
Now in your Sub (that handles click or selectedIndexChanged event), you can use one of the two properties above. For example:
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 AndAlso ListView1.SelectedIndices(0) = 0 Then
ListView1.Items.Add("The Matrix Resurrections")
End If
End Sub
Note that using ListView1.SelectedItems
If no items are currently selected, an empty
ListView.SelectedIndexCollection is returned
So checking ListView1.SelectedItems.Count > 0 prevents errors.
Output

Related

How to get item text from CheckedListBox to ListBox without repeating the before text? VB.net

I have a CheckedListBox of 10 names A to J (it's a string but here I'm just using abc), and I want to make it so that every time a user check something, the text will be shown in the listbox simultaneously. I have tried this:
For i = 0 To chklistbxDrugAvailableList.Items.Count - 1
Dim drugs As String = CType(chklistbxDrugAvailableList.Items(i), String)
If chklistbxDrugAvailableList.GetItemChecked(i) Then
listbxYourOrder.Items.Add(drugs)
End If
'drugs = nothing
Next
But when I check A,B,C the text in the Listbox got repeated like so:
A
A
B
A
B
C
I don't know why this is happening. I didn't declare drugs as an array. Even when I used the drugs = nothing, it still give repeated values.
What should I do? Sorry if this is a noob question.
Here is how I would do the whole thing:
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
'Clear the current ListBox contents.
ListBox1.Items.Clear()
'Get the indexes of the currently checked items.
Dim checkedIndices = CheckedListBox1.CheckedIndices.Cast(Of Integer)()
'Add the current index to the list if the item is being checked, otherwise remove it.
checkedIndices = If(e.NewValue = CheckState.Checked,
checkedIndices.Append(e.Index),
checkedIndices.Except({e.Index}))
'Get the checked items in order and add them to the ListBox.
ListBox1.Items.AddRange(checkedIndices.OrderBy(Function(n) n).
Select(Function(i) CheckedListBox1.Items(i)).
ToArray())
End Sub
or like this:
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
'Get the indexes of the currently checked items.
Dim checkedIndices = CheckedListBox1.CheckedIndices.Cast(Of Integer)()
'Add the current index to the list if the item is being checked, otherwise remove it.
checkedIndices = If(e.NewValue = CheckState.Checked,
checkedIndices.Append(e.Index),
checkedIndices.Except({e.Index}))
'Get the checked items in order and add them to the ListBox.
ListBox1.DataSource = checkedIndices.OrderBy(Function(n) n).
Select(Function(i) CheckedListBox1.Items(i)).
ToArray()
End Sub
The difference is that, because the second option uses data-binding, the first item in the ListBox will be selected by default.
The reason that it needs to be a bit verbose is that the ItemCheck event is raised before the item is checked or unchecked. For that reason, we need to get the currently checked items first and then either add or remove the current item, depending on whether the current item is being checked or unchecked.
EDIT:
Here's an option that doesn't require clearing the ListBox:
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim item = CheckedListBox1.Items(e.Index)
If e.NewValue = CheckState.Checked Then
ListBox1.Items.Add(item)
Else
ListBox1.Items.Remove(item)
End If
End Sub
The issue with that is that the items will appear in the ListBox in the order you checked them, rather than the order they appear in the CheckedListBox. The other options keep the items in the same order in both lists.
If I understand your question I would this:
lstbxYourOrder.Items.Clear()
For Each l As String In chklistbxDrugAvailableList.CheckedItems
listbxYourOrder.Items.Add(l)
Next

Disable listview item in vb.net

I am trying to disable item (row) in my list view but its seem there no option like .enable = false and I tried to find anything to get my item to by disable but visible. Is there anything like that? If the user is allowed to select it then the item is enabled else it's visible but not enabled.
I have a table in the database that admin will fill it in which the user can view the window or not, so I want the user to able to see it and if not allowed to view it then its disable.
This only works if MultiSelect is set to False and the .Tag property is set for every item. (Yes or No).
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If Not ListView1.SelectedItems.Count = 0 Then
Dim item As ListViewItem = ListView1.SelectedItems(0)
If item.Tag.ToString = "No" Then
item.Selected = False
End If
End If
End Sub
As per # jmcilhinney , The following code should work with MultiSelect = True. I tried to access the last item added to the collection but it seems that the SelectedItems collection is ordered the same as the order the items appear in the ListView; not as expected the last item added would be last in the collection..
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If Not ListView1.SelectedItems.Count = 0 Then
For Each item As ListViewItem In ListView1.SelectedItems
If item.Tag.ToString = "No" Then
item.Selected = False
End If
Next
End If
End Sub

Setting ListView Property FocusedItem to "Nothing" in VB.Net

This Sub is supposed to remove the row of data (Items and subItems) at the "Selected/Focused" ListView index. It works but, after the selected row is removed, the Focus remains on the index of the row that was just Deleted.
Now the curent focused index contains a new row of data. I need "ListView1.FocusedItem" to reset to "Nothing" in order to make the User manualy select a row in the listView before the "Remove" button will remove anything.
As it is now, after the first row is selected and removed, you can just keep clicking the "Remove" button and it will keep deleting rows until the ListView is empty.
Private Sub btn_Remove_Click(sender As Object, e As EventArgs) Handles btn_Remove.Click
If IsNothing(Me.ListView1.FocusedItem) Then
MessageBox.Show("Please select an Item from the grocery list to be removed.")
ElseIf ListView1.SelectedItems.Count <= 0 Then
Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)
' Need ListView1.FocusedItem to reset to "Nothing" here <<--------------------
Else
MessageBox.Show("There are no Items on the grocery list to be removed.")
End If
End Sub
You can make nothing selected with:
ListView1.SelectedItems.Clear()
Setting "ListView1.FocusedItem.Focused" to "False" seems to have fixed my issue. I added an if statment containing "ListView1.FocusedItem.Focused = False". Now, when the Remove button is clicked and there has not been a manual selection of an Item in the ListView, "Me.ListView1.FocusedItem" IsNothing. Here is the working code:
Private Sub btn_Remove_Click(sender As Object, e As EventArgs) Handles btn_Remove.Click
If IsNothing(Me.ListView1.FocusedItem) Then
MessageBox.Show("There are no Items selected to be removed from the grocery list.")
ElseIf ListView1.SelectedItems.Count >= 0 Then
Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)
If ListView1.Items.Count > 0 Then
ListView1.FocusedItem.Focused = False
End If
Else
MessageBox.Show("An Error has halted thid process")
End If
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.

How to adding checked item from checkedlistbox to combobox

I want to adding checked item from checkedlistbox to my combobox, but i have a little problem here. Combobox only show 1 item last checked.
This is my sample code.
If CheckedListBox1.CheckedItems.Count <> 0 Then
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
End If
anyone can help me show all checked item??
thank's for your help...
You aren't adding the items to the combo box, you're only setting its Text property. That's only changing the text currently displayed in the combo box, and only one item can be displayed at a time.
If you want the items to be permanent and selectable, you need to add them to the combo box control's Items collection.
Sample code:
If CheckedListBox1.CheckedItems.Count > 0 Then
For Each checkedItem In CheckedListBox1.CheckedItems
cbCheckedItem.Items.Add(checkedItem.ToString())
Next
End If
Or, better yet, use the AddRange method:
If CheckedListBox1.CheckedItems.Count > 0 Then
Dim checkedItems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray()
cbCheckedItems.Items.AddRange(checkedItems)
End If
Oddly enough the CheckedListBox has a CheckedItems property, which is a collection. As such you can loop through it like you can any other collection, using a For or For Each loop.
then, Each item needs to be added to the Items collection of the ComboBox.
like this sample:
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1.BeginUpdate()
Me.CheckedListBox1.Items.Add("One")
Me.CheckedListBox1.Items.Add("Two")
Me.CheckedListBox1.Items.Add("Three")
Me.CheckedListBox1.Items.Add("Four")
Me.CheckedListBox1.Items.Add("Five")
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As String In Me.CheckedListBox1.CheckedItems
Me.ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
End Class
As sample code shows, the CheckedItems collection contains the items that are checked, just as the name suggests. It doesn't contain a Boolean value for each an every item to indicate whether it is checked or not. If an item is checked then that item is in the CheckedItems, and if it isn't then it isn't. You simply need to loop through the collection and get every item in it, because it contains all the items that are checked and none that aren't.
in the end you can put :
Me.Combobox1.items.clear()
because when it would click with sample code it would have the one that clicked then on the next click would return the previous one it had clicked and then the new one all compiled in the combobox selection menu
perhaps my answer can help you solve your problems
Combobox doesn't have a multiselect option. so only one item at a time could be selected.