Setting ListView Property FocusedItem to "Nothing" in VB.Net - 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

Related

check combobox selected index changed in datagridview in vb.net

Hi all I'm developing an accounting software for a small business where I'm facing an issue in invoice part where user is going to put item for buy / create invoice here I'm fetching data from items table to display as a combobox in data grid view but I want to fetch amount and item code when user will select a item in combo box also I want to hide the selected item from the next row so user is not able to select same item again and again. I had tried all possible solution available on internet mostly are for normal combobox or else in the c# which i'm not able to understand.
You can do it under the CellEndEdit Event
Private Sub dgv_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
Dim StockCol As Integer = dgv.Columns("StockColName").Index ' Get the combobox column index
Try
For i = 0 To dgv.RowCount - 1
If e.RowIndex <> i Then 'skip the comparison for current row
If dgv.Item(StockCol , i).Value = dgvPurchase.Item(StockCol , e.RowIndex).Value Then
MsgBox("Similar Item is selected", MsgBoxStyle.Critical, "Error")
dgv.Item(StockCol , e.RowIndex).Value = "" 'reset the cell to ""
Exit Sub
End If
End If
Next
Catch
End Try

listview condition if selected index

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

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

Listview Checkbox - vb.net

Ok, i have a listview with checkboxes and a button, how it works is that i have to check the items i want to change the values, then press the button to change the value of those checked item, here's my code on the button.
Try
Dim I As Integer
If lv_id.CheckedItems.Count = 0 Then
For I = 0 To lv_id.Items.Count - 1
lv_id.Items(I).SubItems(1).Text = "Pending"
Next
Else
For I = 0 To lv_id.CheckedItems.Count - 1
lv_id.CheckedItems(I).SubItems(1).Text = "Submitted"
Next
End If
Proc_Items.BackColor = Color.Green
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now, what i want to do is, to remove the button and then when i'll check the item i want to the code above will do the process without pressing the button, i tried "ItemCheck, ItemChecked" event, but with no luck.
You should be able to use the ItemChecked event for this. With the ItemCheckedEventArgs giving you everything you need.
This example would set up to toggle the 3rd column depending on the checkbox state
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
If e.Item.Checked Then
e.Item.SubItems(2).Text = "Submitted"
Else
e.Item.SubItems(2).Text = "Pending"
End If
End Sub
If I understood you right, you want to run your code whenever a ckecbox in your ListView gets checked or unchecked. This can be done using
Public Class Form1
'The ListView_SelectedIndexChanged event triggers when a checkbox of the listview gets checked or unchecked
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'Put your code here, access all checked items with "ListBox1.CheckedItems"
End Sub
End Class
This worked for me, and I think it works for you as well. If not, please tell me in the comments what went wrong.

Are ListviewItems ever truely deselected?

I have a listview with several items in it. If I have an item selected and click in an empty white space or anywhere else on the form the highlight is removed but .FocusedItem & .SelectedItems still report an item is selected.
I have events that I want to trigger when no listviewitems are selected, but that never seems to occur. How do I detect if no items are selected or does that ever actually happen after that first item gets clicked?
Private Sub lstCats_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCats.SelectedIndexChanged
If IsNothing(lstCats.FocusedItem) Then
DisableGUI()
Else
EnableGUI()
DisplayQuestions()
End If
End Sub
Basically DisableGUI() will never execute.
Thanks!
You can deselect all items in the ListView by clicking on an empty part of the ListView control (that is not on one of the items). For example:
Private Sub lstCats_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCats.SelectedIndexChanged
If lstCats.SelectedItems.Count = 0 Then
DisableGUI()
Else
EnableGUI()
DisplayQuestions()
End If
End Sub
Note if you click on an item in the ListView, SelectedItems will be > 0. If you click on the background of the ListView (not on an item), SelectedItems will be = 0. This assumes that there is part of the ListView control that does not contain an item.