Answered by:
How to Use 1 Keyboard for PasswordBox and TextBox

Question
-
Hello All,
I have One Textbox and one PasswordBox on a Login Form That Contains KeyBoard on it as Well. I want to Use this Keyboard for Both TextBox and PasswordBox as Well. How i can use it. While i am already Using this Code for All TextBox.
private TextBox _textBox.Text =null private void Button_Click(object sender, RoutedEventArgs e) { if (_textBox.Text == "") { _textBox.Text = ((Button)sender).Content.ToString(); } else { _textBox.Text = _textBox.Text + ((Button)sender).Content.ToString(); } } private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _textBox= (TextBox)sender; }
I am using VS2010 and .NET 4.
Jazaib Hussain
Answers
-
Hi Hussain,
I hope i understood you properly, you want to use same virtual keyboard to type data in both username and password textboxes, if am wrong let me know, follow my code.
private TextBox _textBox = new TextBox(); private TextBox _password = new TextBox(); private bool _isPwd = false; void MainWindowLoaded(object sender, RoutedEventArgs e) { _textBox.GotFocus += new RoutedEventHandler(TextBoxGotFocus); _password.GotFocus += new RoutedEventHandler(PasswordGotFocus); } private void Button_Click(object sender, RoutedEventArgs e) { var button = sender as Button; var text = button.Content.ToString(); if (!_isPwd) { if (string.IsNullOrEmpty(_textBox.Text)) { _textBox.Text = text; } else { _textBox.Text = _textBox.Text + text; } } else { if (string.IsNullOrEmpty(_password.Text)) { _password.Text = text; } else { _password.Text = _password.Text + text; } } } void PasswordGotFocus(object sender, RoutedEventArgs e) { _isPwd = true; } void TextBoxGotFocus(object sender, RoutedEventArgs e) { _isPwd = false; }
Thanks & Regards, Syed Amjad, Sr. Silverlight/WPF Developer, yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
- Proposed as answer by Jason Dot Wang Friday, May 31, 2013 7:10 AM
- Marked as answer by Jason Dot Wang Monday, June 10, 2013 8:22 AM
All replies
-
Code you are using should work just fine
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming -
-
Hi Hussain,
I hope i understood you properly, you want to use same virtual keyboard to type data in both username and password textboxes, if am wrong let me know, follow my code.
private TextBox _textBox = new TextBox(); private TextBox _password = new TextBox(); private bool _isPwd = false; void MainWindowLoaded(object sender, RoutedEventArgs e) { _textBox.GotFocus += new RoutedEventHandler(TextBoxGotFocus); _password.GotFocus += new RoutedEventHandler(PasswordGotFocus); } private void Button_Click(object sender, RoutedEventArgs e) { var button = sender as Button; var text = button.Content.ToString(); if (!_isPwd) { if (string.IsNullOrEmpty(_textBox.Text)) { _textBox.Text = text; } else { _textBox.Text = _textBox.Text + text; } } else { if (string.IsNullOrEmpty(_password.Text)) { _password.Text = text; } else { _password.Text = _password.Text + text; } } } void PasswordGotFocus(object sender, RoutedEventArgs e) { _isPwd = true; } void TextBoxGotFocus(object sender, RoutedEventArgs e) { _isPwd = false; }
Thanks & Regards, Syed Amjad, Sr. Silverlight/WPF Developer, yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
- Proposed as answer by Jason Dot Wang Friday, May 31, 2013 7:10 AM
- Marked as answer by Jason Dot Wang Monday, June 10, 2013 8:22 AM
-
Did you attach the event handler TextBox_GotFocus to both TextBoxes?
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming -