Change CheckedComboBox item font size using C# and VB.Net in Windows Application

Last Reply 8 days ago By pandeyism

Posted 8 days ago

Hi!

I used checkcombobox in windows form. Its worked without problem. Populate its from database. Now I want change item font size. Can anybody help me? 

            DataTable dt = cash.getNameUnderBolDep(cmbUndep.SelectedValue.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
               CCBoxItem item = new CCBoxItem(dt.Rows[i][1].ToString().Trim(), Convert.ToInt32(dt.Rows[i][0].ToString().Trim()));
               cmbForm.Items.Add(item);
               float size = Convert.ToSingle(((CheckComboBox)sender).Text);
               cmbForm.Font = new(cmbForm.Font.FontFamily, size);
               cmbForm.Font = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold);
            }

 

Posted 8 days ago Modified on 8 days ago

Hey PRA,

We do not support third party tools or dll, But created a sample for full-fill your requirement.

Namespaces

C#

using System.Data.SqlClient;
using System.Data;
using System.Drawing;

VB.Net

Imports System.Data.SqlClient

Code

CheckComboBox.cs

public class CheckComboBoxItem
{
    public CheckComboBoxItem(string text, bool initialCheckState)
    {
        _checkState = initialCheckState;
        _text = text;
    }

    private bool _checkState = false;
    public bool CheckState
    {
        get { return _checkState; }
        set { _checkState = value; }
    }

    private string _text = "";
    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }

    private object _tag = null;
    public object Tag
    {
        get { return _tag; }
        set { _tag = value; }
    }
    public override string ToString()
    {
        return "Select Options";
    }
}

public partial class CheckComboBox : ComboBox
{
    public CheckComboBox()
    {
        this.DrawMode = DrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(CheckComboBox_DrawItem);
        this.SelectedIndexChanged += new EventHandler(CheckComboBox_SelectedIndexChanged);
        SelectedText = "Select Options";
    }

    void CheckComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckComboBoxItem item = (CheckComboBoxItem)SelectedItem;
        item.CheckState = !item.CheckState;
        if (CheckStateChanged != null)
            CheckStateChanged(item, e);
    }

    void CheckComboBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1)
        {
            return;
        }

        if (!(Items[e.Index] is CheckComboBoxItem))
        {
            e.Graphics.DrawString(Items[e.Index].ToString(), this.Font, Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
            return;
        }

        CheckComboBoxItem box = (CheckComboBoxItem)Items[e.Index];
        CheckBoxRenderer.RenderMatchingApplicationState = true;
        CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.X, e.Bounds.Y), e.Bounds, box.Text, this.Font, (e.State & DrawItemState.Focus) == 0, box.CheckState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
    }
    public event EventHandler CheckStateChanged;

}

CheckComboBox.vb

Public Sub New(ByVal text As String, ByVal initialCheckState As Boolean)
    _checkState = initialCheckState
    _text = text
End Sub

Private _checkState As Boolean = False

Public Property CheckState As Boolean
    Get
        Return _checkState
    End Get
    Set(ByVal value As Boolean)
        _checkState = value
    End Set
End Property

Private _text As String = ""

Public Property Text As String
    Get
        Return _text
    End Get
    Set(ByVal value As String)
        _text = value
    End Set
End Property

Private _tag As Object = Nothing

Public Property Tag As Object
    Get
        Return _tag
    End Get
    Set(ByVal value As Object)
        _tag = value
    End Set
End Property

Public Overrides Function ToString() As String
    Return "Select Options"
End Function
End Class

Partial Public Class CheckComboBox
    Inherits ComboBox

    Public Sub New()
        Me.DrawMode = DrawMode.OwnerDrawFixed
        AddHandler Me.DrawItem, AddressOf CheckComboBox_DrawItem
        AddHandler Me.SelectedIndexChanged, AddressOf CheckComboBox_SelectedIndexChanged
        SelectedText = "Select Options"
    End Sub

    Private Sub CheckComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim item As CheckComboBoxItem = CType(SelectedItem, CheckComboBoxItem)
        item.CheckState = Not item.CheckState
        RaiseEvent CheckStateChanged(item, e)
    End Sub

    Private Sub CheckComboBox_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
        If e.Index = -1 Then
            Return
        End If

        If Not (TypeOf Items(e.Index) Is CheckComboBoxItem) Then
            e.Graphics.DrawString(Items(e.Index).ToString(), Me.Font, Brushes.Black, New Point(e.Bounds.X, e.Bounds.Y))
            Return
        End If

        Dim box As CheckComboBoxItem = CType(Items(e.Index), CheckComboBoxItem)
        CheckBoxRenderer.RenderMatchingApplicationState = True
        CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(e.Bounds.X, e.Bounds.Y), e.Bounds, box.Text, Me.Font, (e.State And DrawItemState.Focus) = 0, If(box.CheckState, CheckBoxState.CheckedNormal, CheckBoxState.UncheckedNormal))
    End Sub

    Public Event CheckStateChanged As EventHandler

Form1.cs

public Form1()
{
    InitializeComponent();
    checkComboBox1.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Test;uid=sa;pwd=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name,Country FROM Customers", con))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem(dt.Rows[i]["Country"].ToString(), true));
                checkBox1.Visible = false;
                checkBox2.Visible = false;
                checkBox3.Visible = false;
                this.checkComboBox1.CheckStateChanged += new System.EventHandler(this.checkComboBox1_CheckStateChanged);
            }
        }
    }
}

private void checkComboBox1_CheckStateChanged(object sender, EventArgs e)
{
    if (sender is CheckComboBox.CheckComboBoxItem)
    {
        CheckComboBox.CheckComboBoxItem item = (CheckComboBox.CheckComboBoxItem)sender;
    }
}

Form1.vb

Public Sub New()
    InitializeComponent()
    checkComboBox1.Font = New Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold)
    Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Test;uid=sa;pwd=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, Name,Country FROM Customers", con)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
            For i As Integer = 0 To dt.Rows.Count - 1
                checkComboBox1.Items.Add(New CheckComboBoxItem(dt.Rows(i)("Country").ToString(), True))
                checkBox1.Visible = False
                checkBox2.Visible = False
                checkBox3.Visible = False
                AddHandler Me.checkComboBox1.DrawItem, AddressOf checkComboBox1_CheckStateChanged
            Next
        End Using
    End Using
End Sub

Private Sub checkComboBox1_CheckStateChanged(ByVal sender As Object, ByVal e As EventArgs)
    If TypeOf sender Is CheckComboBoxItem Then
        Dim item As CheckComboBoxItem = CType(sender, CheckComboBoxItem)
    End If
End Sub

Screenshot

And refer below link might be helpful for applying font size in your dll -

https://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c15261/Extending-the-ComboBox-with-C.htm

https://stackoverflow.com/questions/47920007/adding-checkbox-to-combobox-not-visible-in-dropdown