Hi everybody, I am using VB.NET (2005), I have a problem with MaskedTextbox. Not always , but strangly some times I got the problem. I was useing MaskedTextbox for date input and I set the date format to ShortDateString (dd/MM/yyyy). In most cases it works fine. Ex: 01/05/2008
When itgot fucus all contents would be selected with SelectAll(). Ex: 01/05/2008
It also work fine Problem: But, When First Digit Enters in control, all mask gone and the only digit displays. Ex: 1 All Mask gone and no more digits accepting, and if we delete one character with Delete Key or Backspace Key, remaining text displays. When Ipress Backspace, it looks Like below. Ex: 50/52/008_ I got so much of trouble with this problem. Can anybody give me any solution or suggestion. Thanks in advance, Raju. - Edited byCh.Appa Rao Monday, March 16, 2009 9:53 AM
-
| | Ch.Appa Rao Monday, March 09, 2009 10:56 AM | I found the solution,
Instead of useing the KeyPress event the KeyDown event not raising this problem. The Keydown event as follows
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then SendKeys.Send("{tab}") End If End Sub
---- Raju - Marked As Answer byCh.Appa Rao Saturday, March 21, 2009 5:58 AM
-
| | Ch.Appa Rao Saturday, March 21, 2009 5:58 AM | Hi Raju,
> But, When First Digit Enters in control, all mask gone and the only digit displays.
I performed a test on this but didn't reproduce the problemon my side.
> if we delete one character with Delete Key or Backspace Key, remaining text displays. When Ipress Backspace, it looks Like below. Ex: 50/52/008_
This behavior is by design. A workaround is to handle the KeyUp event of the MaskedTextBox to insert a " " to the text in the appropriate position. The following is a sample:
void maskedTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
int position = this.maskedTextBox1.SelectionStart;
char c = this.maskedTextBox1.Text[position];
if (!c.Equals('-'))
{
this.maskedTextBox1.Text = this.maskedTextBox1.Text.Insert(position, " ");
this.maskedTextBox1.SelectionStart = position;
}
}
}
The above code works fine in my application. Please try it to see if it is what you want.
Hope this helps.
Sincerely, Linda Liu - Unmarked As Answer byCh.Appa Rao Thursday, March 19, 2009 7:24 AM
- Marked As Answer byLinda LiuMSFT, ModeratorMonday, March 16, 2009 4:15 AM
-
| | Linda Liu Friday, March 13, 2009 6:01 AM | Madam Linda Liu,
Thanks for your reply. But, I am using VB.Net. I converted your code from C to VB.Net and checked, but not worked.
BackSpace or DeleteKey is not my problem. Whenall MaskedTextbox contestsselected, if we press any key,all Mask disappears (it may be moved to left or right but not visible) and entered character is displaying and no more characters accepting in to the control.
When testing the control, the problem comes once or twice in a 10 times test. But when user enter the input, This problem comes often.
Kindly give me any suggestions, or Iwill use the MaskedTextbox without using SELECTALL() method.
Sincerely Raju. | | Ch.Appa Rao Monday, March 16, 2009 9:52 AM | Hi Raju,
Thank you for your reply.
> Whenall MaskedTextbox contestsselected, if we press any key,all Mask disappears (it may be moved to left or right but not visible) and entered character is displaying and no more characters accepting in to the control.
I still couldn't reproduce the problem on my side. I perform tests in my XP and Vista machines. What's the OS version on your pc?
Sincerely, Linda Liu | | Linda Liu Friday, March 20, 2009 2:55 AM | Madam, Thank you very much for your cooperation. and i am copying the entier code below. the FORM1 contains Two controls, one MaskedTextbox control and an ordinaryTextbox (for getting focus)
''''''' The Complete Code
Option Strict On Option Explicit On Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress If e.KeyChar = Convert.ToChar(Keys.Return) Then System.Windows.Forms.SendKeys.Send("{tab}") e.Handled = True End If End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MaskedTextBox1.Mask = "99/99/9999" MaskedTextBox1.Text = Today.ToShortDateString KeyPreview = True End Sub
Private Sub MaskedTextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.GotFocus MaskedTextBox1.SelectAll() End Sub End Class
''''I found the problme, and the problem raising when Form's KeyPress event traping ''''When Press Enter Key, the Focus go to next control ''''the following function (already mentioned above)''''''''''''''
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress If e.KeyChar = Convert.ToChar(Keys.Return) Then System.Windows.Forms.SendKeys.Send("{tab}") e.Handled = True End If End Sub
'''''''What if the above KeyPress event not used, there is no any problem ''''''' But this event is required in my environment.
Is there any alternative for this Keypress event. Please help me.
Thanks in advance. Raju | | Ch.Appa Rao Saturday, March 21, 2009 5:52 AM | I found the solution,
Instead of useing the KeyPress event the KeyDown event not raising this problem. The Keydown event as follows
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then SendKeys.Send("{tab}") End If End Sub
---- Raju - Marked As Answer byCh.Appa Rao Saturday, March 21, 2009 5:58 AM
-
| | Ch.Appa Rao Saturday, March 21, 2009 5:58 AM |
|