locked
Maskedtextbox and SelectAll() RRS feed

  • Question

  • Hi,

     

    If have a problem with the maskedtextbox.

    I want the select all the text when the the maskedtextbox got focused (via "Enter" event).

    and then using the maskedTextBox.SelectAll () funtion.

    It works fine without putting a mask.

    But when i put the mask = "00000", the SelectAll() funtion doesnt work anymore.

    Little side question, whats the difference between the mask "00000" and the mask "99999".

    Can anyone can help me with this?

    thx !

    BambooZelD

    Sunday, January 8, 2006 3:04 PM

Answers

  • The MaskedTextBox performs a select of its own in the focused event, so it overrides your set. You'll need to post your select after the MaskedTextBox performs its select. You can do this by using the BeginInvoke method

     

    -mark

    Program Manager

    Microsoft

    This post is provided "as-is"

     

    Monday, January 9, 2006 5:52 AM

All replies

  • Hi,

    I can answer your side question easily. Go read here :) You want to look at the remarks section.

    I'm not so sure about your first problem, however. Perhaps because the input has failed validation against the mask?

    --Geoff

    Monday, January 9, 2006 5:25 AM
  • The MaskedTextBox performs a select of its own in the focused event, so it overrides your set. You'll need to post your select after the MaskedTextBox performs its select. You can do this by using the BeginInvoke method

     

    -mark

    Program Manager

    Microsoft

    This post is provided "as-is"

     

    Monday, January 9, 2006 5:52 AM
  •  Mark Rideout wrote:

    The MaskedTextBox performs a select of its own in the focused event, so it overrides your set. You'll need to post your select after the MaskedTextBox performs its select. You can do this by using the BeginInvoke method

     

    -mark

    Program Manager

    Microsoft

    This post is provided "as-is"

     

     

    I'm not very familiar whith begininvokes etc  But i gave it a try and it works !!!

    But I dont really understand it for 100 % why  

     

    here is my code :

     

    private delegate void SetMaskedTextBoxSelectAllDelegate(MaskedTextBox txtbox);

    private void maskedTextBox1_Enter(object sender, EventArgs e)

    {

     this.BeginInvoke(new SetMaskedTextBoxSelectAllDelegate(SetMaskedTextBoxSelectAll), new object[] { (MaskedTextBox) sender });

    }

    private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)

    {

    txtbox.SelectAll();

    }

    Any remarks cause i dont want my program to crash

     

    Any way thanks a lot for you're help !!!

    Monday, January 9, 2006 8:10 PM
  • Looks good. The way BeginInvoke works is that is posts a messsage (win32) to the message queue. Messages from the queue are retrieved when the app isn't doing anything. The message queue and the code your application is running is both on the same thread. When your app is executing code the message queue isn't processing messages. For example, if you start a long running loop in your code, your application isn't responsive to painting because it isn't processing any paint messages. Same is true for the message that BeginInvoke posts.

    So, in your scenario, the BeginInvoke code (SetMaskedTextBoxSelectAll) runs after Windows Form's Enter code and after your Enter code. So this allows your Selectall code to occur after Windows Forms has done its processing. Basically just think that BeginInvoke code runs after "everyone" has done their work.

    Note with Visual Studio 2005 and C# you can make your code look better by using what is called Anonymous Delegates. Here is what it would look like:



    private void maskedTextBox1_Enter(object sender, EventArgs e)
    {
     this.BeginInvoke((MethodInvoker) delegate() {
       txtbox.SelectAll();
     });
    }

     
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

     

     

    Monday, January 9, 2006 8:59 PM
  • How does this VB.Net version look?  It works, just wondering if more condensed code is possible.  Thanks....

    Private Sub txtMsk_Handle_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles txtMskField1.Enter, txtMskField2.Enter  etc

    Try

    Dim objDelegate As MaskedTextBoxSelectAll_Delegate

    objDelegate = AddressOf MaskedTextBoxSelectAll

    BeginInvoke(objDelegate, Me.ActiveControl)

    Catch etc..

    ----

    Public Delegate Sub MaskedTextBoxSelectAll_Delegate(ByVal ctl As MaskedTextBox)

    ----

    Private Sub MaskedTextBoxSelectAll(ByVal ctl As MaskedTextBox)

    ctl.SelectAll()

    End Sub

     

    Thursday, November 2, 2006 4:14 PM
  • This is great. I not only had the same problem as BambooZelD but I have always wanted to know how to execute code after the code returns to the event's owner.

    Also, as a side note, the "why?" of anonymous delegates has left me scratching my head until today.

    Thanks.

    Stritch

    Wednesday, January 17, 2007 3:39 AM
  • I still have a why. Why would the VS2005 documentation for the MaskedTextBox show the SelectAll method when it does not work unless done with a delagate? Either the documentation should be fixed or the method should be fixed to work like the documentation shows.
    Monday, February 19, 2007 9:47 PM
  • wah, i was fightting this problem for a long time, now i finally take it down, thanks man.
    Tuesday, February 20, 2007 6:35 AM
  • Amen to that (i.e. why it's in the documentation as just plain old SelectAll()) - heartily seconded after wondering what in the heck -I- was doing wrong. Indeed, documentation "does not correspond to reality".
    Tuesday, June 19, 2007 7:41 PM
  • For what it's worth here's another solution for those who will hit this bug in future.
    (if it's not a bug in the code, it's a bug in the documentation, in both cases it would still be existent in today versions (.net 3.5 and VS 2008)

    Instead of the workaround with beginInvoke, it also is possible to EXPLICITELY set focus on the control on enter.

    private void maskedTextBox1_Enter(object sender, EventArgs e)
    {
    maskedTextBox1.Focus();
    maskedTextBox1.SelectAll();
    }


    And: no, I do not know why this works.
    • Proposed as answer by seyed_m Tuesday, April 13, 2010 11:36 AM
    Wednesday, July 9, 2008 12:07 PM
  • Lysander,

     

    Thak you so much for your helpful posting.  I just came from two days of learning how to handle dates and null values back to my database in a maskedtextbox and then I ran into this same issue.   I think its ridiculous that we have to do such coding for such a simple task.

     

    Listen, I know we all like to code, right?  But come on Microsoft, really!

     

    I come from a Visual Foxpro background and I'm use to having a property that accepts a Boolean value called "SelectOnEntry."  Now doesn't that make much more sense.

     

    Anyway, Lysander, your information was very helpful and helped me solve my problem in two minutes.

     

    Thank you.

     

    Slider

     

     

    Tuesday, July 29, 2008 2:56 AM
  •  *Lysander* wrote:


    private void maskedTextBox1_Enter(object sender, EventArgs e)
    {
    maskedTextBox1.Focus();
    maskedTextBox1.SelectAll();
    }




    Cheers, I used this and it works, a heck of a lot easier than issuing delegates etc.
    Monday, September 15, 2008 9:42 AM
  • So much troubles for so simple a thing ... it won't work for me - here is my code and I beleave it 1-1 what you got:

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

    and I can NOT just type some new keystrokes to replace what was previously entered - I HAVE to use BACKSPACE first - why ?

     


    Best regards KSor, Denmark
    Wednesday, February 23, 2011 4:23 PM
  • Yep I liked that better too. Thnaks
    Wednesday, March 27, 2013 11:12 PM
  • I'm sure this violates some sort of "proper code" rule but this also works:

    Private Sub SelectDate(sender As Object, _
                                      e As System.EventArgs) Handles mskDate.GotFocus
    
            SendKeys.Send("^a")
    
    End Sub

    Monday, August 31, 2015 2:58 AM
  • It seems it works perfectly using the GetFocus event instead:

    Private Sub mtb_GotFocus(sender As Object, e As EventArgs) Handles mtb.GotFocus
              mtb_tlf.SelectAll()
    End Sub
    Monday, January 25, 2016 9:28 PM