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.

Recommended Answers

All 18 Replies

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

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.

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
commented: WinAPI - The secret weapon :) +12

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.

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

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

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.

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.

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

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.

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.

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

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

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.

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.

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

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.