Event Handlers

  • Thread starter Thread starter Steve Behman
  • Start date Start date
S

Steve Behman

Look as I may, I cannot find a general discussion about the information
available to an Event Handler in Visual C++ 8.0 Express Edition.

In particular I am trying to manage the contents of the TextBox.

I have figured out a rather painful way of handling the "text changed" event
but, it takes saving the contents and cursor position from the last event of
this type in order to compare it to the "new" content and then altering the
current content appropriately.

I feel that there must be some way to use the PreviewKeyDown event to
examine the proposed change and, if it is unacceptable to beep the user and
reject the keystroke.

A specific solution to what I want to accomplish would be appreciated. Even
more so a pointer to a document that*fully* discusses the events that various
controls raise any appropriate responses to them.

I would hope that there is an analogy to the way one responds to messages at
the SDK level; wherein the programmer has complete control over further
action of the message (i.e. alter it and pass it on, snuff it, or take the
system defined default action).
..
 
Look as I may, I cannot find a general discussion about the information
available to an Event Handler in Visual C++ 8.0 Express Edition.

In particular I am trying to manage the contents of the TextBox.

I have figured out a rather painful way of handling the "text changed" event
but, it takes saving the contents and cursor position from the last eventof
this type in order to compare it to the "new" content and then altering the
current content appropriately.

I feel that there must be some way to use the PreviewKeyDown event to
examine the proposed change and, if it is unacceptable to beep the user and
reject the keystroke.

It sounds like you want to validate the contents of the textbox on the
fly, and block user from inputting the wrong keys. If so, you should
consider two things:

1. KeyPress event gets an argument of type KeyPressEventArgs, which
has a property called Handled. Here's what MSDN has to say about it:

"Set Handled to true to cancel the KeyPress event. This keeps the
control from processing the key press."

KeyDown and KeyUp have similar arrangements.

2. Data binding can do it for you if you put the binding into
"OnPropertyChanged" mode. Data source should throw exceptions to
indicate invalid input.
I would hope that there is an analogy to the way one responds to messagesat
the SDK level; wherein the programmer has complete control over further
action of the message (i.e. alter it and pass it on, snuff it, or take the
system defined default action).  

Not for any arbitrary event, since some of them are in fact raised by
the control itself once it finished processing the input (such as
TextChanged). It is up to the control to provide some facility to
cancel further processing of the event if such a thing is possible.
 
Pavel , thanks for the response.

This is the code I wrote:

private: System::Void Amount_KeyPress(System::Object^ sender,
System::Windows::Forms::KeyPressEventArgs^ e)
{
e -> Handled = true;
bool c;
wchar_t a=e ->KeyChar;
c= ( ( a >= L'0') && (a >= L'9') || (a == L'.') );

if (c == false) error -> Visible = false;
else
{
e ->KeyChar=0x0007;
error -> Visible = true;
}
};

And this is entirely unexpected result:

"error C2039: 'Handled' :
**is not a member of**
'System::Windows::Forms::PreviewKeyDownEventArgs' "

This leaves me with ttrse questions:

1) Why am I getting this error?
2) Must Handled be set to "true" before the function is exited?
3) What would the consequences of not setting Handled to "true"?
 
Pavel, I am very sorry about my previous post -- the problem actually
occurred in nearby code . That is the answer to question 1, however it
leads me to a fourth question;
How can I get the system to complete the "normal" processing of the
(possibly modified) keystroke?
 
Pavel, I am very sorry about my previous post -- the problem actually
occurred in  nearby code .   That is the answer to question 1, however it
leads me to a fourth question;
How can I get the system to complete the "normal"  processing of the  
(possibly modified) keystroke?

For KeyDown, you can allow it to process the event normally by setting
Handled and SuppressKeyPress properties of KeyEventArgs to false (or
not touching those properties in the first place, since their default
value is false). You cannot rewrite keycodes in KeyDown event handler,
though.

In KeyPress event handler, you can modify input characters simply by
changing the value of KeyPressEventArgs.KeyChar property. Again,
unless you set Handled to true, the default processing will take place
after your event handler returns, and the modified character value
will be used (e.g. inserted into the text in case of TextBox).
 
Pavel,

Using the keypressed event worked like a charm! I can now manage a regular
TextBox as I might have used a masked text box and as a bonus not have to put
up with the ugly placeholders. I just "swallow" the unwanted characters.

Thank you so very much for the help.
 
Back
Top