Button FlatStyle.System spacebar behavior

  • Thread starter Jason Kyle Baginski
  • Start date
J

Jason Kyle Baginski

Here's a little test app to demonstrate a problem I'm having.

It creates four buttons, each one with the different FlatStyle types
available. Three of them behave exactly the same way(and the way I'd
anticipate), but the FlatStyle.System one does not.

If you click on any of the buttons, use the enter key, or the
spacebar, they will bring up a messagebox that says "Activate".
Except the FlatStyle.System one, which will bring up _two_
messageboxes.

Why does FlatStyle.System fire twice, when all of the other
FlatStyle's only fire once?

Below is the simplest example to recreate the problem. For reference,
its been tested under XP Pro, XP Home, and 98(yes..98). All 1.1
framework. They all act the exact same way.

using System;
using System.Windows.Forms;

namespace ButtonStyleProblem
{
// create four buttons showing off each of the FlatStyle's
// If you tab between the different buttons, then hit the space bar
// all of the buttons will bring up a messagebox that says
"Activate".
// The problem is that the FlatStyle.System will bring up the
"Activate"
// messagebox _twice_. So its a behavior only with FlatStyle.System.
// The strangest thing is this test program behaves the same way
under XP and 98
// so its not an XP theme problem.
public class Form1 : System.Windows.Forms.Form
{
public Button buttonStandard;
public Button buttonSystem;
public Button buttonPopup;
public Button buttonFlat;

public Form1()
{
this.Width=240;
this.Height=80;

buttonStandard=new Button();
buttonStandard.FlatStyle=FlatStyle.Standard;
this.Controls.Add(buttonStandard);
buttonStandard.Left=0;
buttonStandard.Top=2;
buttonStandard.Width=110;
buttonStandard.Height=20;
buttonStandard.Text="FlatStyle.Standard";
buttonStandard.KeyDown+=new KeyEventHandler(OnButton);
buttonStandard.Click+=new EventHandler(OnButtonClick);

buttonSystem=new Button();
buttonSystem.FlatStyle=FlatStyle.System;
this.Controls.Add(buttonSystem);
buttonSystem.Left=115;
buttonSystem.Top=2;
buttonSystem.Width=110;
buttonSystem.Height=20;
buttonSystem.Text="FlatStyle.System";
buttonSystem.KeyDown+=new KeyEventHandler(OnButton);
buttonSystem.Click+=new EventHandler(OnButtonClick);

buttonPopup=new Button();
buttonPopup.FlatStyle=FlatStyle.Popup;
this.Controls.Add(buttonPopup);
buttonPopup.Left=0;
buttonPopup.Top=25;
buttonPopup.Width=110;
buttonPopup.Height=20;
buttonPopup.Text="FlatStyle.Popup";
buttonPopup.KeyDown+=new KeyEventHandler(OnButton);
buttonPopup.Click+=new EventHandler(OnButtonClick);

buttonFlat=new Button();
buttonFlat.FlatStyle=FlatStyle.Flat;
this.Controls.Add(buttonFlat);
buttonFlat.Left=115;
buttonFlat.Top=25;
buttonFlat.Width=110;
buttonFlat.Height=20;
buttonFlat.Text="FlatStyle.Flat";
buttonFlat.KeyDown+=new KeyEventHandler(OnButton);
buttonFlat.Click+=new EventHandler(OnButtonClick);
}
private void OnButtonClick(object sender,EventArgs e)
{
MessageBox.Show("Activate");
}

private void OnButton(object sender,KeyEventArgs e)
{
if(e.KeyData==Keys.Space || e.KeyData==Keys.Enter)
button.PerformClick();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
B

Brian Patterson

Unfortunately - this worked fine for me with the exception of this code:
private void OnButton(object sender,KeyEventArgs e)

{

if(e.KeyData==Keys.Space || e.KeyData==Keys.Enter)

button.PerformClick();

}



This doesn't work because you didn't define "button." --- can hyou clarify
what you are doing here and what is "PerformClick()"



Brian Patterson

Jason Kyle Baginski said:
Here's a little test app to demonstrate a problem I'm having.

It creates four buttons, each one with the different FlatStyle types
available. Three of them behave exactly the same way(and the way I'd
anticipate), but the FlatStyle.System one does not.

If you click on any of the buttons, use the enter key, or the
spacebar, they will bring up a messagebox that says "Activate".
Except the FlatStyle.System one, which will bring up _two_
messageboxes.

Why does FlatStyle.System fire twice, when all of the other
FlatStyle's only fire once?

Below is the simplest example to recreate the problem. For reference,
its been tested under XP Pro, XP Home, and 98(yes..98). All 1.1
framework. They all act the exact same way.

using System;
using System.Windows.Forms;

namespace ButtonStyleProblem
{
// create four buttons showing off each of the FlatStyle's
// If you tab between the different buttons, then hit the space bar
// all of the buttons will bring up a messagebox that says
"Activate".
// The problem is that the FlatStyle.System will bring up the
"Activate"
// messagebox _twice_. So its a behavior only with FlatStyle.System.
// The strangest thing is this test program behaves the same way
under XP and 98
// so its not an XP theme problem.
public class Form1 : System.Windows.Forms.Form
{
public Button buttonStandard;
public Button buttonSystem;
public Button buttonPopup;
public Button buttonFlat;

public Form1()
{
this.Width=240;
this.Height=80;

buttonStandard=new Button();
buttonStandard.FlatStyle=FlatStyle.Standard;
this.Controls.Add(buttonStandard);
buttonStandard.Left=0;
buttonStandard.Top=2;
buttonStandard.Width=110;
buttonStandard.Height=20;
buttonStandard.Text="FlatStyle.Standard";
buttonStandard.KeyDown+=new KeyEventHandler(OnButton);
buttonStandard.Click+=new EventHandler(OnButtonClick);

buttonSystem=new Button();
buttonSystem.FlatStyle=FlatStyle.System;
this.Controls.Add(buttonSystem);
buttonSystem.Left=115;
buttonSystem.Top=2;
buttonSystem.Width=110;
buttonSystem.Height=20;
buttonSystem.Text="FlatStyle.System";
buttonSystem.KeyDown+=new KeyEventHandler(OnButton);
buttonSystem.Click+=new EventHandler(OnButtonClick);

buttonPopup=new Button();
buttonPopup.FlatStyle=FlatStyle.Popup;
this.Controls.Add(buttonPopup);
buttonPopup.Left=0;
buttonPopup.Top=25;
buttonPopup.Width=110;
buttonPopup.Height=20;
buttonPopup.Text="FlatStyle.Popup";
buttonPopup.KeyDown+=new KeyEventHandler(OnButton);
buttonPopup.Click+=new EventHandler(OnButtonClick);

buttonFlat=new Button();
buttonFlat.FlatStyle=FlatStyle.Flat;
this.Controls.Add(buttonFlat);
buttonFlat.Left=115;
buttonFlat.Top=25;
buttonFlat.Width=110;
buttonFlat.Height=20;
buttonFlat.Text="FlatStyle.Flat";
buttonFlat.KeyDown+=new KeyEventHandler(OnButton);
buttonFlat.Click+=new EventHandler(OnButtonClick);
}
private void OnButtonClick(object sender,EventArgs e)
{
MessageBox.Show("Activate");
}

private void OnButton(object sender,KeyEventArgs e)
{
if(e.KeyData==Keys.Space || e.KeyData==Keys.Enter)
button.PerformClick();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
A

Alien

PerformClick() is a public method defined in IButtonControl which is called
automatically when you press enter... To make that line compile, change
button.PerformClick(); to ((Button)sender).PerformClick();

But, since PerformClick() is already called automatically when you press
enter, calling that method manually, as you do, will of course result in it
being called twice.

I'm not sure why you have a handler for the KeyDown event, since pressing
space or enter on a focused button will raise its Click event without any
additional coding. You basically have 2 event handlers calling the same
method.

All you need to do is create a handler only for the Click event and all will
be well...


Brian Patterson said:
Unfortunately - this worked fine for me with the exception of this code:
private void OnButton(object sender,KeyEventArgs e)

{

if(e.KeyData==Keys.Space || e.KeyData==Keys.Enter)

button.PerformClick();

}



This doesn't work because you didn't define "button." --- can hyou clarify
what you are doing here and what is "PerformClick()"



Brian Patterson

Jason Kyle Baginski said:
Here's a little test app to demonstrate a problem I'm having.

It creates four buttons, each one with the different FlatStyle types
available. Three of them behave exactly the same way(and the way I'd
anticipate), but the FlatStyle.System one does not.

If you click on any of the buttons, use the enter key, or the
spacebar, they will bring up a messagebox that says "Activate".
Except the FlatStyle.System one, which will bring up _two_
messageboxes.

Why does FlatStyle.System fire twice, when all of the other
FlatStyle's only fire once?

Below is the simplest example to recreate the problem. For reference,
its been tested under XP Pro, XP Home, and 98(yes..98). All 1.1
framework. They all act the exact same way.

using System;
using System.Windows.Forms;

namespace ButtonStyleProblem
{
// create four buttons showing off each of the FlatStyle's
// If you tab between the different buttons, then hit the space bar
// all of the buttons will bring up a messagebox that says
"Activate".
// The problem is that the FlatStyle.System will bring up the
"Activate"
// messagebox _twice_. So its a behavior only with FlatStyle.System.
// The strangest thing is this test program behaves the same way
under XP and 98
// so its not an XP theme problem.
public class Form1 : System.Windows.Forms.Form
{
public Button buttonStandard;
public Button buttonSystem;
public Button buttonPopup;
public Button buttonFlat;

public Form1()
{
this.Width=240;
this.Height=80;

buttonStandard=new Button();
buttonStandard.FlatStyle=FlatStyle.Standard;
this.Controls.Add(buttonStandard);
buttonStandard.Left=0;
buttonStandard.Top=2;
buttonStandard.Width=110;
buttonStandard.Height=20;
buttonStandard.Text="FlatStyle.Standard";
buttonStandard.KeyDown+=new KeyEventHandler(OnButton);
buttonStandard.Click+=new EventHandler(OnButtonClick);

buttonSystem=new Button();
buttonSystem.FlatStyle=FlatStyle.System;
this.Controls.Add(buttonSystem);
buttonSystem.Left=115;
buttonSystem.Top=2;
buttonSystem.Width=110;
buttonSystem.Height=20;
buttonSystem.Text="FlatStyle.System";
buttonSystem.KeyDown+=new KeyEventHandler(OnButton);
buttonSystem.Click+=new EventHandler(OnButtonClick);

buttonPopup=new Button();
buttonPopup.FlatStyle=FlatStyle.Popup;
this.Controls.Add(buttonPopup);
buttonPopup.Left=0;
buttonPopup.Top=25;
buttonPopup.Width=110;
buttonPopup.Height=20;
buttonPopup.Text="FlatStyle.Popup";
buttonPopup.KeyDown+=new KeyEventHandler(OnButton);
buttonPopup.Click+=new EventHandler(OnButtonClick);

buttonFlat=new Button();
buttonFlat.FlatStyle=FlatStyle.Flat;
this.Controls.Add(buttonFlat);
buttonFlat.Left=115;
buttonFlat.Top=25;
buttonFlat.Width=110;
buttonFlat.Height=20;
buttonFlat.Text="FlatStyle.Flat";
buttonFlat.KeyDown+=new KeyEventHandler(OnButton);
buttonFlat.Click+=new EventHandler(OnButtonClick);
}
private void OnButtonClick(object sender,EventArgs e)
{
MessageBox.Show("Activate");
}

private void OnButton(object sender,KeyEventArgs e)
{
if(e.KeyData==Keys.Space || e.KeyData==Keys.Enter)
button.PerformClick();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
J

Jason Kyle Baginski

I'm not sure why you have a handler for the KeyDown event, since pressing
space or enter on a focused button will raise its Click event without any
additional coding. You basically have 2 event handlers calling the same
method.

This was the simplest example code to generate the behavior I was
experiencing with a much more complicated DataGrid custom column where
I'm intercepting keyboard behavior. My code had worked fine until I
put FlatStyle.System in for the button. So this is the test app.

Why do all of the other button flatstyle's give the desired action(one
text box), but FlatStyle.System(XP or 98) fires it twice, while all
the other buttons that are written the exact same way, fire once? I
know the example code is ugly, but it's to demonstrate odd behavior
for setting one attribute that should have nothing to do with events.
 

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