c# - Display text in textbox when clicked in OK message -
i trying develop program show message "pass" (inside textbox2) when 2 conditions happen:
1) when textbox1 receives text containing "1" in first string
and
2) when "enter" key pressed send text contained in (this text send sql db)
it difficult me develop if function insert these 2 conditions. appreciated, thank much.
below piece of code i´ve tried:
private void textbox2_textchanged(object sender, eventargs e) { string input = textbox2.text; string strname = "pass"; if (input.startswith("g") && (control.modifierkeys & keys.enter) != 0) { textbox5.text = strname; //change_color = green } }
1st condition: may use keypresseventargs determine key has been pressed.
2nd condition: may choose implement seperate function check whether or not there '1' character in first string of textbox1.
goal: if both conditions occur, textbox2's text be: "pass".
i offer following solution:
private void textbox1_keypress(object sender, keypresseventargs e) { char _enter = (char)13; if (e.keychar == _enter && isfirststringcontaining1()) textbox2.text = "pass"; else textbox2.text = ""; } private bool isfirststringcontaining1() { int length = textbox1.text.indexof(' '); if (length == -1) length = textbox1.text.length; string first_str = textbox1.text.substring(0, length); foreach (char c in first_str) if (c.equals('1')) return true; return false; }
Comments
Post a Comment