Button.Click Handling Issue

csharp Kenya
  • 13 years ago

      
    I have one button and one listbox on same Form.

    Inside InitializeComponent(){

        For button1 I have
        this.button1.Click += new System.EventHandler(this.Button1Click);

        and for listBox1 I have
        this.listBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ListBoxKeyUp);
    }
    public partial class MainForm : Form{
        public MainForm(){
            InitializeComponent();

        }
        void Button1Click(object sender, EventArgs e){
            Console.WriteLine("Btn Clicked");
            this.listBox1.Focus();
        }
        void ListBoxKeyUp(Object sender, KeyEventArgs kev){
            Console.WriteLine("List KeyUp");
            // My Code to Handle the Key Up
    }

    On Running the App and Pressing Enter on button1
    The Output is
    Btn Clicked
    List KeyUp

    What is happening here is that the user is clicking the Button, the focus is shifting to listbox1 and a KeyEvent is being generated on listbox and my key handling code is getting executed. (Total KeyPresses 1)

    What I really need is that the user should Press a Push Button (button1) and the Focus be transfered to my listbox1. Now if the User wants she may Press a Key (Enter) again to select an Item in listbox1. (Total Key Presses by user 2)

    I think I've explained my problem clearly.

    So what may be the proper way to achieve this.

    Thanks... 

  • 13 years ago

    I've just created a simple app as you describe with this code:

         public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Console.WriteLine("Btn Clicked");
                this.listBox1.Focus();
            }

            private void listBox1_KeyUp(object sender, KeyEventArgs e)
            {
                Console.WriteLine("List KeyUp");
                // My Code to Handle the Key Up
            }
        }
     

    and do not get the behaviour you describe.... so I suspect there is some other bits of your code at work here. Try just pasting the above code into a new standalone app and check you don't get that behavior...  

  • 13 years ago

    Here's My Solution

    class YoButton : System.Windows.Forms.Button
      {
       public event System.EventHandler Selection;
       public YoButton()
       {
        this.KeyUp +=new KeyEventHandler(keyUp);
        this.MouseUp += new MouseEventHandler(mouseUp);
       }
       protected void mouseUp(object obj, MouseEventArgs mev){
        if( mev.Button == MouseButtons.Left )
         raiseSelected();
       }
       protected void keyUp(object obj, KeyEventArgs kev){
        if( kev.KeyCode == Keys.Enter || kev.KeyCode == Keys.Space )
         raiseSelected();
       }
       protected void raiseSelected(){
        if( this.Selection != null )
         this.Selection(this, new System.EventArgs());
       }
      }


    And Then...

    InitComponent()
    {
    this.button1.Selection += new EventHandler(Button1Click);

    this.listBox1.KeyPress += new KeyPressEventHandler(this.ListBoxKeyPress);
    this.listBox1.KeyUp += new KeyEventHandler(this.ListBoxKeyUp); 
    this.listBox1.KeyDown += new KeyEventHandler(this.ListBoxKeyDown);
    this.listBox1.GotFocus += new System.EventHandler(listGotFocus);
    }

    void Button1Click(Object sender, EventArgs e)
    {
     Console.WriteLine("Btn Clicked");
     this.listbox1.Focus();
    }
    void listGotFocus(object sen, EventArgs e)
    {
     Console.WriteLine("Listbox got Focus");
    }
    void ListBoxKeyDown(Object sender, KeyEventArgs kev)
    {
     Console.WriteLine("List KeyDown");
    }
    void ListBoxKeyUp(Object sender, KeyEventArgs kev)
    {
     Console.WriteLine("List KeyUp");
    }
    void ListBoxKeyPress(Object sender, KeyPressEventArgs kev)
    {
     Console.WriteLine("List Key Pressed");
    }

    Now if we handle button1.Click Event Then Output is
    Btn Clicked
    Listbox got Focus
    List KeyUp

    If We handle button1.Selection (Custom Event) Then Output is
    Btn Clicked
    Listbox got Focus

    Which is what I want

    Thanks Everybody...

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson