Button FlatStyle.System spacebar behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Kyle Baginski

    #1

    Button FlatStyle.System spacebar behavior

    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.Syste m 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.Syste m one, which will bring up _two_
    messageboxes.

    Why does FlatStyle.Syste m 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 ButtonStyleProb lem
    {
    // 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.Syste m will bring up the
    "Activate"
    // messagebox _twice_. So its a behavior only with FlatStyle.Syste m.
    // 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=FlatS tyle.Standard;
    this.Controls.A dd(buttonStanda rd);
    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(On ButtonClick);

    buttonSystem=ne w Button();
    buttonSystem.Fl atStyle=FlatSty le.System;
    this.Controls.A dd(buttonSystem );
    buttonSystem.Le ft=115;
    buttonSystem.To p=2;
    buttonSystem.Wi dth=110;
    buttonSystem.He ight=20;
    buttonSystem.Te xt="FlatStyle.S ystem";
    buttonSystem.Ke yDown+=new KeyEventHandler (OnButton);
    buttonSystem.Cl ick+=new EventHandler(On ButtonClick);

    buttonPopup=new Button();
    buttonPopup.Fla tStyle=FlatStyl e.Popup;
    this.Controls.A dd(buttonPopup) ;
    buttonPopup.Lef t=0;
    buttonPopup.Top =25;
    buttonPopup.Wid th=110;
    buttonPopup.Hei ght=20;
    buttonPopup.Tex t="FlatStyle.Po pup";
    buttonPopup.Key Down+=new KeyEventHandler (OnButton);
    buttonPopup.Cli ck+=new EventHandler(On ButtonClick);

    buttonFlat=new Button();
    buttonFlat.Flat Style=FlatStyle .Flat;
    this.Controls.A dd(buttonFlat);
    buttonFlat.Left =115;
    buttonFlat.Top= 25;
    buttonFlat.Widt h=110;
    buttonFlat.Heig ht=20;
    buttonFlat.Text ="FlatStyle.Fla t";
    buttonFlat.KeyD own+=new KeyEventHandler (OnButton);
    buttonFlat.Clic k+=new EventHandler(On ButtonClick);
    }
    private void OnButtonClick(o bject sender,EventArg s e)
    {
    MessageBox.Show ("Activate") ;
    }

    private void OnButton(object sender,KeyEvent Args e)
    {
    if(e.KeyData==K eys.Space || e.KeyData==Keys .Enter)
    button.PerformC lick();
    }
    [STAThread]
    static void Main()
    {
    Application.Run (new Form1());
    }
    }
    }
  • Brian Patterson

    #2
    Re: Button FlatStyle.Syste m spacebar behavior

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

    {

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

    button.PerformC lick();

    }



    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" <nx-2000@winvoice.c om> wrote in message
    news:bd302d68.0 307291234.6b1cc 779@posting.goo gle.com...[color=blue]
    > 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.Syste m 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.Syste m one, which will bring up _two_
    > messageboxes.
    >
    > Why does FlatStyle.Syste m 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 ButtonStyleProb lem
    > {
    > // 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.Syste m will bring up the
    > "Activate"
    > // messagebox _twice_. So its a behavior only with FlatStyle.Syste m.
    > // 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=FlatS tyle.Standard;
    > this.Controls.A dd(buttonStanda rd);
    > 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(On ButtonClick);
    >
    > buttonSystem=ne w Button();
    > buttonSystem.Fl atStyle=FlatSty le.System;
    > this.Controls.A dd(buttonSystem );
    > buttonSystem.Le ft=115;
    > buttonSystem.To p=2;
    > buttonSystem.Wi dth=110;
    > buttonSystem.He ight=20;
    > buttonSystem.Te xt="FlatStyle.S ystem";
    > buttonSystem.Ke yDown+=new KeyEventHandler (OnButton);
    > buttonSystem.Cl ick+=new EventHandler(On ButtonClick);
    >
    > buttonPopup=new Button();
    > buttonPopup.Fla tStyle=FlatStyl e.Popup;
    > this.Controls.A dd(buttonPopup) ;
    > buttonPopup.Lef t=0;
    > buttonPopup.Top =25;
    > buttonPopup.Wid th=110;
    > buttonPopup.Hei ght=20;
    > buttonPopup.Tex t="FlatStyle.Po pup";
    > buttonPopup.Key Down+=new KeyEventHandler (OnButton);
    > buttonPopup.Cli ck+=new EventHandler(On ButtonClick);
    >
    > buttonFlat=new Button();
    > buttonFlat.Flat Style=FlatStyle .Flat;
    > this.Controls.A dd(buttonFlat);
    > buttonFlat.Left =115;
    > buttonFlat.Top= 25;
    > buttonFlat.Widt h=110;
    > buttonFlat.Heig ht=20;
    > buttonFlat.Text ="FlatStyle.Fla t";
    > buttonFlat.KeyD own+=new KeyEventHandler (OnButton);
    > buttonFlat.Clic k+=new EventHandler(On ButtonClick);
    > }
    > private void OnButtonClick(o bject sender,EventArg s e)
    > {
    > MessageBox.Show ("Activate") ;
    > }
    >
    > private void OnButton(object sender,KeyEvent Args e)
    > {
    > if(e.KeyData==K eys.Space || e.KeyData==Keys .Enter)
    > button.PerformC lick();
    > }
    > [STAThread]
    > static void Main()
    > {
    > Application.Run (new Form1());
    > }
    > }
    > }[/color]


    Comment

    • Alien

      #3
      Re: Button FlatStyle.Syste m spacebar behavior

      PerformClick() is a public method defined in IButtonControl which is called
      automatically when you press enter... To make that line compile, change
      button.PerformC lick(); 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" <GOAWAYbriandpa tterson@IHATESP AMmchsi.com.NOS PAM> wrote in
      message news:Oqn02tkVDH A.2272@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Unfortunately - this worked fine for me with the exception of this code:
      > private void OnButton(object sender,KeyEvent Args e)
      >
      > {
      >
      > if(e.KeyData==K eys.Space || e.KeyData==Keys .Enter)
      >
      > button.PerformC lick();
      >
      > }
      >
      >
      >
      > 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" <nx-2000@winvoice.c om> wrote in message
      > news:bd302d68.0 307291234.6b1cc 779@posting.goo gle.com...[color=green]
      > > 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.Syste m 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.Syste m one, which will bring up _two_
      > > messageboxes.
      > >
      > > Why does FlatStyle.Syste m 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 ButtonStyleProb lem
      > > {
      > > // 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.Syste m will bring up the
      > > "Activate"
      > > // messagebox _twice_. So its a behavior only with FlatStyle.Syste m.
      > > // 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=FlatS tyle.Standard;
      > > this.Controls.A dd(buttonStanda rd);
      > > 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(On ButtonClick);
      > >
      > > buttonSystem=ne w Button();
      > > buttonSystem.Fl atStyle=FlatSty le.System;
      > > this.Controls.A dd(buttonSystem );
      > > buttonSystem.Le ft=115;
      > > buttonSystem.To p=2;
      > > buttonSystem.Wi dth=110;
      > > buttonSystem.He ight=20;
      > > buttonSystem.Te xt="FlatStyle.S ystem";
      > > buttonSystem.Ke yDown+=new KeyEventHandler (OnButton);
      > > buttonSystem.Cl ick+=new EventHandler(On ButtonClick);
      > >
      > > buttonPopup=new Button();
      > > buttonPopup.Fla tStyle=FlatStyl e.Popup;
      > > this.Controls.A dd(buttonPopup) ;
      > > buttonPopup.Lef t=0;
      > > buttonPopup.Top =25;
      > > buttonPopup.Wid th=110;
      > > buttonPopup.Hei ght=20;
      > > buttonPopup.Tex t="FlatStyle.Po pup";
      > > buttonPopup.Key Down+=new KeyEventHandler (OnButton);
      > > buttonPopup.Cli ck+=new EventHandler(On ButtonClick);
      > >
      > > buttonFlat=new Button();
      > > buttonFlat.Flat Style=FlatStyle .Flat;
      > > this.Controls.A dd(buttonFlat);
      > > buttonFlat.Left =115;
      > > buttonFlat.Top= 25;
      > > buttonFlat.Widt h=110;
      > > buttonFlat.Heig ht=20;
      > > buttonFlat.Text ="FlatStyle.Fla t";
      > > buttonFlat.KeyD own+=new KeyEventHandler (OnButton);
      > > buttonFlat.Clic k+=new EventHandler(On ButtonClick);
      > > }
      > > private void OnButtonClick(o bject sender,EventArg s e)
      > > {
      > > MessageBox.Show ("Activate") ;
      > > }
      > >
      > > private void OnButton(object sender,KeyEvent Args e)
      > > {
      > > if(e.KeyData==K eys.Space || e.KeyData==Keys .Enter)
      > > button.PerformC lick();
      > > }
      > > [STAThread]
      > > static void Main()
      > > {
      > > Application.Run (new Form1());
      > > }
      > > }
      > > }[/color]
      >
      >[/color]


      Comment

      • Jason Kyle Baginski

        #4
        Re: Button FlatStyle.Syste m spacebar behavior

        > I'm not sure why you have a handler for the KeyDown event, since pressing[color=blue]
        > 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.[/color]

        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.Syste m 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.Syste m(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.

        Comment

        Working...