jeudi 9 janvier 2020

Custom checkbox background issue in VB.Net

I was trying to build a custom checkbox, but I see a black rectangle around the checkbox. Why does this happen? And why the second checkbox has the first overlapped?

It looks like there's a black rectangle in a default position of the control. When I add one more checkbox, the second looks like 2 checkboxes in the same position.

Thanks.

Public Class mycheckbox

    Inherits CheckBox

Protected Overrides Sub onpaint(pevent As PaintEventArgs)

        pevent.Graphics.FillRectangle(New SolidBrush(BackColor), Location.X, Location.Y, Width, Height)

        Dim brsh As New SolidBrush(Color.YellowGreen)
        Dim boxside As Integer = CInt(pevent.Graphics.MeasureString(Text, Font, Width).Height)
        pevent.Graphics.FillRectangle(brsh, Location.X, Location.Y, Width, Height)

        If Checked And Enabled Then

            pevent.Graphics.DrawImage(My.Resources.X, Location.X + 1, Location.Y + 1, 18, 18)
            pevent.Graphics.DrawRectangle(Pens.Black, New Rectangle(Location.X, Location.Y, 19, 19))
            pevent.Graphics.DrawString(Text, Font, Brushes.Black, boxside + 15, 11)

        End If

        brsh.Dispose()

    End Sub

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim mcb1 As New mycheckbox
        Dim mcb2 As New mycheckbox

        mcb1.Name = "cb1"
        mcb1.Text = "Hello!!!"
        mcb1.Location = New Point(10, 10)
        mcb1.Size = New Size(300, 30)

        mcb2.Name = "cb2"
        mcb2.Text = "Hi!!!"
        mcb2.Location = New Point(10, 50)
        mcb2.Size = New Size(300, 30)

        Me.Controls.Add(mcb1)
        Me.Controls.Add(mcb2)


    End Sub



Aucun commentaire:

Enregistrer un commentaire