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.
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.