c# - Textbox difficulty -
c# - Textbox difficulty -
i trying info 2 textbox
fields , add together them 3rd textbox
using simple button. can done easily.
where got stuck scenario this. button may first disabled nil provided in text fields, , enabled user type digit in text field.
public partial class form1 : form { public form1() { initializecomponent(); if (textbox1.text != null) { button1.enabled = true; } else { button1.enabled = false; } } private void button1_click(object sender, eventargs e) { int = convert.toint32(textbox1.text); int b = convert.toint32(textbox2.text); textbox3.text = (a + b).tostring(); } }
something should trick (but it's not elegant):
public partial class form1 : form { public form1() { initializecomponent(); this.setbuttonenablestate(); } private void setbuttonenablestate() { button1.enabled = !(string.isnullorwhitespace(textbox1.text) || string.isnullorwhitespace(textbox2.text)); } private void button1_click(object sender, eventargs e) { int = convert.toint32(textbox1.text); int b = convert.toint32(textbox2.text); textbox3.text = (a + b).tostring(); } private void textbox1_textchanged(object sender, eventargs e) { this.setbuttonenablestate(); } private void textbox2_textchanged(object sender, eventargs e) { this.setbuttonenablestate(); } }
update:
in case might want check if textbox values int values , if case, enable button. why not update method above this?
private void setbuttonenablestate() { int n; button1.enabled = int.tryparse(textbox1.text, out n) && int.tryparse(textbox2.text, out n); }
c# winforms
Comments
Post a Comment