CheckedListBox editor

Posted by: h-cohen on 8 September 2017, 2:31 pm EST

  • Posted 8 September 2017, 2:31 pm EST

    Hello GrapeCity tech support,

    Using the forums and your FarOut example, I am trying to implement a combobox cell type with a CheckedListBox as the editor. The problem I cannot seem to resolve is that when the combo box closes, the value of the cell is always the last selected row in the checkedlistbox irrespective of whether or not it is checked.  If I check item 2, the cell shows "Two". If I uncheck item 2, the cell also shows "Two". I also tried to capture checkeditems and use SetValue but the editor overrides the value that I put in.

    Any advice you could offer would be grand. My code is below..

    Thank you,

    H Cohen


    Public Class Form1

        WithEvents ccb As New CheckedComboBoxCellType()

        Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click

            FpSpread3.ActiveSheet.Cells(1, 1).CellType = ccb

        End Sub

    End Class

    Public Class CheckedComboBoxCellType

        Inherits FarPoint.Win.Spread.CellType.ComboBoxCellType
        Private clb As Windows.Forms.CheckedListBox

        Sub New()

            clb = New System.Windows.Forms.CheckedListBox
            Dim array_ = New String() {"One", "Two", "Three"}
            clb.DrawMode = DrawMode.OwnerDrawFixed

            MyBase.ListControl = clb

        End Sub

        Public Overrides Sub PaintCell(ByVal g As Graphics, ByVal r As Rectangle, ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal value As Object, ByVal isSelected As Boolean, ByVal isLocked As Boolean, ByVal zoomFactor As Single)
            Dim objStringFormat As StringFormat
            Dim brushText As Brush
            Dim rectfText As RectangleF
            Dim brushBackground As Brush
            Dim rectComboButton As Rectangle

            If value Is Nothing Then
                brushBackground = New SolidBrush(appearance.BackColor)
                g.FillRectangle(brushBackground, r)

                rectComboButton = New Rectangle(r.Right - 17, r.Y, 17, r.Height)
                ControlPaint.DrawComboButton(g, rectComboButton, ButtonState.Normal)
                brushBackground = New SolidBrush(appearance.BackColor)
                g.FillRectangle(brushBackground, r)

                objStringFormat = New StringFormat()
                objStringFormat.Alignment = StringAlignment.Near
                objStringFormat.LineAlignment = StringAlignment.Center

                rectfText = New RectangleF(r.X + 24, r.Y, g.MeasureString(value.ToString(), appearance.Font).Width, r.Height)
                brushText = New SolidBrush(appearance.ForeColor)
                g.DrawString(value.ToString(), appearance.Font, brushText, rectfText, objStringFormat)

                ControlPaint.DrawComboButton(g, New Rectangle(r.Right - 17, r.Y, 17, r.Height), ButtonState.Normal)
            End If
        End Sub 'PaintCell

        Public Overrides Function GetEditorControl(ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal zoomFactor As Single) As Control
            Return MyBase.GetEditorControl(appearance, zoomFactor)
        End Function 'GetEditorControl

    End Class

  • Replied 8 September 2017, 2:31 pm EST

    The default behavior you are inheriting from ComboBoxCellType will display the Text of the last selected index. You need to overwrite GetEditorValue in the Celltype to put the CheckedListBox.CheckedItems into the Cell Value.
  • Replied 8 September 2017, 2:31 pm EST

    Indeed, that worked. Thank you very much.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels