Borderless Button

M

Martijn Mulder

I want my buttons to look like and behave like the 'Tools' in, for example,
MSPaint. The border of the button is invisible, the image just 'hangs' in
the gray background. When the mouse pointer is moved over the button, the
border shows. Clicking the button first indents the picture and on releasing
the mouse button the selection is visible with a dark border and a lighter
shade of gray as background color.

How can I achieve this?
 
D

David Boucherie & Co

Hey Martijn...


If I understand correctly what you try to achieve:

- Don't use a Button control, but a CheckBox control.

- At design time, set the following properties:
-- Appearance = Button
-- FlatAppearance.BorderColor = whatever you like
-- FlatAppearance.BorderSize = 0
-- FlatAppearance.MouseDownBackColor = whatever you like
-- FlatAppearance.MouseOverBackColor = whatever you like
-- FlatStyle = Flat

At runtime you can catch the MouseEnter and MouseLeave events if you
like, to display a border when necessary. And catch the CheckedChanged
event (for example) to keep the border around when the button is pressed
(checked).


private void checkBox1_CheckedChanged( object sender, EventArgs e )
{
checkBox1.FlatAppearance.BorderSize =
(( sender as CheckBox ).Checked ? 1 : 0);
}

private void checkBox1_MouseEnter( object sender, EventArgs e )
{
checkBox1.FlatAppearance.BorderSize = 1;
}

private void checkBox1_MouseLeave( object sender, EventArgs e )
{
checkBox1.FlatAppearance.BorderSize =
( ( sender as CheckBox ).Checked ? 1 : 0 );
}


Of course, you can finetune this to your liking.

Have fun.

David
 
S

Stoitcho Goutsev \(100\)

Martijn,

The button FlatStyle property can be set to Popup that has the behaviour you
are after. However there is one small problem that the button shows border
evetn when the mouse is not over it. I haven't try to work around this
problem and I think that it might be possible removing the border. I don't
know though.

The behavior that you described was implemented in the old ToolBar
component. It is still there and you can use it, but you need to install it
manually in the toolbox. I tried it and it works as you want with the small
problem that the 3D border that shows up when the mouse hovers over the
button has rounded corners.
 

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