• 0

[vb .net] handling checkbox event in listview


Question

2 answers to this question

Recommended Posts

  • 0
  Quote
Property Value

true if a check box appears next to each item in the ListView control; otherwise, false. The default is false.

Remarks

The CheckBoxes property allows you to display a check box next to each item in the list. This enables your application to display a list of items (and subitems if the View property is set to View.Details) that the user can select by clicking the check box. The CheckBoxes property offers a way to select multiple items in the ListView control without using the CTRL key. Depending on your application, using check boxes to select items rather than the standard multiple selection method may be easier for the user. Even if the MultiSelect property of the ListView control is set to false, you can still display checkboxes and provide multiple selection capabilities to the user. This feature can be useful if you do not want multiple items to be selected yet still want to allow the user to choose multiple items from the list to perform an operation within your application.

To determine when an item has been checked, create an event handler for the ItemCheck event. To get all the items that are checked in the ListView, use the CheckedItems property to access the ListView.CheckedIndexCollection for the control. To get the indexes of all items that are checked in the ListView, use the CheckedIndices property

If an ImageList is specified in the StateImageList property, the images at index positions 0 and 1 in the ImageList are displayed instead of the check box. The image at index position 0 is displayed instead of the unchecked check box, and the image at index position 1 is displayed instead of the checked check box.

Example
[Visual Basic, C#] The following code example demonstrates the System.Windows.Forms.CheckedListViewItemCollection class.The example handles the System.Windows.Forms.ListView.ItemChecked event and uses the ListView. CheckedItems property to tally a new price each time an item is checked or unchecked. To run this example paste the following code in a form containing a ListView named ListView1 and a TextBox named Textbox1. Call the InititalizeListView method from the form's constructor or Load method. Ensure all events are connected to their event-handling methods.

[Visual Basic] 
Private Sub InitializeListView()
    Me.ListView1 = New System.Windows.Forms.ListView

    ' Set properties such as BackColor, Location and Size
    Me.ListView1.BackColor = System.Drawing.SystemColors.Control
    Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top
    Me.ListView1.Location = New System.Drawing.Point(0, 0)
    Me.ListView1.Size = New System.Drawing.Size(292, 130)
    Me.ListView1.View = System.Windows.Forms.View.Details
    Me.ListView1.HideSelection = False

    ' Allow user to select multiple items.
    Me.ListView1.MultiSelect = True

    ' Show check boxes in the ListView.
    Me.ListView1.CheckBoxes = True

    'Set the column headers and populate the columns.
    ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
    Dim columnHeader1 As New ColumnHeader
    With columnHeader1
        .Text = "Breakfast Choices"
        .TextAlign = HorizontalAlignment.Left
        .Width = 146
    End With
    Dim columnHeader2 As New ColumnHeader
    With columnHeader2
        .Text = "Price Each"
        .TextAlign = HorizontalAlignment.Center
        .Width = 142
    End With
    Me.ListView1.Columns.Add(columnHeader1)
    Me.ListView1.Columns.Add(columnHeader2)
    Dim foodList() As String = New String() {"Juice", "Coffee", _
        "Cereal & Milk", "Fruit Plate", "Toast & Jelly", _
        "Bagel & Cream Cheese"}

    Dim foodPrice() As String = New String() {"1.09", "1.09", "2.19", _
        "2.79", "2.09", "2.69"}
    Dim count As Integer

    ' Members are added one at a time, so call BeginUpdate to ensure 
    ' the list is painted only once, rather than as each list item is added.
    ListView1.BeginUpdate()

    For count = 0 To foodList.Length - 1
        Dim listItem As New ListViewItem(foodList(count))
        listItem.SubItems.Add(foodPrice(count))
        ListView1.Items.Add(listItem)
    Next

    'Call EndUpdate when you finish adding items to the ListView.
    ListView1.EndUpdate()
    Me.Controls.Add(Me.ListView1)
End Sub
. . . 
' Handles the ItemCheck event.  The method loops through all the 
' checked items and tallies a new price each time an item is 
' checked or unchecked. It outputs the price to TextBox1.
Private Sub ListView1_ItemCheck2(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
    Handles ListView1.ItemCheck

    Dim item As ListViewItem
    Dim price As Double = 0.0
    Dim checkedItems As ListView.CheckedListViewItemCollection = _
        ListView1.CheckedItems
    For Each item In checkedItems
        price += Double.Parse(item.SubItems(1).Text)
    Next
    If (e.CurrentValue = CheckState.Unchecked) Then
        price += Double.Parse(Me.ListView1.Items(e.Index).SubItems(1).Text)
    ElseIf (e.CurrentValue = CheckState.Checked) Then
        price -= Double.Parse(Me.ListView1.Items(e.Index).SubItems(1).Text)
    End If

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)
End Sub

http://msdn.microsoft.com/library/default....nclasstopic.asp

MSDN (Y)

Edited by xStainDx
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft takes unreliability to reliable levels, ffs...
    • It's a feature, not a bug: Windows Hello face unlock no longer works in the dark by Usama Jawad Windows Hello is a security mechanism offered by Microsoft in compatible Windows PCs that allows customers to use biometric authentication mechanisms such as facial or iris recognition, fingerprint, or a PIN to access their accounts. Although it's a great feature when it works correctly, that is not always the case. Now, Microsoft has changed the way this feature works, and people aren't exactly pleased. As spotted by Windows Central, Windows Hello hasn't been working as people would expect since at least April's Patch Tuesday update. Since then, the mechanism requires both an infrared (IR) sensor and a color webcam to function correctly. Previously, Windows Hello relied on IR sensors to create a 3D facial map, just like the iPhone. IR sensors work quite well in low-light environments, but Microsoft does not allow customers to rely on them anymore due to a patch for a security vulnerability. It is important to note that most webcams don't illuminate the environment on their own and rely on external light. This essentially means that Windows Hello simply doesn't work in low-light environments for many users. In our own testing, Windows Hello seems to be working in low warm light, shown below: However, it now fails to work in an even dimmer environment, such as that with red light: That said, it seems that Microsoft has not implemented the change perfectly. Users have reported that if they disable their webcam through Device Manager, Windows Hello falls back to using IR sensors only and continues to function the way it used to. While that may act as a workaround for users right now, it'll be interesting to see if Microsoft patches this loophole down the line, too; it certainly wouldn't be surprising now that it's more widely known. Source: Windows Central
    • I know Windows DHCP and DNS servers get teased a lot, but those services were at least created at a time when MS cared more than they do now, and they work mostly as expected. I actually use both in a multisite professional setting, not really because I love them, but because we use Active Directory and those services go hand-in-hand with it. A few times when I have needed to configure some more advanced features, I was worried Windows might not support them, but so far it has not let me down. I can only image the horror a Windows 11 era DHCP server would be, probably fewer options than the basic service on a Netgear router.
    • Since the article doesn't cover it: >Pinned Tabs allow you to always keep your favorite tabs open and just a click away. Pinned Tabs are small, can't be closed accidentally, and open automatically when you start Firefox. from https://support.mozilla.org/en...keep-favorite-websites-open
    • CapCut 6.4.0 (offline installer) by Razvan Serea CapCut is a versatile video editing app that offers a range of features such as multi-layer editing, keyframe animations, special effects, and more, to create professional-quality videos. With CapCut, users can edit and enhance their videos with a variety of tools such as filters, transitions, effects, and text overlays. CapCut's extensive library of pre-designed templates and visual effects also allows users to create unique and eye-catching videos in just a few clicks. Users can also adjust video speed, crop, and merge multiple clips, among other features. CapCut is available for both mobile devices and Windows, making it accessible for everyone. CapCut key features: User-friendly interface for easy video editing A wide range of editing tools, including trim, split, cut, and merge Music library with a wide range of tracks to choose from Customizable text and fonts to add captions and titles Multi-layer timeline for seamless editing and layering Filters and effects to enhance video quality and style A variety of transitions to choose from Multiple aspect ratio options for different platforms Green screen/chroma key for adding custom backgrounds Overlays and stickers to add to your videos Easy exporting to different video formats and resolutions Large library of pre-designed templates and visual effects Customizable video thumbnails for branding Keyframe animation to add movement to your video Speed adjustment for slow motion or time-lapse effects Customizable transitions between clips Reverse video playback for creative effects Voiceover recording and editing for narrating your video Color grading tools and much more... Download: CapCut 6.4.0 | 709.0 MB (Freeware) Links: CapCut Website | CapCut Screenshot | CapCut Online Editor Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      642
    2. 2
      ATLien_0
      279
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      131
  • Tell a friend

    Love Neowin? Tell a friend!