Disable listview item in vb.net - 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

Related

Selecting 1 Tool Strip Menu Item at any one time

I have a Tool strip menu with 4 options.
The options are degrees of screen rotation. (0, 90, 180, 270)
I am trying to select only 1 of these and keep it selected.
When I choose one then another, both are ticked (selected).
I have searched and found some solutions but only this works for me.
I have used the click event for each option to clear the ones not chosen like below.
Private Sub DegreesToolStripMenuItem6_Click(sender As Object, e As EventArgs) Handles DegreesToolStripMenuItem6.Click
DegreesToolStripMenuItem6.Checked = True
DegreesToolStripMenuItem7.Checked = False
DegreesToolStripMenuItem8.Checked = False
DegreesToolStripMenuItem9.Checked = False
End Sub
This sub is in my code 4 times to make it work but i'm sure there must be a cleaner, easier way to do this.
I have found online some other solutions but I can't seem to make it work, this one kind of makes sense like it should work but I can't figure it out
Private Sub DegreesToolStripMenuItem6_CheckedChanged(sender As Object, e As EventArgs) Handles DegreesToolStripMenuItem6.CheckedChanged, _
DegreesToolStripMenuItem7.CheckedChanged, _
DegreesToolStripMenuItem8.CheckedChanged, _
DegreesToolStripMenuItem9.CheckedChanged
Dim currentItem As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
If currentItem IsNot Nothing AndAlso currentItem.Checked Then
Dim menu As ContextMenuStrip = TryCast(currentItem.Owner, ContextMenuStrip)
If menu IsNot Nothing Then
'The current item has just been checked so uncheck all other items on the same context menu strip.
For Each item As ToolStripMenuItem In menu.Items
If item IsNot currentItem Then
item.Checked = False
End If
Next item
End If
End If
End Sub
the toolstrip menu reads like this
-PreferedRotationToolStripMenuItem-DegreesToolStripMenuItem6
DegreesToolStripMenuItem7
DegreesToolStripMenuItem8
DegreesToolStripMenuItem9
Thanks for your time and looking forward to any help you guys can give.
Set CheckOnClick to True for each menu item, which I'm guessing you already have. This code worked for me:
Private Sub ToolStripMenuItems_CheckedChanged(sender As Object, e As EventArgs) Handles DegreesToolStripMenuItem9.CheckedChanged,
DegreesToolStripMenuItem8.CheckedChanged,
DegreesToolStripMenuItem7.CheckedChanged,
DegreesToolStripMenuItem6.CheckedChanged
Dim currentMenuItem = DirectCast(sender, ToolStripMenuItem)
If currentMenuItem.Checked Then
Dim menu = DirectCast(currentMenuItem.Owner, ContextMenuStrip)
For Each menuItem In menu.items.Cast(Of ToolStripMenuItem)
menuItem.Checked = menuItem Is currentMenuItem
Next
End If
End Sub
Here's a slight variation that avoids checking the already checked current item:
Private Sub ToolStripMenuItems_CheckedChanged(sender As Object, e As EventArgs) Handles DegreesToolStripMenuItem9.CheckedChanged,
DegreesToolStripMenuItem8.CheckedChanged,
DegreesToolStripMenuItem7.CheckedChanged,
DegreesToolStripMenuItem6.CheckedChanged
Dim currentMenuItem = DirectCast(sender, ToolStripMenuItem)
If currentMenuItem.Checked Then
Dim menu = DirectCast(currentMenuItem.Owner, ContextMenuStrip)
For Each menuItem In menu.Items.Cast(Of ToolStripMenuItem).Except({currentMenuItem})
menuItem.Checked = False
Next
End If
End Sub
The code above works if you have added the items to a ContextMenuStrip. If you have added them directly to an item on a MenuStrip then change this:
Dim menu = DirectCast(currentMenuItem.Owner, ContextMenuStrip)
to this:
Dim menu = DirectCast(currentMenuItem.Owner, ToolStripDropDownMenu)

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

Creating/Deleting Objects via Checking if Toggles in CheckListBox in VB.Net

I am playing around with some CheckedListItems in CheckBoxes and i am having issues with determining when a specific checked Product is checked and/or unchecked via its index position.
I know from MsgBox debugging that if i select either Product Allpurpose Cleaner or Cleaning Wipes it calls the MsgBoxes of both methods despite me attempting using logic to only execute for that specific Product. Somehow it thinks I have selected both items?
So essentially i am trying to do this:
If item zero in CheckedListBox is checked
Call conform menu to get desired amountand then come back to ordering menu.
ElseIF item zero in CheckedListBox is unchecked
Remove it from the current order.
This would essentially be rinse and repeat for all items in my CheckedListBox. I suspect VB.NET is causing my code to 'fall through' and it thinks all of the items i select are the same despite my attempts at preventing this.
May i please have some thoughts on this?
Thank you.
Private Sub CleaningProductsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CleaningProductsList.SelectedIndexChanged
Dim allPurposeCleaner = New AllPurposeCleaner()
Dim cleaningCloths = New cleaningCloths()
' If checked.
If CleaningProductsList.GetItemChecked(0) = True Then
isChecked = True
Me.Hide()
' MsgBox("All Purpose Cleaner Selected")
AmountMenue.setGivenProduct(allPurposeCleaner)
AmountMenue.Show()
' If unchecked.
ElseIf CleaningProductsList.GetItemChecked(0) = False Then
isChecked = False
' MsgBox("All Purpose Cleaner UnSelected ")
MsgBox(CleaningProductsList.GetItemChecked(0).ToString + " ALLPURPOSE UNCHECKED")
AmountMenue.removedGivenProduct(allPurposeCleaner)
End If
' If checked.
If CleaningProductsList.GetItemChecked(1) = True Then
Me.Hide()
AmountMenue.setGivenProduct(cleaningCloths)
MsgBox("cleaning cloths Selected ")
AmountMenue.Show()
' If unchecked.
ElseIf CleaningProductsList.GetItemChecked(1) = False And CleaningProductsList.CheckOnClick = False Then
MsgBox("cleaning cloths UnSelected ")
MsgBox(CleaningProductsList.GetItemChecked(2).ToString + " Cleaning Cloths UNCHECKED")
isChecked = False
AmountMenue.removedGivenProduct(cleaningCloths)
End If
End Sub
Here's an example of how you determine changes in a CheckedListBox:
Private checkedIndexes As New List(Of Integer)
Private checkedItems As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckedListBox1.Items.AddRange({"First", "Second", "Third", "Fourth", "Fifth"})
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue = CheckState.Checked Then
'An item is being checked.
checkedIndexes.Add(e.Index)
checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
Else
'An item is being unchecked.
checkedIndexes.Remove(e.Index)
checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
End If
Label1.Text = $"Checked indexes: {String.Join(", ", checkedIndexes)}"
Label2.Text = $"Checked items: {String.Join(", ", checkedItems)}"
End Sub
The e parameter tells you what item is changing, via the Index property, and what it is changing from and to, via the CurrentValue and NewValue properties.
If you want to get a full list of checked items in that event handler, because the event is raised before the change is finalised, you need to start with the list provided by the control and then add or remove the current item:
Dim checkedItems = CheckedListBox1.CheckedItems.Cast(Of String)().ToList()
If e.NewValue = CheckState.Checked Then
checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
Else
checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
End If
'Use checkedItems here.

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

Change the tooltip text when selecting item in listview

I have a listview containing the source addresses of files (from user system) and the destination addresses (two columns, multilpe selection = false).
Since the source addresses might be quite long like:
d:\root\branch1\branch2\branch3\branch4\myfile.dat
the first column shows just:
d:\ ... \myfile.dat
The real path is stored in the ListViewItem.Tag
I want to have a tooltip showing the whole path every time the user clicks (or changes) the selected item. I came out with this:
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
For Each locItem As ListViewItem In ListView1.SelectedItems
With ToolTip1
.RemoveAll()
.SetToolTip(ListView1, locItem.Tag)
End With
Next
End Sub
Now, the tooltip does change, but it always skip one selection. That is:
selecting item 1: tooltip shows correctly
selecting item 2: tooltip don't show
selecting item 3: tooltip shows correctly
selecting item 5: tooltip don't show
selecting item 2: tooltip shows correctly (selection went back to item 2 which didn't show the 1st time)
Any idea?
PS: I am using Visual Studio Community 2015
PPS: I also need to have the View property set to View.Details (to show both columns and headers), so setting ShowItemToolTip = True does not work
CURRENT SOLUTION
I found a workaround, destroying and recreating the tooltip control. Now the tooltip shows correctly on every item:
Private myTooltip As ToolTip
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If myTooltip IsNot Nothing Then myTooltip.Dispose()
myTooltip = New ToolTip
For Each locItem As ListViewItem In ListView1.SelectedItems
myTooltip.SetToolTip(ListView1, locItem.Tag)
Next
End Sub
I'm still baffled about the skipping in the first approach.
ListView has a property called ShowItemToolTips (https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.showitemtooltips(v=vs.110).aspx). You can set that to true and then set the ToolTipText property of the ListViewItems to the long path.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListView1.ShowItemToolTips = True
Dim lvi1 As New ListViewItem With {.Text = "d:\..\myFile.dat", .Tag = "d:\myLongPath\myFile.dat", .ToolTipText = .Tag}
Dim lvi2 As New ListViewItem With {.Text = "d:\..\myFile2.dat", .Tag = "d:\myLongPath\myFile2.dat", .ToolTipText = .Tag}
ListView1.Items.Add(lvi1)
ListView1.Items.Add(lvi2)
End Sub