Hello all,

Im using below code to change the command button icon at run time. When the form is loaded button's icon will be showing but again icon will disappear when focus is changed to other controls on the form. Icon will be shown whenever focus is on that button.

Is there any other way to set button icon at runtime??

Thanks in advance

Private Const GWL_STYLE& = (-16)
Private Const Graphical_Style As Long = (1409351691)

Private Sub Form_Load()

Dim lWindowStyle As Long
lWindowStyle = SetWindowLong(Command2.hwnd, GWL_STYLE, Graphical_Style)
Command2.Picture = LoadPicture("D:\Icons\Po.ico")

End Sub

Recommended Answers

All 9 Replies

There is a much easier way of doing this -

Private Sub Command1_GotFocus()

Command1.Picture = LoadPicture("D:\Icons\Po.ico")
End Sub

Private Sub Command1_LostFocus()

Command1.Picture = Nothing
End Sub

Private Sub Form_Load()

Command1.Picture = LoadPicture("D:\Icons\Po.ico")
End Sub

Just use you got/lost focus subs.

Thanks Andre :)

But no need to implement Command1_GotFocus() also. When the focus is set to button again, the icon will be shown automatically.

With the code mentioned, suppose i have 2 buttons command1 and command2 on form, after loading it will show icon for command2. Click on command2 then command1, icon will disapper from command2. Again click on command2 icon will come :(

Actually my application allows user to configure as many buttons as he wants( Button properties like Button icon, Button back color etc., also he can select). Based on his selection, application UI will be changed to show buttons with selected properties. In that case i can not do as u suggested.

Actual thing i need to do is :
I have create buttons dynamically and set button's style to Graphical so that i can set button icon and button back color [ I cannot use Command1.Style bec it is readonly]. So im using SetWindowLong() . But its not working properly.

Might be im wrong in giving style ie., 3rd parameter to SetWindowLong.

Don't know what happened to the other 2 links. Anyway, attached something to do with pic and text alignment done some time ago. Try out as well.

hi...

did you get any solution for this issue..
Would be appreciative if you reply...

hi...

did you get any solution for this issue..
Would be appreciative if you reply...

ratheeshpranava,

It is not nice to raise the dead... You might be labeled a necrophiliac, necromonger, or a necromancer...

As for your question the button will need to have its style set to graphical and then you can use command1.picture = loadpicture(pathtopic) to load your graphic (you might need to use the set keyword).

Good Luck

hi...

did you get any solution for this issue..
Would be appreciative if you reply...

Hi ratheesh,

There is no way to set the button style to Graphic at run time. Even if you use "SetWindowLong", it will not work properly :(

So for my project, i created one custom control which internally makes use of vb command button with graphical style set.

Wherever i need to create VB command button dynamically, im creating my custom button control. Im providing all button properties to my custom control also so that i can set button properties through custom control.

Now im able to set icon/picture to button not directly but through custom control. :)

P.S Thanks to Andre a lot for his help even though i couln't use what he has suggested.

Hi ratheesh,

There is no way to set the button style to Graphic at run time. Even if you use "SetWindowLong", it will not work properly :(

So for my project, i created one custom control which internally makes use of vb command button with graphical style set.

Wherever i need to create VB command button dynamically, im creating my custom button control. Im providing all button properties to my custom control also so that i can set button properties through custom control.

Now im able to set icon/picture to button not directly but through custom control. :)

P.S Thanks to Andre a lot for his help even though i couln't use what he has suggested.

Thanks... thanks a lot...

I know this OCX method.. but mine is an huge application.. more than 800 forms....so each form I need to a new OCX...

any way thanks... better let think some class file... rt?..

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.