question

VAer-4038 avatar image
0 Votes"
VAer-4038 asked DanielZhang-MSFT commented

How to call method from class?

I still have trouble to understand calling method from class.

Let's say, I have two textbox in Form1 and two textbox in Form2, and all those four textboxes use the same code.

So how can I put the code in class, so that I don't need to repeat the code? The thing I don't understand is: class does not have textbox, how can I write txtbox Key Press there?

Anyway, how to write it and what is the code to call it?

Thanks.

 private void txtBoxFirstNameinForm1_KeyPress(object sender, EventArgs e)
 {
 //Validate input character
 }
    
 private void txtBoxFirstNameinForm2_KeyPress(object sender, EventArgs e)
 {
 //Validate input character
 }
    
 private void txtBoxLastNameinForm1_KeyPress(object sender, EventArgs e)
 {
 //Validate input character
 }
    
 private void txtBoxLastNameinForm2_KeyPress(object sender, EventArgs e)
 {
 //Validate input character
 }
windows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @VAer-4038
You can add a new class in your project and put your shared method into it.
(Right click your project name->Add->Class)
Then you can call this method in each form through an instance of the new class directly.
Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered VAer-4038 commented

Hi VAer-4038,
>>class does not have textbox, how can I write txtbox Key Press there
First, you can drag the TextBox from ToolBox to Form1 and Form2. (You can find ToolBox in View menu)
Then right-click the TextBox and click the Properties.
In the Properties page, click Events icon and find the KeyPress event.
53833-16.png
Double click KeyPress and you will see the txtbox Key Press in form class code.
Next, you can put same code (Validate input character) in a method.
And then call this method in KeyPress event.
Here is a simple code example you can refer to.
Form1.cs

 public Form1()
     {
         InitializeComponent();
     }
        
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
     {
         validate();
     }
        
     private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
     {
         validate();
     }
        
     public  void validate() 
     {
         //Validate input character
        
     }
        
     private void button1_Click(object sender, EventArgs e)
     {
         Form2 f2 = new Form2();
         f2.yourAction =validate;
         f2.Show();
     }

Form2.cs

  public Form2()
     {
         InitializeComponent();
     }
     //Pass an action through to the new form
     Form1 f1 = new Form1();
     public Action yourAction { get; set; }
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
     {
         Action instance = yourAction;
         if (instance != null)
             instance();
     }
        
     private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
     {
         Action instance = yourAction;
         if (instance != null)
             instance();
     }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






16.png (9.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, I know how it flows now. Basically, you define below method validate in Form1, then use a variable (whatever it is called) yourAction to pass the method to Form2, the use the variable in Form2.

My original thinking is: Writing method validate in a class (not belong to any form), so in this case, deleting Form1 will not affect other forms calling the method. I was thinking that a shared method should be placed somewhere not belonging to any forms, but every form can call the method directly.

I still have issue with your flow logic. Now button1 is in Form1, but user may click parent's form menustrip button to switch from form to form. Such kind of flow does not make code simplified too much, I feel it makes worse. If not too many items sharing the code, I can just copy and paste.

Thanks anyway.

      public  void validate() 
      {
          //Validate input character
            
      }
0 Votes 0 ·