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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the color of two cmd buttons when a certain key is pressed 4

Status
Not open for further replies.

thermalman

Technical User
Aug 26, 2003
89
GB
Hi,
I have a keyboard test program that has a listbox and when you press any keyboard key it's hex value is converted to a VK code and shows in the listbox. I have now added a lot of command buttons to represent the different keys on the keyboard but when i try to link say the 2 shift keys i get an error.
here is a copy of the coding

Case &H10: szRetval = "VK_SHIFT": cmdRShift.BackColor = vbGreen

I have tried
Case &H10: szRetval = "VK_SHIFT": cmdRShift & cmdLShift.BackColor = vbGreen

but it errors every time.

I want it so that when I press either of the shift keys both command buttons change color but I am not quite sure how to join the statement so that it sets the color of both cmd buttons at the same time

Can anybody help?
 
Try this:

Code:
Case &H10: szRetval = "VK_SHIFT"
    cmdRShift.BackColor = vbGreen
    cmdLShift.BackColor = vbGreen
Case...
 

You do know that - in order to change the BackColor property of a command button - you have to set its Style property to 1 - Graphical (the default is 0 - Standard, shich will not allow you to change the BackColor)

Have fun.

---- Andy
 
Hello jges and Andrzejek,

Many Thanks for the posts,

firstly yes my cmd buttons are set to graphical and for most of the buttons i have no problem.
I only encounter a problem when I want to reference 2 cmd buttons at the same time. i.e. L Shift, R Shift, L Control and R Control.
I have tried jges coding and I am getting a compile error - syntax error when running the coding.
here is what i have put

Case &H10: szRetval = "VK_SHIFT": cmdLShift.BackColor = vbGreen cmdRShift.BackColor = vbGreen

if i remove one of the cmd statements it works but it does not work when i try referencing both at the same time. I am sure there is a way of using both statements on the same line, isn't there???
 
jges' code should not give an error, so you would have to show how you implemented it to see where the error is.

In your code, you have to separate all statements with a colon:
Code:
Case &H10: szRetval = "VK_SHIFT": cmdLShift.BackColor = vbGreen[highlight][b]:[/b][/highlight]cmdRShift.BackColor = vbGreen
 
Hello guitarzan

Many Thanks for that, I missed the colon as it was not included in jges reply. I knew that it needed a colon in the front but did not realise that it needed another to join two statements. My Keyboard Test program now work a treat.

Many Thanks to you, jges and Andrezejek
 

jges' sample did not have a colon because the code is on separate lines - I would even write it like this:
Code:
Case &H10
    szRetval = "VK_SHIFT"
    cmdRShift.BackColor = vbGreen
    cmdLShift.BackColor = vbGreen
Case...
You have the same written this way:
Code:
Case &H10: szRetval = "VK_SHIFT": cmdLShift.BackColor = vbGreen:cmdRShift.BackColor = vbGreen
Case...
And - IMHO - is more difficult to read, debug and see the logic.

Have fun.

---- Andy
 
Hello Andrezejek,

Can you tell me if it is also possible to use a blanket command to put the ALL command button colors back to default.
I have used the command above and it works well but I also have a clear button which clears all of my listbox but I have a lot of command buttons that represent the keys and after setting them to vbGreen I want it to reset them all back to default when i press the clear button.
Is this possible do you know?

Many Thanks
Thermalman
 
Are they in an array?
[tt]
Dim LoopCounter As Integer
For LoopCounter = 0 To Command1.Count - 1
Command1(LoopCounter).BackColor = vbButtonFace
Next LoopCounter
[/tt]

Not an array?
[tt]
Dim C As Control
For Each C In Me.Controls
If TypeOf C Is CommandButton Then C.BackColor = vbButtonFace
Next C
[/tt]




Good Luck

 
Hello Vb5prgrmr,


Many Thanks that worked a treat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top