Answer the question
In order to leave comments, you need to log in
How to clear textBoxes on another form?
private
This is how the form actually looks like.
void Cancel_Click(object sender, EventArgs e)
{
Close();
Form1 f = new Form1();
f.textBox1.Text = null;
f.textBox2.Text = "";
f.textBox3.Text = string.Empty;
}
Tried to write like this, but it doesn't work. The form closes and the textBoxes are not cleared.
Answer the question
In order to leave comments, you need to log in
You create a new form and clear the data in it, which is why the current version does not work.
As I understand it, you need to clear the data in an already created form.
The most correct solution would be
in Form1:
Form2 f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK)
{
TextBox1.Text = "ok";
}
else
{
TextBox1.Text = "";
}
private void Cancel_Click(object sender, EventArgs e)
{
dialogResult = DialogResult.Cancel;
Close();
}
private void OK_Click(object sender, EventArgs e)
{
dialogResult = DialogResult.OK;
Close();
}
So your object f is thrown away immediately after the Cancel_Click method is completed, you do nothing with it, do not open this form, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question