locked
Masked textbox and SelectAll ???? RRS feed

  • Question

  • Oh, so much troubles for so simple a thing ... just selecting all entered text after enter is pressed - that's it - simple and easy !

    it won't work for me - I've tried this:

      private void maskedTextBox_SOnr_Enter(object sender, EventArgs e) {
       maskedTextBox_SOnr.Focus();
       maskedTextBox_SOnr.SelectAll();
      }
    
    

    I've tried this:

         protected override void OnGotFocus(EventArgs e) {
          base.OnGotFocus(e);
          maskedTextBox_SOnr.SelectionStart = 0;
          maskedTextBox_SOnr.SelectionLength = this.Text.Length;
        }
    

    but this is what works best for me - but NOT really:

        private void maskedTextBox_SOnr_KeyDown(object sender, KeyEventArgs e) {
          if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Return) {
            byte[] sendData= new byte[2];  <strong><em>// Now a look-up in hardware is done</em></strong>
            sendData[0]=byte.Parse(commonThings.getLbyte(short.Parse(maskedTextBox_SOnr.Text.ToString())).ToString());
            if (commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.Text.ToString())).ToString()!="")
              sendData[1]=byte.Parse(commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.Text.ToString())).ToString());
            else sendData[1]=0;
            MessageBox.Show(sendData[0].ToString()); // Now I show the key I have to look-up - it's ALLWAYS the first key I enter !
            byte[] replyData =IB.sendCommandoToIB(P50X.Commando.xSOGet, sendData);
            textBox_Value.Text=(replyData[0]*0xff+replyData[1]).ToString();
            maskedTextBox_SOnr.SelectionStart=0;
            maskedTextBox_SOnr.SelectionLength=maskedTextBox_SOnr.Text.Length;
    
          }
        }
    
    

    In my masked textbox, maskedTextBox_SOnr, I enter a nummer, press ENTER / Return and then another textbox should show a value as a result from a sort of look-up in a special peace of hardware - never mind that hardware - the masked textbox should then be fully selected so I can just enter a new key and get the result form that look-up .. ect ect

    I HAVE tried all the solutions coming up when I enter this question - none of them works for me !

    OK - the masked textbox IS FULLY SELECTED but the value of maskedTextBox_SOnr.Text.ToString() ALLWAY stays tha same = the first key I enter - it never changes and I can NOT just type some new keystrokes in the selection to replace what was previously entered - I HAVE to use BACKSPACE or DELETE first !

    HILFE - How should this simple thing be done ?


    Best regards KSor, Denmark
    • Changed type Ksor Thursday, February 24, 2011 7:08 PM
    Thursday, February 24, 2011 7:01 AM

Answers

  • I think because you are using KeyDown event , You should use KeyPress 

    because when you are pressing the Enter , KeyDown event is fired but after that KeyUp event will fire so fast 

    because of that , your code is not execute completely

     

    • Marked as answer by Ksor Saturday, March 12, 2011 9:08 AM
    Saturday, March 12, 2011 8:49 AM

All replies

  • Hello Ksor.. Hope you are doing great.. :) Your code is wonderful eventhough it is small. Let me try whether I can help you.. First of all I wanted to let you know that Enter() is the event triggered whenever focus comes to the control. I think it sometimes mislead developers. Secondly, the Text property of textbox gives you the entire text - not the selected text. You should be using maskedTextBox.SelectedText property. Please let me know if it works or you have other constraints. Regards, Jean Paul
    Resolving n Evolving in C#
    • Marked as answer by Ksor Saturday, February 26, 2011 8:51 AM
    • Unmarked as answer by Ksor Friday, March 11, 2011 12:08 PM
    Saturday, February 26, 2011 6:36 AM
  • Hi Jean Paul V.A

    Off cause - how stupid can one be - and now it works nicely !

     


    Best regards KSor, Denmark
    Saturday, February 26, 2011 8:51 AM
  • No ... it doesn't do the job right !

    Here is my code:

        private void maskedTextBox_SOnr_KeyDown(object sender, KeyEventArgs e) {
          if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Return) {
            maskedTextBox_SOnr.SelectionStart=0;
            maskedTextBox_SOnr.SelectionLength=maskedTextBox_SOnr.Text.Length;
    
            byte[] sendData= new byte[2];
            sendData[0]=byte.Parse(commonThings.getLbyte(short.Parse(maskedTextBox_SOnr.SelectedText.ToString())).ToString());
            if (commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.SelectedText.ToString())).ToString()!="")
              sendData[1]=byte.Parse(commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.SelectedText.ToString())).ToString());
            else sendData[1]=0;
            
            byte[] replyData =IB.sendCommandoToIB(P50X.Commando.xSOGet, sendData);
            textBox_Value.Text=(replyData[0]*0xff+replyData[1]).ToString();
            maskedTextBox_SOnr.SelectionStart=0;
            maskedTextBox_SOnr.SelectionLength=maskedTextBox_SOnr.Text.Length;
          }
        }
    
    

    The first two lines in the IF matches the last two lines - simply to get the entered text marked.

    When you enter some new text - you  HAVE to use DELETE/BACKSPACE to get rid of the last entered text !

    Why is this so hard to get done ?


    Best regards KSor, Denmark
    Friday, March 11, 2011 12:19 PM
  • KSor, I was looking at this code and wanted to ask you why do you want to complex things by using text selection to get the textbox value? Ibelieve the intent was to select the entire text in the textbox! and also the following code:

    maskedTextBox_SOnr.SelectionStart=0;
    maskedTextBox_SOnr.SelectionLength=maskedTextBox_SOnr.Text.Length;
    maskedTextBox_SOnr.SelectedText.ToString()

    relates to (output similar to):

    maskedTextBox_SOnr.Text

    The modified code can look like:

        private void maskedTextBox_SOnr_KeyDown(object sender, KeyEventArgs e)
        {
          if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
          {
            //maskedTextBox_SOnr.SelectionStart = 0;
            //maskedTextBox_SOnr.SelectionLength = maskedTextBox_SOnr.Text.Length;
    
            byte[] sendData = new byte[2];
            sendData[0] = byte.Parse(commonThings.getLbyte(short.Parse(maskedTextBox_SOnr.Text)).ToString());
            if (commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.Text)).ToString() != "")
              sendData[1] = byte.Parse(commonThings.getHbyte(short.Parse(maskedTextBox_SOnr.Text)).ToString());
            else sendData[1] = 0;
    
            byte[] replyData = IB.sendCommandoToIB(P50X.Commando.xSOGet, sendData);
            textBox_Value.Text = (replyData[0] * 0xff + replyData[1]).ToString();
            //maskedTextBox_SOnr.SelectionStart = 0;
            //maskedTextBox_SOnr.SelectionLength = maskedTextBox_SOnr.Text.Length;
    
          }
        }
    

    Balaji Baskar
    http://codesupport.wordpress.com
    Click on "Vote As Helpful" and "Mark As Answer" if this has helped you.
    Friday, March 11, 2011 9:51 PM
  • >> Balaji Baskar

    Yeah, I thought so too, but Oh my God ....

    The idea is:

    I enter a number in one textbox

    I press ENTER

    the "result" is presented in another textbox

    AND

    the number in the FIRST textbox is now marked so I just have to enter a new number witch (because of the marking) will replace the first entered number and a new result will be presented in the seconf textbox.

    But that's NOT the case - something IS left from the first input - especially if you enter FULL length - in my case 3 characters !

     


    Best regards KSor, Denmark
    Friday, March 11, 2011 11:58 PM
  • I think because you are using KeyDown event , You should use KeyPress 

    because when you are pressing the Enter , KeyDown event is fired but after that KeyUp event will fire so fast 

    because of that , your code is not execute completely

     

    • Marked as answer by Ksor Saturday, March 12, 2011 9:08 AM
    Saturday, March 12, 2011 8:49 AM
  • >> Hamid Jabarpourfard

    Yeah, that's it - thx a lot !

     


    Best regards KSor, Denmark
    Saturday, March 12, 2011 9:08 AM
  • Your welcome dude ;)
    Saturday, March 12, 2011 9:14 AM