Results 1 to 11 of 11

Thread: [RESOLVED] MaskedTextbox cursor placed at the beginning when clicked

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    16

    Resolved [RESOLVED] MaskedTextbox cursor placed at the beginning when clicked

    Hi everyone

    I have a maskedTextbox in a form.
    My problem is when the user clicks on the empty box the cursor stays where they clicked it, not at the beginning of the box as it would with a normal textbox.

    I've used this line:
    Code:
    Private Sub SolDeb_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SolDeb.MouseClick
            SolDeb.SelectionStart = 0
     End Sub
    But everytime the user clicks it goes to the beginning, if you already have something writen it doesn't let you change it from where you want to.

    Is there a way to make the cursor go to the beginning of the Maskedtextbox when the user Clicks it IF it's blank BUT if it has something writen let the cursor stay where the user clicks?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: MaskedTextbox cursor placed at the beginning when clicked

    You could add a condition to check if the string returned from the Text is empty:
    Code:
    Private Sub SolDeb_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SolDeb.MouseClick
        If String.IsNullOrWhiteSpace(SolDeb.Text) Then
            SolDeb.SelectionStart = 0
        End If
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    16

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Thanks for the reply dday9

    For some reason it doesn't work

    I've tried like this also but with the same luck
    Code:
    Private Sub SolDeb_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SolDeb.MouseClick
    If SolDeb.text = "" then
            SolDeb.SelectionStart = 0
    End if
     End Sub
    If you add your code it just stays where the user clicks always being the maskedtextbox empty or not


    it's because of the mask i'm using(0-000000000000)
    I've tried it in a simple 5 digit mask and it worked fine

    Is there a way to make this but with the other mask?(0-000000000000)
    Last edited by Iceman1988; Oct 6th, 2015 at 10:39 AM.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: MaskedTextbox cursor placed at the beginning when clicked

    See, that is why I don't like how the MaskedTextBox works. In HTML5 with the pattern attribute it simply validates that the user's entered in a valid pattern where as in the MaskedTextBox it automatically adds in some default value. I do not know how help you on this one if you're insistent on using a MaskedTextBox.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    16

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Humm the reason i'm using a maskedtextbox is to make the box numeric but allow users to copy paste numeric value's

    I've tried with a regular Textbox but couldn't get all i wanted

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: MaskedTextbox cursor placed at the beginning when clicked

    If you want numeric input, then your best option is a NumericUpDown control. It is very customizable and I would highly suggest that you check it out.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    16

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Thats not what i need in my form

    Name:  Teste1.BMP
Views: 1227
Size:  349.3 KB

    Here's a picture to clarify

    The maskedtextbox needs to be filed with all that data

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Could you disable the "next" button until the user enters a valid number? This could be done by using RegEx and a TextBox:
    Code:
    Private Sub MyTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles MyTextBox.TextChanged
        NextButton.Enabled = System.Text.RegularExpressions.Regex.IsMatch(MyTextBox.Text, "\d-\d{12}")
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: MaskedTextbox cursor placed at the beginning when clicked

    You can use this to determine if the MaskedTextBox is empty.
    Code:
    Public Shared Function MaskedTextBoxIsEmpty(mtb As MaskedTextBox) As Boolean
       Dim isEmpty As Boolean
       With mtb
          Dim oldFormat As MaskFormat = .TextMaskFormat
          .TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
          isEmpty = String.IsNullOrWhiteSpace(.Text)
          .TextMaskFormat = oldFormat
       End With
       Return isEmpty
    End Function

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Every time I've tried to use MaskedTextBox it's been a major headache. It tries really hard to do a good thing, but it's ultimately more trouble than it's worth. Think about all the applications you use daily. How many of them use a MaskedTextBox? It doesn't seem like many other people bother with it, either.

    It's not even fun as the user. I find when I do have to use one I'm never quite sure what's going to happen when I push a key. Is this one allowed? Can I backspace? Will the whole thing reset if I make a mistake? It's terrible.

    If you want a numeric input, there's always NumericUpDown. It has a couple of burdens of its own, but it's not quite as aggravating.

    But personally, I'd use a good old TextBox. It already works like you want, and people are used to using it. Not many people get mad when you say "Please enter your number, for example 1-2345" and their "44444" is rejected.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    16

    Re: MaskedTextbox cursor placed at the beginning when clicked

    Quote Originally Posted by TnTinMN View Post
    You can use this to determine if the MaskedTextBox is empty.
    Code:
    Public Shared Function MaskedTextBoxIsEmpty(mtb As MaskedTextBox) As Boolean
       Dim isEmpty As Boolean
       With mtb
          Dim oldFormat As MaskFormat = .TextMaskFormat
          .TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
          isEmpty = String.IsNullOrWhiteSpace(.Text)
          .TextMaskFormat = oldFormat
       End With
       Return isEmpty
    End Function
    This worked exactly like i wanted

    Cheers my friend very helpful indeed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width