-
Oct 6th, 2015, 10:08 AM
#1
Thread Starter
Junior Member
[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?
-
Oct 6th, 2015, 10:23 AM
#2
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
-
Oct 6th, 2015, 10:29 AM
#3
Thread Starter
Junior Member
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.
-
Oct 6th, 2015, 10:39 AM
#4
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.
-
Oct 6th, 2015, 10:42 AM
#5
Thread Starter
Junior Member
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
-
Oct 6th, 2015, 11:17 AM
#6
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.
-
Oct 6th, 2015, 11:27 AM
#7
Thread Starter
Junior Member
Re: MaskedTextbox cursor placed at the beginning when clicked
Thats not what i need in my form
Here's a picture to clarify
The maskedtextbox needs to be filed with all that data
-
Oct 6th, 2015, 12:52 PM
#8
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
-
Oct 6th, 2015, 01:23 PM
#9
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
-
Oct 6th, 2015, 01:51 PM
#10
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.
-
Oct 7th, 2015, 04:40 AM
#11
Thread Starter
Junior Member
Re: MaskedTextbox cursor placed at the beginning when clicked
Originally Posted by TnTinMN
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|