 |
|
 |
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)
Dim tmpset = Me.SelectionStart
On Error Resume Next
Select Case e.KeyCode
Case Keys.Delete
If Len(Me.SelectedText) = Len(Me.Text) Then
Me.Text = String.Empty
Exit Select
End If
Me.Text = ""
For a = tmpset To aMask.Length - 1
Select Case aMskMask.GetValue(a + 1)
Case ".", "-", "\", "/", ","
aMask.SetValue(aMask.GetValue(a + 2), a)
a = a + 1
Case Else
aMask.SetValue(aMask.GetValue(a + 1), a)
End Select
Next
aMask.SetValue(CType("_", Char), aMask.Length - 1)
e.Handled = True
Me.Text = ""
For a = 0 To aMask.Length - 1
Me.Text += aMask.GetValue(a)
Next
Me.SelectionStart = tmpset
End Select
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)
MyBase.Text = Me.FormatData(Me.Text())
End Sub
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
MyBase.Text = Me.FormatData(Me.Text())
End Sub
Protected Function FormatData(ByVal sText As String) As String
If sText Is Nothing Or sText Is String.Empty Then
Exit Function
Else
sText = sText.Replace("_", "")
If Len(Me.Mask) < 10 Then
Try
Dim MyDate As DateTime = CType(sText, Date)
Return MyDate.ToString("MM/dd/yy")
Catch
Return sText
End Try
Else
Try
Dim MyDate As DateTime = CType(sText, Date)
Return MyDate.ToString("MM/dd/yyyy")
Catch
Return sText
End Try
End If
End If
End Function
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".
AddHandler txtMyDateField1.DataBindings("Text").Format, AddressOf Format
AddHandler txtMyDateField2.DataBindings("Text").Format, AddressOf Format
Public Sub Format(ByVal sender As Object, ByVal e As Windows.Forms.ConvertEventArgs)
'This subroutine converts any SQL Server timedate to mm/dd/yy format
If Not IsDBNull(e.Value) Then
Dim MyDate As DateTime = CType(e.Value, Date)
e.Value = MyDate.ToString("MM/dd/yy")
End If
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.
Public Sub SetTextWithUnmaskedText(ByVal sText As String)
'Use this sub to apply text to the control when incoming text does not have the mask already in it.
'For example, during Form_Load, pass in the phone number without dashes or brackets.
On Error Resume Next
If sText = "" Then
For a = 0 To tmpMask.Length - 1
If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
aMask.SetValue(CType("_", Char), a)
Else
aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
End If
Next a
Else
Dim iPos As Integer = 0
For iLpr As Integer = 0 To tmpMask.Length - 1
If tmpMask.Substring(iLpr, 1) = "#" Or tmpMask.Substring(iLpr, 1) = "&" Then
aMask.SetValue(CType(sText.Substring(iPos, 1), Char), iLpr)
iPos += 1
Else
aMask.SetValue(CType(tmpMask.Substring(iPos, 1), Char), iLpr)
End If
Next iLpr
End If
Me.Text = ""
For iFill As Integer = 0 To aMask.Length - 1
Me.Text += aMask.GetValue(iFill)
Next iFill
End Sub
|
|
|
|
 |
|
 |
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
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
Case Keys.D0 To Keys.D9
If key And Keys.Shift Then
Return Chr(0)
Else
Return Chr(keyCode)
End If
Case Keys.NumPad0 To Keys.NumPad9
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.
|
|
|
|
 |