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

Disabling a Command button 1

Status
Not open for further replies.

Perilous1

IS-IT--Management
Mar 10, 2005
171
0
0
US
Is there a way to Prevent user interaction with a Command Button without the "graying out" visual effect on the Form that Enabled = False gives you?
 

Try placing the buuton in a Frame (or some other Container) and disable that.
 
I don't have much experience using Frames. Is there something more to placing the Button in the Frame than clicking and dragging it on top of the Frame? If not, it doesn't seem to work.
 
If the button is in the frame it will move around when you move the Frame. If not right click the button select Cut, give the Frame focus right click it and select Paste.

 

If you want to place a command button on the frame, click on botton, cut it, click on a frame, and paste it (button). To make sure you button is on the frame, and not straight on the Form, select frame, and do Crtl-Arrow key to move the frame. If your button moves with the frame, you got it right.

You can set frame's BorderStyle property to None to 'hide' the frame from your user.

Have fun.

---- Andy
 
Very cool. A Frame did exactly what I needed it to. The only issue I see now is that when selecting the identical background color for the Frame that the Form itself has, the Frame's color is shaded a bit lighter. I am not sure why this is happening.

Aside from that though, thank you very much for this answer.
 

You may size your Frame to be the same as your button on it. The best way would be in your ocde, so in design you will still see the frame.

But aside from that - do you think this is a good idea to show your users command button that looks like it would take a click, but it will not? And there is no indication to the user they should not be clicking on it? It is kind of miss-leading....


Have fun.

---- Andy
 
This form is a card table with four players. When it is not that player's turn I want the board locked down except for that player's own cards.

Its a networked game and the clients report back to a server that controls cards being dealt, which turn it is, etc.

This process worked by disabling the three other players cards, but as I described it greyed out the color of the cardbacks. This is by far a more visually pleasing solution.
 

...or you can give the player a message when he clicks the button when he should not, something like:
Code:
Private Sub cmdPlayer_Click()

If SomeCondition Then
    MsgBox "It is NOT your turn, you ^&%#"
    Exit Sub
End If

....[green]'Your regular code goes here
[/green]
End Sub

No dis-able needed :)

Have fun.

---- Andy
 
I guess my goal here is to avoid unnecessary clicking on the Form. Preventing Buttons from depressing discourages clicking on them for no reason.

I appreciate the advice though. I've literally learned all of my VB6 from these forums [smile]
 
In conjunction with andrzejek's use of code you could have a different picture (like a scowling face)appearing only when you depress the button when you have inhibited it.
Code:
If SomeCondition Then
  Set Command1.DownPicture = LoadPicture("C:\Graphics\scowlingface.jpg")
else
  Set Command1.DownPicture = LoadPicture("C:\Graphics\NormalPicture.jpg") 'or a different pic again
exit sub
end if
 
I really wouldn't mess about with the UI standards unless you ahve got a VERY good reason to do so.

In this case it seems to me like you are using the buttons as pictures (of the cards), so that each card is represented by a button. Has it occurred to you to use a picturebox instead? (and, of course, if your button doesn't actually look like a button then the user won't know you are breaking the rules, so a button could be fine)

 
Sorry I meant

Code:
If SomeCondition Then
  Set Command1.DownPicture = LoadPicture("C:\Graphics\scowlingface.jpg")
  Exit sub
else
  Set Command1.DownPicture = LoadPicture("C:\Graphics\NormalPicture.jpg") 'or a different pic again
   '... normal code here
end if

Your button will still show as depressed when you click on it in either case only you get as different picture.

In the case of using pictureboxes, you need the edges of picture for the box with "3D edges" if you want that to happen.

Also if you use a form for a button you can have any weird shape "button" you like.
You make a windowless form the size of the button
The following code is the only code in this little form
Put a picture of the button shape in the form with only the parts you want invisible being black (Color 0).Eg for a yellow circle button have the yellow circle on a black form.

Place the button form on your main form
Code:
'courtesy of various contributors to Tek-tips
Option Explicit
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 SetLayeredWindowAttributes Lib "user32.dll" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2&
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000

Private Sub Form_Load()
Dim dwExStyle As Long
Dim Success As Integer
' Set up a transparent window
dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
dwExStyle = dwExStyle Or WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, dwExStyle
BackColor = RGB(0, 0, 0)   ' this is what will be transparant
SetLayeredWindowAttributes hWnd, RGB(0, 0, 0), 0, LWA_COLORKEY
Success = SetWindowPos(frmIndicator.hWnd, -1, 0, 0, 0, 0, 1) 'always on top of previous loaded forms or applications so it doesn't dissappear when you click on the form
End Sub
You can make the "button" change by changing the form picture as above.
 
>What rule am I breaking, exactly?

That a command buton must be greyed out when it is disabled.

I suspoect you are not even aware that Windows has some very specific guidelines as to how the GUI looks and responds to user input. Those guidelines are there so that users get a consistent experience, and can apply knowledge learned in one application to another application.

In other words once I've learned how a button works I can expect it to ALWAYS work like that.

Here's one of the first guidelines in the current interface document about disabling ...

Microsoft said:
Enabled is better than disabled. Disabled controls are often confusing, so use them only when users can easily deduce why the control is disabled. Otherwise, remove the control if it doesn't apply or leave it enabled and give helpful feedback.
 
While it is important to maintain a standard MS style UI in business applications a more little creativity may be acceptable in a game.
Currently I understand you have 4 buttons set to Graphical style with pictures of playing cards on them and they are each placed within 4 Frames. So currently when a button (actually its Container Frame) is disabled it looks the same as it does when it is enabled. To get around that;

Size the buttons so that they are just a little larger than the playing card picture/ image; vb6 is good enough to keep the picture centred on the button so a thin border will appear all round it. Then you can indicate if the button is enabled by adjusting the color of the border using code like;

Private Sub Command2_Click()

Frame1.Enabled = Not Frame1.Enabled
If Frame1.Enabled Then
Command1.BackColor = vbGreen
Else
Command1.BackColor = vbRed
End If

End Sub
 
As I said earlier, if it does not actually look like a button then it doesn't matter if it does not act like one (although one then has to question why a button control is being used, as there may be better suited controls that don't need a bunch of workarounds to achieve the desired result)
 
There is no reason for a player to touch another player's cards in this game, much the same way it would be unthinkable to reach across a card table and touch an opponent's cards. I believe that realism in the game takes precedence over UI standards. As I said before I also want to discourage the needless clicking of controls on the client, also something I place higher in precedence than UI standards.
 
And I'll repeat, since you seem to be missing the point, that there's no need to use a button when you don't actually want a button's behaviour.

>discourage the needless clicking of controls

That's one of the reasons Windows has a style guide. Let me ask a question: if you see a button that is not disabled would you assume that it is enabled, and thus be tempted to try clicking it? If your answer is 'no' then I suggest that you are deceiving yourself. If the answer to this is yes, then your goal of reducing clicking has NOT been achieved plus you've confused your users. If you then argue "but in my interface they don't look like buttons, they look like cards, and I don't want them behaving like buttons" then I return to my first point: why use a button control?
 
I use a button control because I want the user to experience the tactile sensation of depressing a button and having it execute a function for them. When a user visually witnesses a button being pressed when they click their mouse they are far less likely to click a button more than once out of impatience. I didn't choose to use Buttons over another control, such as PictureBox, arbitralily as you seem to have assumed. I chose them because they offered the experience I want my users to have while playing the game.

I will go ahead and repeat since you do seem to be missing my point that the only "active" cards on the board are directly in front of the user (bottom). The other three sets are arrayed around the board (Left, Across & Right) and those are contained within a Frame that is disabled. There is no reason to click on those cards while there is every reason to click on their own.

Its clear you disapprove of using Buttons in this fashion and its clear you disapprove of disabling buttons for any reason. Nevertheless, that is what is necessary to provide the experience I desire my users to have while playing this game.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top