How to call method from class?

VAer 756 Reputation points
2021-01-05T22:31:20.053+00:00

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
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-01-06T02:28:08.117+00:00

    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.


0 additional answers

Sort by: Most helpful