locked
Checkbox/Buttons etc within a list view RRS feed

  • Question

  • Checkbox/Buttons etc within a list view column.

    How is this done?

    Thursday, June 28, 2012 9:14 AM

Answers

  • Can you point me to the precise lines of code that handle the checkbox.. I might be going blind and can't see it?

    I code in C# though do have experience with VB.

    Edit:Nvm I see the checkboxes thing now. Already knew about that one.

    But what I really want is checkboxes (more than one) or other controls within the subitems (combo boxes perhaps?). Is that possible.

    Since you are coding in C# and want to add more than one checkbox per row take a look at the following at Code Project.

    http://www.codeproject.com/Articles/4012/C-List-View-v1-3 (see Highlight section)


    KSG

    • Marked as answer by Danzence Friday, July 6, 2012 2:11 PM
    Friday, July 6, 2012 1:41 PM

All replies

  • As per my assumption you are trying to show the check box inside the ListBox.

    If yes then it is not possible(as per my knowledge) please try to use any other thirdparty control in your .net project


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!


    Thursday, June 28, 2012 9:41 AM
  • Windows has listview controls with checkboxes in, e.g. permissions tab in security settings for files/folders.
    Thursday, June 28, 2012 9:48 AM
  • Hello,

    Please read the MSDN documentation on checkbox for ListView and an example here. Not sure about a button. What you might consider is using a DataGridView instead where doing a CheckBox is easy. If interested I can point you to a Microsoft button control for a DataGridView.

    • Edited by KareninstructorMVP Thursday, June 28, 2012 1:14 PM
    • Marked as answer by Bob Wu-MT Monday, July 2, 2012 9:44 AM
    • Unmarked as answer by Danzence Tuesday, July 3, 2012 12:46 PM
    Thursday, June 28, 2012 1:14 PM
  • If interested I can point you to a Microsoft button control for a DataGridView.

    Yes please :)

    Thursday, June 28, 2012 2:22 PM
  • If interested I can point you to a Microsoft button control for a DataGridView.

    Yes please :)

    It is most likely this one shown below

    Here are two working demos I have done

    http://www.vbforums.com/showthread.php?p=4188531#post4188531
    http://www.vbforums.com/showthread.php?p=3968104#post3968104

    ''' <summary>
    ''' Author: Microsoft
    ''' Tweaks: Kevin Gallagher
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DataGridViewDisableButtonCell
        Inherits DataGridViewButtonCell
        Private mEnabledValue As Boolean
        Public Property Enabled() As Boolean
            Get
                Return mEnabledValue
            End Get
            Set(ByVal value As Boolean)
                mEnabledValue = value
            End Set
        End Property
        Private mShowDisabledText As Boolean
        Public Property ShowDisabledText() As Boolean
            Get
                Return mShowDisabledText
            End Get
            Set(ByVal value As Boolean)
                mShowDisabledText = value
            End Set
        End Property
        Public Overrides Function Clone() As Object
            Dim Cell As DataGridViewDisableButtonCell = CType(MyBase.Clone(), DataGridViewDisableButtonCell)
            Cell.Enabled = Me.Enabled
            Return Cell
        End Function
        Public Sub New()
            Me.mEnabledValue = True
            Me.mShowDisabledText = False
        End Sub
        Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal elementState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
            advancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None
            ' The button cell is disabled, so paint the border, background, and disabled button for the cell.
            If Not Me.mEnabledValue Then
                ' Draw the background of the cell, if specified.
                If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
                    Dim cellBackground As New SolidBrush(cellStyle.BackColor)
                    graphics.FillRectangle(cellBackground, cellBounds)
                    cellBackground.Dispose()
                End If
                ' Draw the cell borders, if specified.
                If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
                    PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
                End If
                ' Calculate the area in which to draw the button.
                Dim buttonArea As Rectangle = cellBounds
                Dim buttonAdjustment As Rectangle = Me.BorderWidths(advancedBorderStyle)
                buttonArea.X += buttonAdjustment.X
                buttonArea.Y += buttonAdjustment.Y
                buttonArea.Height -= buttonAdjustment.Height
                buttonArea.Width -= buttonAdjustment.Width
                If ShowDisabledText Then
                    ' Draw the disabled button.                
                    ButtonRenderer.DrawButton(graphics, buttonArea, VisualStyles.PushButtonState.Disabled)
                    ' Draw the disabled button text. 
                    If TypeOf Me.FormattedValue Is String Then
                        TextRenderer.DrawText(graphics, CStr(Me.FormattedValue), Me.DataGridView.Font, buttonArea, SystemColors.GrayText)
                    End If
                End If
            Else
                ' The button cell is enabled, so let the base class handle the painting.
                MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
            End If
        End Sub
    End Class
    ''' <summary>
    ''' Author: Microsoft
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DataGridViewDisableButtonColumn
        Inherits DataGridViewButtonColumn
        Public Sub New()
            Me.CellTemplate = New DataGridViewDisableButtonCell()
        End Sub
    End Class


    KSG

    • Marked as answer by Bob Wu-MT Monday, July 2, 2012 9:43 AM
    • Unmarked as answer by Danzence Tuesday, July 3, 2012 12:46 PM
    Thursday, June 28, 2012 3:53 PM
  • Hm so how does Microsoft include a tickbox in its list view control subitems (e.g. the security settings for files/folders in AD)

    Tuesday, July 3, 2012 12:53 PM
  • Anyone?
    Wednesday, July 4, 2012 12:25 PM
  • Anyone?

    The following shows how to do a CheckBox. My bet for the ticks they are images done most likely the same as in the link below. I have not tried it but it seems logical.

    http://www.java2s.com/Tutorial/VB/0260__GUI/ListViewwithCheckBoxcell.htm


    KSG

    Thursday, July 5, 2012 5:24 AM
  • Anyone?

    The following shows how to do a CheckBox. My bet for the ticks they are images done most likely the same as in the link below. I have not tried it but it seems logical.

    http://www.java2s.com/Tutorial/VB/0260__GUI/ListViewwithCheckBoxcell.htm


    KSG


    The code in here has nothing to do with the checkbox. It seems incomplete?
    Thursday, July 5, 2012 4:17 PM
  • Anyone?

    The following shows how to do a CheckBox. My bet for the ticks they are images done most likely the same as in the link below. I have not tried it but it seems logical.

    http://www.java2s.com/Tutorial/VB/0260__GUI/ListViewwithCheckBoxcell.htm


    KSG


    The code in here has nothing to do with the checkbox. It seems incomplete?

    The code shows how to do a CheckBox which I thought you wanted. In regards to tick mark as mentioned before that is an image.

    I did try the code out, only needed to change one line to make it work with Option Strict On in a VS2010 project.

    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Imports System.IO
    Public Class Form1
        Dim nIndex As Integer = 0
        Private Sub CreateMyListView(ByVal dirpath As String)
            Dim hImgSmall As IntPtr
            Dim hImgLarge As IntPtr
            Dim shinfo As FileInfoClass.SHFILEINFO = New FileInfoClass.SHFILEINFO()
            ListView1.Clear()
            ImageListSmall.Images.Clear()
            ImageListLarge.Images.Clear()
            nIndex = 0
            ListView1.Columns.Add("File Name", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("Date", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("Attribute", 100, HorizontalAlignment.Center)
            ListView1.LargeImageList = imageListLarge
            ListView1.SmallImageList = imageListSmall
            ListView1.View = View.Details
            ListView1.LabelEdit = True
            ListView1.AllowColumnReorder = True
            ListView1.CheckBoxes = True
            ListView1.FullRowSelect = True
            ListView1.GridLines = True
            ListView1.Sorting = SortOrder.Ascending
            Dim FilesInDir As String() = Directory.GetFiles(dirpath, "*.*")
            Dim SFile As String
            Dim item1 As New ListViewItem("", 0)
            item1.SubItems.Add("1")
            item1.SubItems.Add("2")
            item1.SubItems.Add("3")
            Dim ctdate As Date
            Dim fAttr As FileAttribute
            For Each SFile In FilesInDir
                ctdate = IO.File.GetCreationTime(SFile)
                fAttr = CType(IO.File.GetAttributes(SFile), FileAttribute)
                shinfo.szDisplayName = New String(Chr(0), 260)
                shinfo.szTypeName = New String(Chr(0), 80)
                hImgSmall = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, Marshal.SizeOf(shinfo), FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_SMALLICON)
                ImageListSmall.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon))       'Add icon to smallimageList.
                hImgLarge = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, Marshal.SizeOf(shinfo), FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_LARGEICON)
                ImageListLarge.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon))       'Add icon to LargeimageList.
                ListView1.Items.Add(New ListViewItem(New String() {SFile, CStr(FileLen(SFile)), ctdate.ToString, fAttr.ToString}, nIndex))
                nIndex = nIndex + 1
            Next
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim disks As String() = Directory.GetLogicalDrives
            Dim Sdisk As String
            ComboBox1.Items.Clear()
            For Each Sdisk In disks
                ComboBox1.Items.Add(Sdisk)
            Next
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            CreateMyListView(ComboBox1.Text)
        End Sub
    End Class
    Public Class FileInfoClass
        Public Structure SHFILEINFO
            Public hIcon As IntPtr
            Public iIcon As Integer
            Public dwAttributes As Integer
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
            Public szDisplayName As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
            Public szTypeName As String
        End Structure
        Public Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
                (ByVal pszPath As String, _
                 ByVal dwFileAttributes As Integer, _
                 ByRef psfi As SHFILEINFO, _
                 ByVal cbFileInfo As Integer, _
                 ByVal uFlags As Integer) As IntPtr
        Public Const SHGFI_ICON = &H100
        Public Const SHGFI_SMALLICON = &H1
        Public Const SHGFI_LARGEICON = &H0    ' Large icon
    End Class


    KSG

    Thursday, July 5, 2012 4:28 PM
  • Can you point me to the precise lines of code that handle the checkbox.. I might be going blind and can't see it?

    I code in C# though do have experience with VB.

    Edit:Nvm I see the checkboxes thing now. Already knew about that one.

    But what I really want is checkboxes (more than one) or other controls within the subitems (combo boxes perhaps?). Is that possible.

    • Edited by Danzence Friday, July 6, 2012 1:05 PM
    Friday, July 6, 2012 1:02 PM
  • Can you point me to the precise lines of code that handle the checkbox.. I might be going blind and can't see it?

    I code in C# though do have experience with VB.

    Edit:Nvm I see the checkboxes thing now. Already knew about that one.

    But what I really want is checkboxes (more than one) or other controls within the subitems (combo boxes perhaps?). Is that possible.

    Since you are coding in C# and want to add more than one checkbox per row take a look at the following at Code Project.

    http://www.codeproject.com/Articles/4012/C-List-View-v1-3 (see Highlight section)


    KSG

    • Marked as answer by Danzence Friday, July 6, 2012 2:11 PM
    Friday, July 6, 2012 1:41 PM
  • Wow thank you, that's just what I needed.

    :)

    Friday, July 6, 2012 2:11 PM