Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox color not changing

Status
Not open for further replies.

slowNsteady

Programmer
May 29, 2009
22
0
0
US
Hi

In vb.net 2.0,i have created a checkbox programatically.
now i am trying to change the color of it.
It is not changing

Here is the code:

Dim chkDS1 As New CheckBox
chkDS1.Location = New Point(200, 118)
chkDS1.Text = ""
chkDS1.CheckState = CheckState.Checked
chkDS1.Visible = True
chkDS1.Enabled = true
chkDS1.BackColor = Color.Cyan
Panel1.Controls.Add(chkDS1)

is there any other way to do it ??

Thanks,
Sri
 

Works for me.

I've tied your code and got blue-ish (Cyan I guess) background in the checkbox created.

What do you expect to get and what are you actually getting?

Have fun.

---- Andy
 
Hi Andy

chkDS1.BackColor = Color.Cyan

when you run this code it changes only the backcolor of the whole checkbox to cyan and not the color of small box on the right which has the tick mark (when checked during runtime).It remains white when it is enabled and default grey when it is disabled during runtime. I need to change color to my choice & I also need to change the dimensions of that small box.

hope you got what iam trying to do here

i tried all the attributes in the property box ,cannot find it

thanks
sri

 

I see....

In this case you need to create your own custom check box, based on (inherit from) the 'regular' check box. Then you can change anything you want.

I am sure people here will help you. I can not - never done it myself. I just use out-of-the-box controls in VB.NET

Have fun.

---- Andy
 

I'm not sure if this is what you want, but here is a class that inherits CheckBox and fills the actual check square with a color instead of a check or x:

Code:
Public Class ColorCheckBox
    Inherits System.Windows.Forms.CheckBox

    Private CheckRegionColor As Color = Color.Cyan

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim CheckRegion As New Rectangle(2, 3, 9, 9)

        MyBase.OnPaint(e)

        If Me.Checked Then
            e.Graphics.FillRectangle(New SolidBrush(CheckRegionColor), CheckRegion)
        End If

    End Sub
End Class


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Sorry if my question is dumb

i copied your code and ran it as new project. The form appears to be blank.I cannot find the rectangle on the form. Is that supposed to be like that ??

please explain me.

sri
 

1. In Visual Studio, right-click the project in the Solution Explorer window. In the popup menu, select Add->Class... In the Add New Item dialog, name the class ColorCheckBox.vb and click the Add button.

2. Open the ColorCheckBox.vb file in the Solution Explorer, copy the code I gave you and paste it into the class file.

3. Build the project

4. Open a form in design view. Add a form if needed.

5. In the Toolbox, look under <Project Name> Components. You should see an entry for ColorCheckBox. Drag this item to the form. You should now see a ColorCheckBox on the form. Note: look for your project's name in place of <Project Name>.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
i could not find any Chkbox components (chkbox is my <projectname>) in the tool box.

wat do to ?
 
finally got a way (not a very perfect way but will work) to change the backcolor of the small checkbox which has the tick mark

checkbox1.text = ""
checkbox1.size = New Size(15, 14)
chkVIPF.BackColor = Color.Maroon
chkVIPF.FlatStyle = FlatStyle.Popup

Sri


 
finally got a way (not a very perfect way but will work) to change the backcolor of the small box which has the tick mark

checkbox1.text = ""
checkbox1.size = New Size(15, 14)
checkbox1.BackColor = Color.Maroon
checkbox1.FlatStyle = FlatStyle.Popup

Sri

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top