c# - Textbox cannot be cleared with button press -
i keep getting error message whenever tried clear textbox2. how can solve this?
private void textbox2_textchanged(object sender, eventargs e) { string hexkey = this.textbox2.text; if(textbox2.focused) int key = convert.toint32(hexkey, 16); } private void button2_click_1(object sender, eventargs e) { textbox2.clear(); }
[error]: system.argumentoutofrangeexception: 'index out of range. must non-negative , less size of collection. parameter name: startindex'
[solution]:
private void textbox2_textchanged(object sender, eventargs e) { string hexkey = this.textbox2.text; if(textbox2.focused) //add line in int key = convert.toint32(hexkey, 16); }
firzanah, when clear textbox2, textchanged event trigger. since there won't in text box @ point, error occur when try convert nothing int32. solve problem, add if(textbox2.focused)
condition change event or, better yet, check you've got int begin with:
private void textbox2_textchanged(object sender, eventargs e) { int n; bool isnumeric = int.tryparse(textbox2.text, out n); if (!isnumeric) return; string hexkey = textbox2.text; int key = convert.toint32(hexkey, 16); }
Comments
Post a Comment