none
How to Use 1 Keyboard for PasswordBox and TextBox RRS feed

  • 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

    Thursday, May 30, 2013 10:59 AM

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.

    Thursday, May 30, 2013 12:41 PM

All replies

  • Code you are using should work just fine

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Thursday, May 30, 2013 11:54 AM
  • Hey,
    Thanks for reply, But this code is working fine for TextBox but not for PasswordBox. :(


    Jazaib Hussain

    Thursday, May 30, 2013 11:59 AM
  • 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.

    Thursday, May 30, 2013 12:41 PM
  • Did you attach the event handler TextBox_GotFocus to both TextBoxes?

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Thursday, May 30, 2013 12:46 PM
  • Dear Syed,

    Thanks for your help, But i am using a TextBox Control and PasswordBox Control in Windows Forms in c#... Ur Code is working For TextBox Controls Just

    Thanks


    Jazaib Hussain

    Thursday, May 30, 2013 1:30 PM