We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,628 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Change Color of Command Buttons

I have figured out how to create command buttons and have tried to change their general color, but to no avail. This is what I tried, but it did nothing:

Option Explicit
Dim WithEvents Com As CommandButton

Private Sub Form_Load()
Set Com = Me.Controls.Add("vb.commandbutton", "BtnName")

    With Com
        .Left = 100
        .Top = 100
        .Width = 2000
        .Height = 500
        .Visible = True
        .Caption = "Student 1"
        .DragMode = 0
    End With
End Sub
Private Sub Com_click()
Com.BackColor = &HFF&
End Sub

It runs but does nothing. any help would be very much appreciated.

5
Contributors
18
Replies
2 Weeks
Discussion Span
2 Years Ago
Last Updated
19
Views
ben25x
Light Poster
41 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

You need to change them to GRAPHICAL in order to change the color.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

change the style to Graphical (as WaltP suggest). Com.Style = 1 but this property is read only..you can't change at the run time.
so, you can't do that.

Jx_Man
Senior Poster
3,521 posts since Nov 2007
Reputation Points: 1,468
Solved Threads: 514
Skill Endorsements: 64

you can't do that.

You can, just with a lot of code....

'Change the style...
Private Declare Function GetWindowLong Lib "user32" _
	Alias "GetWindowLongA" (ByVal hWnd As Long, _
	ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
	Alias "SetWindowLongA" (ByVal hWnd As Long, _
	ByVal nIndex As Long, ByVal dwNewLong As Long) _
	As Long
Private Declare Function SendMessage Lib "user32" _
	Alias "SendMessageA" (ByVal hWnd As Long, _
	ByVal wMsg As Long, ByVal wParam As Long, _
	ByVal lParam As Long) As Long

Private Const BS_BITMAP As Long = &H80&
Private Const BS_ICON As Long = &H40&
Private Const BS_TEXT As Long = 0&
Private Const BM_GETIMAGE As Long = &HF6
Private Const BM_SETIMAGE As Long = &HF7
Private Const IMAGE_BITMAP As Long = 0&
Private Const IMAGE_ICON As Long = 1&
Private Const GWL_STYLE As Long = (-16&)
Private Const GWL_EXSTYLE As Long = (-20&)

Public Sub SetButtonGlyph(Button As CommandButton, _
	Picture As StdPicture)
	Dim dwStyle As Long
	Dim hImage As Long
	Dim ImageType As Long
	
	' Get the button style
	dwStyle = GetWindowLong(Button.hWnd, GWL_STYLE)
	
	If Picture Is Nothing Then
		' Clear the graphic style, add the text style
		dwStyle = (dwStyle Or BS_TEXT) And _
			Not (BS_BITMAP Or BS_ICON)
		If (dwStyle And BS_BITMAP) <> 0 Then
			Call SendMessage(Button.hWnd, _
				BM_SETIMAGE, IMAGE_BITMAP, 0&)
		ElseIf (dwStyle And BS_ICON) <> 0 Then
			Call SendMessage(Button.hWnd, _
				BM_SETIMAGE, IMAGE_ICON, 0&)
		End If
		
		' Update style bits & redraw
		SetWindowLong Button.hWnd, GWL_STYLE, dwStyle
		Button.Refresh
		
	Else
		' Remove mutually exclusive bits
		dwStyle = dwStyle And _
			Not (BS_BITMAP Or BS_ICON Or BS_TEXT)
		Select Case Picture.Type
		Case vbPicTypeIcon
			dwStyle = dwStyle Or BS_ICON
			ImageType = IMAGE_ICON
		Case vbPicTypeBitmap
			dwStyle = dwStyle Or BS_BITMAP
			ImageType = IMAGE_BITMAP
		End Select
		
		' Handle of image to attach to button.
		hImage = Picture.Handle
		' Change the style of the button
		Call SetWindowLong(Button.hWnd, GWL_STYLE, _
			dwStyle)
		' Add or remove the glyph
		Call SendMessage(Button.hWnd, BM_SETIMAGE, _
			ImageType, hImage)
	End If
End Sub
AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

workaround :-

Use a label instead of a button to avoid all the lengthy windows API calls.

and use RGB() to create color of your choice.

debasisdas
Posting Genius
6,968 posts since Feb 2007
Reputation Points: 722
Solved Threads: 457
Skill Endorsements: 20

>>You can, just with a lot of code....
hehe..i mean its not working with change a style..but can do it API..;)

Jx_Man
Senior Poster
3,521 posts since Nov 2007
Reputation Points: 1,468
Solved Threads: 514
Skill Endorsements: 64

You're kidding.

Just set the STYLE property to 1 and run the program. Then you can change all the colors you want via code. You don't need APIs.

Dim clr(4) As Long
Dim idx As Integer

Private Sub Command1_Click()
    Command1.BackColor = clr(idx)
    idx = idx + 1
    If idx = 4 Then idx = 0
End Sub

Private Sub Form_Load()
    clr(0) = &HFFFFFF
    clr(1) = &HFFFF00
    clr(2) = &HFF00FF
    clr(3) = &HFFFF&
    idx = 0
End Sub
WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

You're kidding.

Just set the STYLE property to 1 and run the program. Then you can change all the colors you want via code. You don't need APIs.

Yes you do because the style property is read only and can not be changed via code. Remember that the OP is loading the buttons during run time, no style property can be set.:)

I tested your code, not working, because the newly created com button style = 0, which can not be changed, unless you use api.

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

Ahhh, I see. I've never added controls that way. I create one control with an index of 0 and LOAD more as needed. Maybe that's the secret.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

Ran your code, waltp. said I can't assign a read-only value from code, though. thanks, still!

ben25x
Light Poster
41 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Ran your code, waltp. said I can't assign a read-only value from code, though. thanks, still!

My code doesn't assign anything to any read-only values. If it gave you that error, you changed my code.

I guess I should have pulled together all the data from the thread:

Make a form.
Add a command button to the form - Command1
Change it's style property to GRAPHICAL (1)
Add to the form code:

Dim clr(4) As Long
Dim idx As Integer
       
Private Sub Command1_Click()
    Command1.BackColor = clr(idx)
    idx = idx + 1
    If idx = 4 Then idx = 0
End Sub
       
Private Sub Form_Load()
    clr(0) = &HFFFFFF
    clr(1) = &HFFFF00
    clr(2) = &HFF00FF
    clr(3) = &HFFFF&
    idx = 0
End Sub

This code works as intended.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

If you read the root post of this thread carefully, soon you will realize the question was to create a command button at run time and then change the color property.

debasisdas
Posting Genius
6,968 posts since Feb 2007
Reputation Points: 722
Solved Threads: 457
Skill Endorsements: 20

I suppose Walts way is a nice "work around" if you do not want to get into the API stuff. You can always set the cmd1 button's visible property to false after all the code was run to load the others. It makes sense to me, although not exactly what the OP wanted.:)

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

I think waltp try from the other side, and he is also correct.
like andre said, if u don't want to get dizzy with api files then use waltp codes. :)

Jx_Man
Senior Poster
3,521 posts since Nov 2007
Reputation Points: 1,468
Solved Threads: 514
Skill Endorsements: 64

If you read the root post of this thread carefully, soon you will realize the question was to create a command button at run time and then change the color property.

And if you read the thread more carefully, you will see that people have said my code doesn't work. It does. I was pointing that out.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

I never said your code does not work, it works. I accept that.

You also need to accept the fact that your code is a workaround, and is not what the OP asked in this thread.

debasisdas
Posting Genius
6,968 posts since Feb 2007
Reputation Points: 722
Solved Threads: 457
Skill Endorsements: 20

your code is a workaround, and is not what the OP asked in this thread

Correct. The only way Walt's code will work is if the coder adds a command button on the form BEFORE run time, setting its style property to true.

The original question was how to change the color when new command buttons has been added during RUN time.:)

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

i don't really have a time limit on this, it's not an assignment, so if there's a source you know about i can read up on these complicated things you're telling me I have to do if i don't want a work-around, i am open for that kind of thing. I can create the buttons from code already, just need to change their color.

ben25x
Light Poster
41 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

i don't really have a time limit on this, it's not an assignment, so if there's a source you know about i can read up on these complicated things you're telling me I have to do if i don't want a work-around, i am open for that kind of thing. I can create the buttons from code already, just need to change their color.

You're kidding, right? AndreRet already gave you code to do it. What more do you want?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3497 seconds using 2.7MB