Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Newbie here!!

I am creating a custom textbox control based on Forms.TextBox. It should

a) only take individual values separated by commas (1,2,3)
b) a valid range separated by numbers (5-9 but not 9-5)
c) should not accept comma in the first character
d) autocorrect comma in the last character


I am overriding the KeyPress propoerty as follows but unable to get access to the text value entered so far to build the validations above. Please help.

Thanks,

VB
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)


    If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8 Then
                    e.Handled = False

        ElseIf e.KeyChar = "," Then


    ElseIf e.KeyChar = "-"

        'Struggling with the Code for validation
    Else

        'Struggling with the Code for validation
    End if

End Sub
Posted
Updated 12-Oct-14 6:13am
v2
Comments
Maciej Los 12-Oct-14 11:15am    
What's the question?
Member 11127394 12-Oct-14 12:12pm    
Can you help me with the code to build validations? The logic:

a) It can't start with a commma or -
b) Cannot have 2 consecutive commas
c) Cannot have a '-' after a comma
d) Should validate a range (should not allow 9-5)

I need to somehow get access to the text entered so far...dont know how

Thanks

Just one idea: try to use the different class: System.Windows.Forms.MaskedTextBox, or derive from it:
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox%28v=vs.110%29.aspx[^].

Your post is not really a question, this is a request to do part of your work, some UI design. To make your posts more useful, you could rather try to do a design and ask more specific questions when you face some problems, when some behaviors show some flaws, something like that.

—SA
 
Share this answer
 
Comments
Maciej Los 12-Oct-14 14:57pm    
+5
Sergey Alexandrovich Kryukov 12-Oct-14 16:04pm    
Thank you, Maciej.
—SA
Further to Solution 1 (recommended), assuming you stick with your current approach then TextBox1.Text is the contents of the text box.

However, I think you should use Validating event in preference to the OnKeyPress event.

You can still use OnKeyPress to ensure that only 0-9, comma and backspace are accepted e.g.
VB
If (Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57) And Not (New Integer() {8, Asc("-"), Asc(",")}.Contains(Asc(e.KeyChar))) Then
    e.Handled = True

Your attempt was close but you need to set e.Handled = True to ignore the key
 
Share this answer
 
Comments
Maciej Los 12-Oct-14 14:57pm    
+5
Sergey Alexandrovich Kryukov 12-Oct-14 16:05pm    
Good point here it to use OnKeyPress for filtering out unwanted input. My 5.
—SA
Another way is to create Regex validator ;)

Please, see: Validation with Regular Expressions Made Simple[^]
 
Share this answer
 
Comments
CHill60 12-Oct-14 15:05pm    
Of course! 5.
Maciej Los 12-Oct-14 15:21pm    
Thank you, Caroline ;)
Sergey Alexandrovich Kryukov 12-Oct-14 16:07pm    
I hope it would be possible to avoid validation (to keep input also valid), but validation is just another aspect unrelated to filtering input.
—SA
Maciej Los 12-Oct-14 16:25pm    
To be honest, i'm not sure i understand you correctly... Why, in your opinion, validation is unrelated to filtering input? In which aspect?
Sergey Alexandrovich Kryukov 12-Oct-14 16:34pm    
I just reviewed the question. Yes, it could be just validation, up-voted.
The point is: 1) filtering is useful and comes first; 2) if filtering overly complex, could confuse the users; 3) validation may or may not be needed; it's best when it's not needed, but it's better then complicated confusing filtering.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900