How do you cancel a keypress in VB.NET CF?

B

Brian McVay

Hi all,

I will state my problem first:
Using the VS 2003 VB.NET Smart Device project and my own extension of the
base MS textbox device control, How can I cancel an operation and prevent
unwanted character from being entered into a textbox? Or how can I undo
the operation before the textbox shows the characters entered?

Then my explanation of the situation:
I am currently developing an application targeted for the .NET CF for
Windows Mobile and have requirements surrounding text entry into textboxes.

The simple version is that I have to restrict the text entry from textbox to
textbox of acceptable characters including the following criteria: Numeric
Only, Numeric and Letters only, and also to restrict special characters to
only a few special characters for numerous differing criteria.

I have reviewed a couple of third party masked edit boxes, but due to
several reasons they are not going to be acceptable for my implementation.

I have extended the base textbox class to create my own control that
contains validation methods in the OnKeyPress and OnTextChanged methods.

Currently I have a workaround that stores a snapshot of the current state of
the textbox prior to the change and if the entry is invalid, i simpy restore
the textbox to the previous text and textselection settings, but this causes
a lot of overhead as the procedures get kicked off again just to restore the
previous text and is unsightly as the text gets entered and then stripped
out.

I wish to be able to cancel (coming from VB6 background) the keypress method
so that the operations are simply exited or somehow to undo the operation
before the unsightly entry and stripping out occurs. Undo() is not supported
for the compact framework for the existing control.

I have looked into some third party controls, such as the openNetCF
TextBoxEx, but have not found the functionality that im looking for or do
not realize how to implement the solution with those controls and would like
to keep the project to the base controls that shiped with VS2003 and just
one or two custom controls to control program bloat, overhead, resource
sizes, and minimize the toolset needed for this application for continued
maintenance and support operations if at all possible.

Any and all assistance will be greatly appreciated.

Thanks in advance!

Brian
 
E

Edward E. Martins Jr. via DotNetMonster.com

Brian.
Try this to control text box:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/mwdesign/html/mwconIntroductiontotheTextBoxControl.asp

with relation to undo, you can put in the string or string[n] the boxtext,
and in one function you can read the string to each Ctrl+Z...

This code capture the key press, key down and key up...

public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler (Form1_KeyDown);
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
}

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
MessageBox.Show(Convert.ToString(e.KeyChar));
}//fim Form1_KeyPress

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyDown

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyUp
 
B

Brian McVay

Thanks for the reply.

I was hopeful that I could incorporate this functionality within the derived
control class object itself so that every implementation of the control
would act identically and would not require recoding every form for event
handlers, but instead to use the derived controls overriden methods to
compare the attempted text entry with properties that would be set for each
textbox and have the control itself reject (cancel) the invalid attempted
entry based on the property settings for that paticular object.

A simplified (and not perfected or refactored yet) example follows:

I set up a property to accept an array of characters that will be allowed in
the textbox seperate from numeric or letter values. Any other character
should be discarded or disallowed.

Within the form I have:
Dim WithEvents myTextControl as New clsMyTextBoxControl
Dim mySpecialCharacters as Char() = {",",".","$"} 'for example:
currency symbols
myTextControl.AllowSpecialCharacters = mySpecialCharacters 'sets controls
property of special characters to be allowed other than numeric

Within the myTextControl control itself I have:

Public Class clsMyTextBoxControl
Private arrChrAllowSpecial() as Char
Private blnAllowNumericOnly As Boolean
Public Shadows Event KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

Public WriteOnly Property AllowSpecialCharacters() As Array
Set(ByVal Value As Array)
arrChrAllowSpecial = Value
End Set
End Property

Public Property NumericOnly() As Boolean
Get
Return blnAllowNumericOnly
End Get
Set(ByVal Value As Boolean)
blnAllowNumericOnly = Value
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
'check character pressed against the valid characters
If Not ValidateKeyPress(e) Then
'***************************
'TBD - HOW DO I CANCEL THE KEYPRESS WITHIN THE TEXTBOX OBJECT?
'Me.Undo() 'Unsupported by the CF
'***************************
Else
RaiseEvent KeyPress(Me, e)
End If
End Sub

Private Function ValidateKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs) As Boolean
Dim intFoundValue As Integer

Select Case Asc(e.KeyChar)
Case 48 To 57 'numerics
Return True
Case 65 To 90 ' upper case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 97 To 122 'lower case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 8, 27, 16 'backspace, escape, delete
'allow these values
Return True
Case Else 'all special characters and other values
If IsNothing(arrChrAllowSpecial) Then
Return False
Else
intFoundValue =
arrChrAllowSpecial.BinarySearch(arrChrAllowSpecial, Chr(Asc(e.KeyChar)))
If Not arrChrAllowSpecial(intFoundValue) =
CStr(e.KeyChar) Then
Return False
Else
Return True
End If
End If
End Select
End Function

End Class


Am I attempting something that cannot be done?

Thanks for any and all help.


Brian


Edward E. Martins Jr. via DotNetMonster.com said:
Brian.
Try this to control text box:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/mwdesign/html/mwconIntroductiontotheTextBoxControl.asp

with relation to undo, you can put in the string or string[n] the boxtext,
and in one function you can read the string to each Ctrl+Z...

This code capture the key press, key down and key up...

public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler (Form1_KeyDown);
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
}

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
MessageBox.Show(Convert.ToString(e.KeyChar));
}//fim Form1_KeyPress

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyDown

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyUp
 
S

Sergey Bogdanov

"e.Handled = false" instead of Undo is what you are looking for.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Brian said:
Thanks for the reply.

I was hopeful that I could incorporate this functionality within the derived
control class object itself so that every implementation of the control
would act identically and would not require recoding every form for event
handlers, but instead to use the derived controls overriden methods to
compare the attempted text entry with properties that would be set for each
textbox and have the control itself reject (cancel) the invalid attempted
entry based on the property settings for that paticular object.

A simplified (and not perfected or refactored yet) example follows:

I set up a property to accept an array of characters that will be allowed in
the textbox seperate from numeric or letter values. Any other character
should be discarded or disallowed.

Within the form I have:
Dim WithEvents myTextControl as New clsMyTextBoxControl
Dim mySpecialCharacters as Char() = {",",".","$"} 'for example:
currency symbols
myTextControl.AllowSpecialCharacters = mySpecialCharacters 'sets controls
property of special characters to be allowed other than numeric

Within the myTextControl control itself I have:

Public Class clsMyTextBoxControl
Private arrChrAllowSpecial() as Char
Private blnAllowNumericOnly As Boolean
Public Shadows Event KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

Public WriteOnly Property AllowSpecialCharacters() As Array
Set(ByVal Value As Array)
arrChrAllowSpecial = Value
End Set
End Property

Public Property NumericOnly() As Boolean
Get
Return blnAllowNumericOnly
End Get
Set(ByVal Value As Boolean)
blnAllowNumericOnly = Value
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
'check character pressed against the valid characters
If Not ValidateKeyPress(e) Then
'***************************
'TBD - HOW DO I CANCEL THE KEYPRESS WITHIN THE TEXTBOX OBJECT?
'Me.Undo() 'Unsupported by the CF
'***************************
Else
RaiseEvent KeyPress(Me, e)
End If
End Sub

Private Function ValidateKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs) As Boolean
Dim intFoundValue As Integer

Select Case Asc(e.KeyChar)
Case 48 To 57 'numerics
Return True
Case 65 To 90 ' upper case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 97 To 122 'lower case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 8, 27, 16 'backspace, escape, delete
'allow these values
Return True
Case Else 'all special characters and other values
If IsNothing(arrChrAllowSpecial) Then
Return False
Else
intFoundValue =
arrChrAllowSpecial.BinarySearch(arrChrAllowSpecial, Chr(Asc(e.KeyChar)))
If Not arrChrAllowSpecial(intFoundValue) =
CStr(e.KeyChar) Then
Return False
Else
Return True
End If
End If
End Select
End Function

End Class


Am I attempting something that cannot be done?

Thanks for any and all help.


Brian


Brian.
Try this to control text box:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/mwdesign/html/mwconIntroductiontotheTextBoxControl.asp

with relation to undo, you can put in the string or string[n] the boxtext,
and in one function you can read the string to each Ctrl+Z...

This code capture the key press, key down and key up...

public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler (Form1_KeyDown);
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
}

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
MessageBox.Show(Convert.ToString(e.KeyChar));
}//fim Form1_KeyPress

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyDown

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyUp
 
B

Brian McVay

Perfect!

I was able to cancel the input in the object by setting e.handled = true in
this implementation.

Thanks a ton Sergey.

Brian

Sergey Bogdanov said:
"e.Handled = false" instead of Undo is what you are looking for.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Brian said:
Thanks for the reply.

I was hopeful that I could incorporate this functionality within the
derived control class object itself so that every implementation of the
control would act identically and would not require recoding every form
for event handlers, but instead to use the derived controls overriden
methods to compare the attempted text entry with properties that would be
set for each textbox and have the control itself reject (cancel) the
invalid attempted entry based on the property settings for that paticular
object.

A simplified (and not perfected or refactored yet) example follows:

I set up a property to accept an array of characters that will be allowed
in the textbox seperate from numeric or letter values. Any other
character should be discarded or disallowed.

Within the form I have:
Dim WithEvents myTextControl as New clsMyTextBoxControl
Dim mySpecialCharacters as Char() = {",",".","$"} 'for example:
currency symbols
myTextControl.AllowSpecialCharacters = mySpecialCharacters 'sets
controls property of special characters to be allowed other than numeric

Within the myTextControl control itself I have:

Public Class clsMyTextBoxControl
Private arrChrAllowSpecial() as Char
Private blnAllowNumericOnly As Boolean
Public Shadows Event KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

Public WriteOnly Property AllowSpecialCharacters() As Array
Set(ByVal Value As Array)
arrChrAllowSpecial = Value
End Set
End Property

Public Property NumericOnly() As Boolean
Get
Return blnAllowNumericOnly
End Get
Set(ByVal Value As Boolean)
blnAllowNumericOnly = Value
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
'check character pressed against the valid characters
If Not ValidateKeyPress(e) Then
'***************************
'TBD - HOW DO I CANCEL THE KEYPRESS WITHIN THE TEXTBOX
OBJECT?
'Me.Undo() 'Unsupported by the CF
'***************************
Else
RaiseEvent KeyPress(Me, e)
End If
End Sub

Private Function ValidateKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs) As Boolean
Dim intFoundValue As Integer

Select Case Asc(e.KeyChar)
Case 48 To 57 'numerics
Return True
Case 65 To 90 ' upper case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 97 To 122 'lower case characters
'allow these values if not numeric only
If NumericOnly Then Return False
Case 8, 27, 16 'backspace, escape, delete
'allow these values
Return True
Case Else 'all special characters and other values
If IsNothing(arrChrAllowSpecial) Then
Return False
Else
intFoundValue =
arrChrAllowSpecial.BinarySearch(arrChrAllowSpecial, Chr(Asc(e.KeyChar)))
If Not arrChrAllowSpecial(intFoundValue) =
CStr(e.KeyChar) Then
Return False
Else
Return True
End If
End If
End Select
End Function

End Class


Am I attempting something that cannot be done?

Thanks for any and all help.


Brian


"Edward E. Martins Jr. via DotNetMonster.com" <[email protected]>
wrote in message
Brian.
Try this to control text box:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/mwdesign/html/mwconIntroductiontotheTextBoxControl.asp

with relation to undo, you can put in the string or string[n] the
boxtext,
and in one function you can read the string to each Ctrl+Z...

This code capture the key press, key down and key up...

public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler (Form1_KeyDown);
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
}

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
MessageBox.Show(Convert.ToString(e.KeyChar));
}//fim Form1_KeyPress

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyDown

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
MessageBox.Show(Convert.ToString(e.KeyCode));
}//fim Form1_KeyUp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top