1,105,450 Community Members

Change Color of Command Buttons

Member Avatar
ben25x
Light Poster
45 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
1
 

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
Member Avatar
debasisdas
Posting Genius
6,542 posts since Feb 2007
Reputation Points: 580 [?]
Q&As Helped to Solve: 476 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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.

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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
Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
ben25x
Light Poster
45 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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.

Member Avatar
debasisdas
Posting Genius
6,542 posts since Feb 2007
Reputation Points: 580 [?]
Q&As Helped to Solve: 476 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.:)

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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. :)

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
debasisdas
Posting Genius
6,542 posts since Feb 2007
Reputation Points: 580 [?]
Q&As Helped to Solve: 476 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.:)

Member Avatar
ben25x
Light Poster
45 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: