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

Clicking button keeps it pressed down 1

Status
Not open for further replies.

spencern

Programmer
Dec 20, 2001
78
US
Hi,
I'm new to Visual Basic and I'm just trying out some simple ideas I have for easy programs. I was wondering if any Visual Basic wizard could answer this.

Is there any way to make a command button stay pressed down when you click it and then pop back up when click it again? Like a toggle switch sort of. Better yet, when you click down change the text on the button to something different, or would this be some extra coding (Not a problem, just wondering)

Thanks a lot for your time,
Spencer
 
try to use Windows Common Control in Components
use Toolbar
 
I would toggle a bit map (press.bmp) or (notpressed.bmp) when clicking.
 
Hi Spencern,

certainly you can change text on a command button when you press it. On the keypress event you just need to add code to changed the button of the caption.

cmdButton.caption = "New Text"

For a toggle you may need to put in a check

If cmdButton.caption = "Old Text" then
cmdButton.caption = "New Text"
else
cmdButton.caption = "Old Text"
end if

This code will make the text toggle between "Old Text" and "New Text"

As for having the button stay down I can't be sure.
There may be other active x controls out there with this property though. Happy hunting

Transcend
 

Changing the value of the caption of a command button is easy.
[tt]
Option Explicit
Dim Test As Boolean

Private Sub Command1_Click()

If Test = False Then
Test = True
Command1.Caption = "True"
Else
Test = False
Command1.Caption = "False"
End If

End Sub
[/tt]

Now on the other had to keep a command button pressed is not an easy task, but what would be easy is to use a check box and set its style to graphical in the properties window. Once you have place a checkbox on your form and changed its style property is would be very easy to alter the code above to meet your needs.
[tt]
Option Explicit

Private Sub Check1_Click()

If Check1.Value = vbChecked Then
Check1.Caption = "Checked"
Else
Check1.Caption = "Unchecked"
End If

End Sub
[/tt]

Notice that in this second example a boolean variable is not needed since the status/value of the checkbox/button can be referenced.

I would suggest a beginners book on VB that will give you simple projects that you can practice with.

I hope this helps, Good Luck

 
One very easy way to accomplish this is to use the CheckBox control.

Add a CheckBox to the form, and be sure to set its style to Graphical and use the CheckBox Click event just as you would use a CommandButton click event. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Also, the Microsoft Forms 2.0 Object Library contains a ToggleButton control that will behave like that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top