how do you make a button stay depressed

P

Peted

take away its prozac ?


no seriously


I want to drop a button onto a form, then during runtime, when user
clicks the button it stays in the "depressed" image state (with a new
color) then when it is pressed again it pops up to its normal state
and color


and of course i want to execute code on each push down and pop up

any idea how this can be done please.


Also a way to make an array of such depresseable buttons, that
function like an array of radio buttons in that only 1 on the panel is
ever selected at any one time


thanks for any advice

Peted
 
C

ClayB

One way os to use the ControlPaint class and draw the button yourself.

public class ToggleButton : Button
{
public ToggleButton()
{
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs pevent)
{
// base.OnPaint(pevent);
ControlPaint.DrawButton(pevent.Graphics,
this.ClientRectangle, toggleState);
using (Brush b = new SolidBrush(ForeColor))
{
SizeF sz = pevent.Graphics.MeasureString(Text, Font);
float x = Math.Max(2, (ClientSize.Width - sz.Width) /
2);
float y = Math.Max(2, (ClientSize.Height -
sz.Height) / 2);
pevent.Graphics.DrawString(Text, Font, b, x, y); //
draw the text on the button
}
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
toggleState = toggleState == ButtonState.Normal ?
ButtonState.Pushed : ButtonState.Normal;
this.Invalidate();
}
ButtonState toggleState = ButtonState.Normal;
}
===================
Clay Burch
Syncfusion, Inc.
 
S

Sericinus hunter

Peted said:
take away its prozac ?

no seriously

I want to drop a button onto a form, then during runtime, when user
clicks the button it stays in the "depressed" image state (with a new
color) then when it is pressed again it pops up to its normal state
and color

and of course i want to execute code on each push down and pop up

any idea how this can be done please.

This seems like check box functionality. Why not to use checkbox
instead with its Appearance property set to Button?
Also a way to make an array of such depresseable buttons, that
function like an array of radio buttons in that only 1 on the panel is
ever selected at any one time

Same thing: radio button has Appearance property which can be set to
Button making it look like push button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top