Are ListviewItems ever truely deselected? - vb.net

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.

Related

Deselect an Item in a Listbox when clicked in empty space

So I am working on a software at the moment and when you click the load button to show files in a folder I want to make it where when you click in the empty white space to deselect the item. Below is the code I am using and I can't seem to get anywhere it still keeps the item selected.
Private Sub Listbox1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseUp
If ListBox1.IndexFromPoint(e.Location) < 0 Then
' clear the data
End If
End Sub
Any help is appreciated!

Make listview with checkboxes act like checkbox list

I need a checkbox list that I can add read-only items (hence using a listview so I can gray-out an item and keep a user from selecting it).
However, when I click the item, the checkbox doesn't toggle. But when I add the following code to the item click event,
Private Sub LVSubFiles_Click(sender As Object, e As EventArgs) Handles LVSubFiles.Click
If LVSubFiles.Items(LVSubFiles.FocusedItem.Index).ForeColor <> Drawing.Color.Gray Then
If LVSubFiles.Items(LVSubFiles.FocusedItem.Index).Checked = True Then
LVSubFiles.Items(LVSubFiles.FocusedItem.Index).Checked = False
Else
LVSubFiles.Items(LVSubFiles.FocusedItem.Index).Checked = True
End If
End If
End Sub
But in this case when the user clicks on the checkbox rather than the item, nothing happens, as well selecting any other checkbox checks both the highlighted item and the checkbox of the new item selected.
Is there a way I can make the items act like a checkbox list? I've tried using Data Grid View, but i run into similar issues and a lot of code is based on the actions of this listview.
For those who find this question. I ultimately went with a hidden column which tracked "read only" items. Whenever the list would update the read-only tags would turn the item gray, but still enable it to be checked.
In regard to how the list interacted with the user, I moved the action items into the two categories and this seems to be working smoothly. The user can select/deselect with a single click regardless of which part of the item is clicked.
Private Sub dgvSubFiles_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSubFiles.CellContentClick
If dgvSubFiles(dgvSubFiles.Columns("chkSubFiles").Index, dgvSubFiles.CurrentCell.RowIndex).Value = True Then
dgvSubFiles(dgvSubFiles.Columns("chkSubFiles").Index, dgvSubFiles.CurrentCell.RowIndex).Value = False
Else
dgvSubFiles(dgvSubFiles.Columns("chkSubFiles").Index, dgvSubFiles.CurrentCell.RowIndex).Value = True
End If
End Sub
Private Sub dgvSubFiles_SelectionChanged(sender As Object, e As EventArgs) Handles dgvSubFiles.SelectionChanged
dgvSubFiles.ClearSelection()
End Sub

How to remove highlight of combobox when an Item is selected in Vb?

In load page event:
I read some data from DB and then add this data to Combo box then select an item as default and enable of combo box changes to false.
When I load this page, the item witch selected highlights with blue color.
How to remove this highlight?
You can modify its SelectionLength property, which gets or sets how many characters have been selected (highlighted).
Just set it to 0 after you have selected the default item and you should be good to go:
ComboBox1.SelectionLength = 0
EDIT:
In your case this code is executed before the Load event has finished. Due to this the form has not been rendered yet, which is why it is not working for you.
The simple fix is to add this in the form's Shown event too:
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
ComboBox1.SelectionLength = 0
End Sub
If you don't want the caret to be in the beginning you can also add this line to set it to the end of the text:
ComboBox1.SelectionStart = ComboBox1.Text.Length
The proposed solution does not work in VB.NET 2016
The easiest way to do is to pass focus to another element like a label in the SelectedIndexChanged event

Stop items in a list view from being selected on right click

I have a ListView on a Windows Form in VB 2010.
I have set the MultiSelect property of the ListView to False so that only one item can be selected at any time.
I have configured a context menu for the ListView and it shows up correctly when the ListView is right clicked.
[Added a ContextMenuStrip control in the Designer and set the ContextMenuStrip property of the ListView to this.]
Consider these 2 scenarios:
A user right clicks on an item that is already selected in the ListView. Then the context menu is displayed and there are no issues.
A user right clicks on an item other than the item that is already selected in the ListView, Then before the context menu is displayed, the item that the user right clicked is selected.
In scenario 2, I need to stop the item that the user right clicks on from being selected automatically. Need to context menu to be displayed but the item that was previously selected should remain selected.
How can I achieve this?
I noticed that on the ListView's MouseDown event, the SelectedItems.Item(0).Index property is still at the old index. However, on the MouseUp event, this property changes to the new index.
In the MouseDown event handler, or anywhere else, how can I stop the SelectedItems from changing? Or how can I change it back to the previous selected item (without the user noticing it is being changed and then changed back)?
I can catch a right click on the MouseDown or MouseUp using the code below. However, I am not sure what I need to put inside this condition to stop the SelectedItems from changing.
If e.Button = Windows.Forms.MouseButtons.Right Then
...
End If
Note: I am able to use the following code for this. However, when I use this with scenario (2), it selects the item that the user right clicked on and then changes it back to the previous item and this change back is seen by the user. Therefore this solution cannot be used.
Dim intPrevSelectedIndex As Integer = -1
Dim boolCancel As Boolean = False
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
If ListView1.SelectedItems.Count > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Right Then
boolCancel = True
intPrevSelectedIndex = ListView1.SelectedItems(0).Index
End If
End Sub
Private Sub ListView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
If boolCancel Then
lstWalkResults.Items(intPrevSelectedIndex).Selected = True
boolCancel = False
End If
End Sub
Please let me know any solutions you might have. Thanks for your time!
In the code behind you should be able to handle the right click event. In that method you would display the context menu manually and then ignore the click event preventing the item from being selected.
If e.Button = Windows.Forms.MouseButtons.Right Then
//display context menu because you're handling the click event manually.
...context menu code...
Dim ee As New System.Windows.Forms.MouseEventArgs(Forms.MouseButtons.None, e.Clicks, e.X, e.Y, e.Delta)
e = ee
End If

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.