|
I needed to allow the clearing of a time field so that i can recognise that no time had been entered so i used your code as a starting point and added a few specifics to make it 'time only' and made it a specific format to make it simpler for the user and so i can easily validate the text as a time.
Thanks
|
|
|
|
|
Thanks for the simplistic code that was easy to read and modify for my purposes. There are several masked edit controls in this forum that honestly don't work or were poorly tested. I thought I would share the enhancements to my version for the stated valid reasons. My scope for these changes was around date formatting/editing.
1. If a user selects the entire field for delete (to re-type), the control should clear out the entire field. Once the user starts typing, the mask automatically fills in and the user can start typing the new date. Here is my code addition:
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)<br />
Dim tmpset = Me.SelectionStart<br />
<br />
On Error Resume Next<br />
Select Case e.KeyCode<br />
Case Keys.Delete<br />
If Len(Me.SelectedText) = Len(Me.Text) Then<br />
Me.Text = String.Empty<br />
Exit Select<br />
End If <br />
Me.Text = ""<br />
For a = tmpset To aMask.Length - 1<br />
Select Case aMskMask.GetValue(a + 1)<br />
Case ".", "-", "\", "/", ","<br />
aMask.SetValue(aMask.GetValue(a + 2), a)<br />
a = a + 1<br />
Case Else<br />
aMask.SetValue(aMask.GetValue(a + 1), a)<br />
End Select<br />
Next<br />
<br />
aMask.SetValue(CType("_", Char), aMask.Length - 1)<br />
<br />
e.Handled = True<br />
<br />
Me.Text = ""<br />
For a = 0 To aMask.Length - 1<br />
Me.Text += aMask.GetValue(a)<br />
Next<br />
Me.SelectionStart = tmpset<br />
<br />
End Select<br />
End Sub
2. With SQL Server, DateTime values with leading zeroes are stored as m/d/yyyy, mm/d/yyyy and m/dd/yyyy (no leading zeroes). This creates havoc in a control when attempting to edit the "as stored" value since most data entry is done heads down without concern of field delimiters. I added the following events to handle re-formatting the date when first entering then leaving the control. These events also handle any input field defined with a mask of ##/##/#### and the user does not enter the Century date value.
Note: You should always set the "MaxLength" property on MaskedBox to either 8 or 10 (for dates) so that the user can automatically move to the next control.
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)<br />
MyBase.Text = Me.FormatData(Me.Text())<br />
End Sub<br />
<br />
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)<br />
MyBase.Text = Me.FormatData(Me.Text())<br />
End Sub<br />
<br />
Protected Function FormatData(ByVal sText As String) As String<br />
<br />
If sText Is Nothing Or sText Is String.Empty Then<br />
Exit Function<br />
Else<br />
sText = sText.Replace("_", "")<br />
<br />
If Len(Me.Mask) < 10 Then<br />
Try<br />
Dim MyDate As DateTime = CType(sText, Date)<br />
Return MyDate.ToString("MM/dd/yy")<br />
<br />
Catch<br />
<br />
Return sText<br />
<br />
End Try<br />
Else<br />
Try<br />
Dim MyDate As DateTime = CType(sText, Date)<br />
Return MyDate.ToString("MM/dd/yyyy")<br />
<br />
Catch<br />
<br />
Return sText<br />
<br />
End Try<br />
End If<br />
<br />
End If<br />
<br />
End Function<br />
3. For those folks using SQL Server with controls bound to datasets that don't want to show the "as stored" date value from SQL Server, add the following handler and subroutine for all date field reformatting in your form.
Note: User-Defined Date/Time Formats (Format Function) - Unlike previous versions of Visual Basic, the Format function characters are case-sensitive. "MM" is not the same as "mm".
<br />
AddHandler txtMyDateField1.DataBindings("Text").Format, AddressOf Format<br />
AddHandler txtMyDateField2.DataBindings("Text").Format, AddressOf Format<br />
<br />
Public Sub Format(ByVal sender As Object, ByVal e As Windows.Forms.ConvertEventArgs)<br />
'This subroutine converts any SQL Server timedate to mm/dd/yy format<br />
If Not IsDBNull(e.Value) Then<br />
Dim MyDate As DateTime = CType(e.Value, Date)<br />
e.Value = MyDate.ToString("MM/dd/yy")<br />
End If<br />
End Sub
Enjoy!
|
|
|
|
|
great idea !
when i made this control i don't have know too much the framework.
|
|
|
|
|
Hello, only word don´t number..how????
i need more
|
|
|
|
|
Hi,
I have downloaded this control and using the same dll - but somehow I am not able to key in any thing from keyboard. As soon as I press any key from keyboard cursor moves further a place - but somehow it doesn't write anything. Could anyone helps me in finding the error?
Urgent Help needed!!!!
|
|
|
|
|
Great little project. One small problem. If you hit backspace when you are at possition 0, it jumps out to position 1.
I added this test to OnKeyPress() event. That fixed the problem.
If Asc(e.KeyChar) = 8 Then ' original code
' JPM added this check.
' The select start ended up at position 1 with out this.
If (tmpset = 0) Then
Exit Sub
End If
Thanks for the control,
joe
|
|
|
|
|
Just so people know,... here is the first part of that sub, with the new code ....
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
On Error Resume Next
Dim tmpset = Me.SelectionStart
Dim tmpCH As Char
If Me.ReadOnly = True Then Exit Sub
If Asc(e.KeyChar) = 8 Then 'BACKSPACE
'If you are at position 0, this stops it from jumping out to position 1
If (tmpset = 0) Then Exit Sub
e.Handled = True
If Me.SelectionLength = 0 Then
If InStr(ALLOW_CHARS, CType(aMskMask.GetValue(tmpset - 1), String), CompareMethod.Text) > 0 Then
tmpset = tmpset - 2
aMask.SetValue(CType("_", Char), tmpset)
tmpset = tmpset - 1
......
|
|
|
|
|
Thanks so much for the BACKSPACE solution "Asc(e.KeyChar) = 8" - for some reason, "e.KeyChar.Equals(Keys.Back)" and everything else I tried did not work, and it took me a long while to find your solution here.
Thanks!
|
|
|
|
|
Nice control. Just wondering if it is possible to have the control accept more than just /'s and -'s as part of the mask? For example, creating a mask like this:
###-aaa/&&&(##)
The mask does work, but it forces the user to account for the a's when entering the data into the field.
Any help you can give would be greatly appreciated.
|
|
|
|
|
|
There are 2 bugs with this code.
1) When the control loads, the mask is not used when a value is assigned. You can test this by: In the Form_Load event, assign a value to the control. When the form appears, the mask has NOT been used.
2) Using the same code you have in the above example, when the form appears CLICK anywhere in the control to release the highlight, and then press the Backspace key. All the text in the control is deleted.
I can tell you've done a lot of work, but this control is far from being a "final release". Sorry.
Update: I went through it and i added in a totally new procedure that fixes the errors. Just add this extra code in and rebuild the app.
<br />
Public Sub SetTextWithUnmaskedText(ByVal sText As String)<br />
'Use this sub to apply text to the control when incoming text does not have the mask already in it.<br />
'For example, during Form_Load, pass in the phone number without dashes or brackets.<br />
On Error Resume Next<br />
If sText = "" Then<br />
For a = 0 To tmpMask.Length - 1<br />
If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then<br />
aMask.SetValue(CType("_", Char), a)<br />
Else<br />
aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)<br />
End If<br />
Next a<br />
Else<br />
Dim iPos As Integer = 0<br />
For iLpr As Integer = 0 To tmpMask.Length - 1<br />
If tmpMask.Substring(iLpr, 1) = "#" Or tmpMask.Substring(iLpr, 1) = "&" Then<br />
aMask.SetValue(CType(sText.Substring(iPos, 1), Char), iLpr)<br />
iPos += 1<br />
Else<br />
aMask.SetValue(CType(tmpMask.Substring(iPos, 1), Char), iLpr)<br />
End If<br />
Next iLpr<br />
End If<br />
<br />
Me.Text = ""<br />
For iFill As Integer = 0 To aMask.Length - 1<br />
Me.Text += aMask.GetValue(iFill)<br />
Next iFill<br />
End Sub<br />
|
|
|
|
|
Hi,
I have downloaded this control and using the same dll - but somehow I am not ab;e to key in any thing from keyboard. As soon as I press any key from keyboard cursor moves further a place - but somehow it doesn't write anything. Could anyone helps me in finding the error?
hi
|
|
|
|
|
hi man
just remove the . or - etc and type some thing see
bala
|
|
|
|
|
Hi,
I have downloaded this control and using the same dll - but somehow I am not ab;e to key in any thing from keyboard. As soon as I press any key from keyboard cursor moves further a place - but somehow it doesn't write anything. Could anyone helps me in finding the error?
hi
|
|
|
|
|
I haven't yet tried the component, but it looks good for what I will use it for, save me some time anway.
But please be revise your code with Option Strict On, you have quite a few implicit conversions in there... and implicit declerations.
|
|
|
|
|
Hi,
I have downloaded this control and using the same dll - but somehow I am not ab;e to key in any thing from keyboard. As soon as I press any key from keyboard cursor moves further a place - but somehow it doesn't write anything. Could anyone helps me in finding the error?
hi
|
|
|
|
|
cool, nice component,
regardless have a look to the delete key effect,
i think it add a mask character to the default mask...
Exemple MASK = # ### ### ###
Your text box mask = _ ___ ___ ___
If you delete all character with suppr key then
text box mask = _____________
???
|
|
|
|
|
Hi,
I have downloaded this control and using the same dll - but somehow I am not ab;e to key in any thing from keyboard. As soon as I press any key from keyboard cursor moves further a place - but somehow it doesn't write anything. Could anyone helps me in finding the error?
hi
|
|
|
|
|
The Keys enumeration doesn't exactly match the ASCII character codes. For example, try using the number pad.
Also, you need to take the modifier keys into account - try holding down shift and typing in a numeric box.
Since you only need to worry about numbers and letters, you could try something like:
Protected Overridable Function GetCharFromKeys(ByVal key As Keys) As Char
Dim keyCode As Keys = key And Not Me.ModifierKeys
Select Case keyCode
'A-Z
Case Keys.A To Keys.Z
If key And Keys.Shift Then
Return Char.ToUpper(Chr(keyCode))
Else
Return Char.ToLower(Chr(keyCode))
End If
'0-9
Case Keys.D0 To Keys.D9
If key And Keys.Shift Then
Return Chr(0)
Else
Return Chr(keyCode)
End If
'0-9 (number pad)
Case Keys.NumPad0 To Keys.NumPad9
'NumPad0 - D0 = 48
Return Chr(keyCode - 48)
Case Else
Return Chr(0)
End Select
End Function
...
Dim tmpCH As Char = GetCharFromKeys(e.KeyData)
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Thank you for this message i'll update my code.
|
|
|
|