locked
Keyboard event to handle text in Texbox in C#.NET RRS feed

  • Question

  • Hi,

    I am trying to create Keyboard in Windows Form Application using C#.NET. I am struck in one situation; when I press any button/key on keyboard focused/highlighted text should be overwrite with new key/button I press. I am trying to search textbox - keypress event but I am not able to get proper output.

    Can anyone help me with this.


    Rushabh

    Monday, September 10, 2012 3:41 PM

Answers

  • Try the following:

        string newText = "r"; // Or whatever you want to actually replace it with

        txtCal.SelectedText = newText;

    • Proposed as answer by Lisa Zhu Thursday, September 13, 2012 2:55 AM
    • Marked as answer by Lisa Zhu Monday, September 17, 2012 1:52 AM
    Wednesday, September 12, 2012 8:17 PM
  • Use Focus Leave event of Textbox.

    Rushabh

    • Marked as answer by ShahRushabh Monday, September 10, 2012 4:39 PM
    Monday, September 10, 2012 4:39 PM
  • Hi ShahRushabh,

    String.Replace Method  is used to Replaces all occurrences of a specified Unicode character or String in this instance, with another specified Unicode character or String “.

    So when you selected ” aa” and ran  txtCal.Text = txtCal.Text.Replace(_selectText, _text); “ .The Replace() method will replace all the string “aa” in the txtCal.Text to “r” , that is why when inputing “aaaabbbbcccc  but you get “rrbbbbcccc” .

    To do what you want , pcmichaels has given the answer .

    Please try :

      private void txtCal_Leave(object sender, EventArgs e)
      {
         string newText = "r"; // Or whatever you want to actually replace it with
         txtCal.SelectedText = newText; //When focus leave , you will get the wanted string in txtCal
               
      }

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Lisa Zhu Monday, September 17, 2012 1:52 AM
    Thursday, September 13, 2012 2:55 AM

All replies

  • Hello ShahRushabh,

    Have you tried the TextChanged event ?
    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx

    Monday, September 10, 2012 3:44 PM
  • Use Focus Leave event of Textbox.

    Rushabh

    • Marked as answer by ShahRushabh Monday, September 10, 2012 4:39 PM
    Monday, September 10, 2012 4:39 PM
  • Hi ShahRushabh,

    Thanks for sharing us the solution .Have a nice day !

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 12, 2012 6:26 AM
  • Hi,

    I have one confusion which you or anyone can help me. If have a string "aaaabbbbcccc", if I want to replace "aa[aa]bbbbcccc" with "r" to have new string "aarbbbbcccc" then how can I do that.

    I am able to replace the string but my new string is "rrbbbbcccc" instead of "aarbbbbcccc".

    Here is my code,

                if (_selectText.Length > 0)
                {
                    int SelectionLenght = txtCal.SelectionLength;
                    string SelectText = txtCal.Text.Substring(txtCal.SelectionStart, SelectionLenght);
                    txtCal.Text = ReplaceMethod(SelectText, _text);
                }

    Thanks.

    Rusabh.


    Rushabh

    Wednesday, September 12, 2012 7:10 PM
  • Try the following:

        string newText = "r"; // Or whatever you want to actually replace it with

        txtCal.SelectedText = newText;

    • Proposed as answer by Lisa Zhu Thursday, September 13, 2012 2:55 AM
    • Marked as answer by Lisa Zhu Monday, September 17, 2012 1:52 AM
    Wednesday, September 12, 2012 8:17 PM
  • It replaces both the occurrence, I tried that one initially and then think to use substring.

    if (_selectText.Length > 0)
                {              
                    txtCal.Text = txtCal.Text.Replace(_selectText, _text);
                }

    Input : aaaabbbbcccc

    Output : rrbbbbcccc


    Rushabh

    Wednesday, September 12, 2012 8:37 PM
  • Hi ShahRushabh,

    String.Replace Method  is used to Replaces all occurrences of a specified Unicode character or String in this instance, with another specified Unicode character or String “.

    So when you selected ” aa” and ran  txtCal.Text = txtCal.Text.Replace(_selectText, _text); “ .The Replace() method will replace all the string “aa” in the txtCal.Text to “r” , that is why when inputing “aaaabbbbcccc  but you get “rrbbbbcccc” .

    To do what you want , pcmichaels has given the answer .

    Please try :

      private void txtCal_Leave(object sender, EventArgs e)
      {
         string newText = "r"; // Or whatever you want to actually replace it with
         txtCal.SelectedText = newText; //When focus leave , you will get the wanted string in txtCal
               
      }

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Lisa Zhu Monday, September 17, 2012 1:52 AM
    Thursday, September 13, 2012 2:55 AM