none
Keypress Event on a different form

    Question

  • I have two forms, form A and from B. Form A contains a few text boxes that display the record information typed on one of the boxes. The records are on a SQL database.

    Now when users do not remember the record number, they click a button, which opens form B with a list of all the records. So, what I need to do is execute the keypress event of form A when the record is selected on form B. Is this possible?

    Thanks in advance.
    Wednesday, March 18, 2009 11:07 AM

Answers

  • Hi Jomercat,

    Based on my understanding, your purpose is below,
    Show form2 from form1; operate on form2; close form2, then activate keypress event in form1.
    Please correct me if I am wrong.

    Code Snippet
    Form1:
            public Timer t = new Timer();  
     
            private void button3_Click(object sender, EventArgs e)  
            {  
                t.Interval = 1000;  
                t.Tick += new EventHandler(t_Tick);  
     
                Form2 f2 = new Form2();  
                f2.ShowDialog(this);  
            }  
     
            void t_Tick(object sender, EventArgs e)  
            {  
                t.Stop();  
                this.Form1_KeyPress(thisnew KeyPressEventArgs('a'));  
                 
            }  
     
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)  
            {  
                MessageBox.Show("test");  
            } 

    Form2:
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form1 fm = this.Owner as Form1;  
                fm.t.Start();  
     
                this.Close();  
            } 

    Best regards,
    Guo
    • Proposed as answer by Guo Surfer Tuesday, March 24, 2009 2:24 AM
    • Marked as answer by Guo Surfer Friday, March 27, 2009 4:01 AM
    Friday, March 20, 2009 11:23 AM

All replies

  • Hi Jomercat,

    Based on my understanding, your purpose is below,
    Show form2 from form1; operate on form2; close form2, then activate keypress event in form1.
    Please correct me if I am wrong.

    Code Snippet
    Form1:
            public Timer t = new Timer();  
     
            private void button3_Click(object sender, EventArgs e)  
            {  
                t.Interval = 1000;  
                t.Tick += new EventHandler(t_Tick);  
     
                Form2 f2 = new Form2();  
                f2.ShowDialog(this);  
            }  
     
            void t_Tick(object sender, EventArgs e)  
            {  
                t.Stop();  
                this.Form1_KeyPress(thisnew KeyPressEventArgs('a'));  
                 
            }  
     
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)  
            {  
                MessageBox.Show("test");  
            } 

    Form2:
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form1 fm = this.Owner as Form1;  
                fm.t.Start();  
     
                this.Close();  
            } 

    Best regards,
    Guo
    • Proposed as answer by Guo Surfer Tuesday, March 24, 2009 2:24 AM
    • Marked as answer by Guo Surfer Friday, March 27, 2009 4:01 AM
    Friday, March 20, 2009 11:23 AM
  • Guo Surfer,

    Thanks for your reply. I will try your suggestion.

    Thanks.
    Tuesday, March 24, 2009 9:35 PM
  • Hi Jomercat,

    I'm marking my post as answer.
    If you still have problem, please feel free to unmark as the answer and change the issue type back to "Question" and follow up with more necessary information. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
    Thank you!

    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, March 27, 2009 4:01 AM
  • Hi Jomercat,

    You also can define a public method on Form1, in which invokes the Form1_KeyPress event, and call this public method from Form2. In this way, you don't have to use a timer.
    Sample code:
    Form1:
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.ShowDialog(this);
            }
    
            public void function1()
            {
                Form1_KeyPress(this,new KeyPressEventArgs('A'));
            }
    
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                this.Text = "test";
                //MessageBox.Show("test");
            }

    Form2:

            private void button1_Click(object sender, EventArgs e)
            {
                Form1 f1 = this.Owner as Form1;
                this.Close();
                f1.function1();
            }


    Best regards,
    Guo
     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 31, 2009 10:28 AM
  • Guo Surfer,

    Thank you for your reply again. However, I need to execute the KeyPress event of the text box on form1 where the user types the record to find. It was my mistake on the original post.

    Thanks.
    Tuesday, March 31, 2009 11:57 AM
  • Make Form Bs constructor take in a Form A reference.

    private FormA formA;

    public FormB(FormA formA)
    {
        ...
        this.formA = formA;
    }


    Then you will have access to all public functions on Form A. If you have a public KeyEvent function you can call it from Form B.
    Tuesday, March 31, 2009 1:42 PM
  • Holm76,

    Thanks for your reply too.

    I have the reference now. So now how do I call the KeyPress event of the textbox?

    Thanks.
    Tuesday, March 31, 2009 3:22 PM