R
R
random2014-10-13 09:23:49
C++ / C#
random, 2014-10-13 09:23:49

How to clear textBoxes on another form?

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

2 answer(s)
C
cjey, 2014-10-13
@demon123

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 = "";
}

And in Form2
private void Cancel_Click(object sender, EventArgs e)
{
  dialogResult = DialogResult.Cancel;
  Close();
}

private void OK_Click(object sender, EventArgs e)
{
  dialogResult = DialogResult.OK;
  Close();
}

An alternative solution would be to store a reference to Form1 in the static field of the Program class and access form 1 through Program.form1

A
AlexP11223, 2014-10-13
@AlexP11223

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 question

Ask a Question

731 491 924 answers to any question