forms - How can I clear a TextBox in Form1 from Form2? C# -
this question has answer here:
- how access form control form? 4 answers
i have textbox (let's call textbox1) inside form1. when user presses "new" clear screen, after accepting work lost (from within form2), how make textbox1 clear? can't access directly second form , can't think of feasible way this.
thanks in advance help!
add public flag of success in form2
, check afterwards. or can use built-in functionality of showdialog
, dialogresult
. more proper in terms of oop , logic changing value of form1
form2
.
if change value of hardcoded form unable reuse form again.
with approach can reuse form again in place. using simple custom variable:
public class form2 : form { public bool result { get; set; } public void buttonyes_click(object sender, eventargs e) { result = true; this.close(); } public void buttonno_click(object sender, eventargs e) { result = false; this.close(); } } public class form1 : form { public void button1_click(object sender, eventargs e) { using (form2 form = new form2()) { form.showdialog(); if (form.result) textbox1.text = string.empty; } } }
using dialogresult
or showdialog
:
public class form2 : form { public void buttonyes_click(object sender, eventargs e) { this.dialogresult = dialogresult.yes; this.close(); } public void buttonno_click(object sender, eventargs e) { this.dialogresult = dialogresult.no; this.close(); } } public class form1 : form { public void button1_click(object sender, eventargs e) { using (form2 form = new form2()) { var result = form.showdialog(); if (result == dialogresult.yes) textbox1.text = string.empty; } } }
it idea use using
form not disposed after showdialog
.
makes disposing deterministic. way can ensure disposed right after stopped using it.
Comments
Post a Comment