Function Keys Saving

W

Wingot

Hey,



I am using Visual Studio 2008 Beta 2 for some application development in
C#, but I presume that the following question applies equally well to
any environment.



I have created some data entry forms for customer details, transactions,
etc. What I would like to do is make it so that the buttons across the
top right of the BindingNavigator (New, Delete, Save, respectively) were
mapped to the function keys F2-F4. I have tried searching on the web for
how to use function keys in C# (or Visual C#), but couldn't find much
that made any sense.



Another flow on from this problem is that when I take a look at the
source code, the method that saves the details is expecting various
arguments from the click event I believe, but if I call this method from
a function key or another button on the form, these arguments would not
be available. What would be the best way to call the saving method
(which has not been changed from the default) from within another
method. I would prefer not to replicate the code within the function so
as to avoid redundancy and potential later errors. The code for the save
method looks like this (bear in mind that this case uses the
NorthwindDataSet, not my real dataset, as this is a replication of the
issue due to NDA problems):



private void productsBindingNavigatorSaveItem_Click(object
sender, EventArgs e)

{

this.Validate();

this.productsBindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.northwindDataSet);



}



Regards,



Adrian Pavone

Applications & Technical Engineer

CVW Group
 
N

Nicholas Paldino [.NET/C# MVP]

Adrian,

Regarding the mapping, you should be able to handle the KeyUp event and look for the appropriate value in the Keys enumeration (which will be passed to you in the KeyEventArgs instance passed to you (through the KeyCode, KeyValue, or KeyData property).

As for calling the click event handler, the sender and e arguments are not used by your code, so I would separate them to another method which does not take any parameters and then call that method from the click event handler, as well as your other event handlers.

If you need some sort of parameters passed to the method, then you expose only what you need.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hey,



I am using Visual Studio 2008 Beta 2 for some application development in C#, but I presume that the following question applies equally well to any environment.



I have created some data entry forms for customer details, transactions, etc. What I would like to do is make it so that the buttons across the top right of the BindingNavigator (New, Delete, Save, respectively) were mapped to the function keys F2-F4. I have tried searching on the web for how to use function keys in C# (or Visual C#), but couldn’t find much that made any sense.



Another flow on from this problem is that when I take a look at the source code, the method that saves the details is expecting various arguments from the click event I believe, but if I call this method from a function key or another button on the form, these arguments would not be available. What would be the best way to call the saving method (which has not been changed from the default) from within another method. I would prefer not to replicate the code within the function so as to avoid redundancy and potential later errors. The code for the save method looks like this (bear in mind that this case uses the NorthwindDataSet, not my real dataset, as this is a replication of the issue due to NDA problems):



private void productsBindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

this.Validate();

this.productsBindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.northwindDataSet);



}



Regards,



Adrian Pavone

Applications & Technical Engineer

CVW Group
 
W

Wingot

Thanks for the point about moving the functionality to a separate
method, I remember doing that in my basic C# training, and just forgot
it now.



Any chance that you could point me to a resource (or tutorial) on how to
use KeyUp events (and presumably KeyDown and KeyPress as well)? I
presume it is just a matter of setting up an EventHandler for keyboard
input or similar?



From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Tuesday, 20 November 2007 11:19 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Function Keys Saving
Subject: Re: Function Keys Saving



Adrian,



Regarding the mapping, you should be able to handle the KeyUp event
and look for the appropriate value in the Keys enumeration (which will
be passed to you in the KeyEventArgs instance passed to you (through the
KeyCode, KeyValue, or KeyData property).



As for calling the click event handler, the sender and e arguments
are not used by your code, so I would separate them to another method
which does not take any parameters and then call that method from the
click event handler, as well as your other event handlers.



If you need some sort of parameters passed to the method, then you
expose only what you need.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hey,



I am using Visual Studio 2008 Beta 2 for some application development in
C#, but I presume that the following question applies equally well to
any environment.



I have created some data entry forms for customer details, transactions,
etc. What I would like to do is make it so that the buttons across the
top right of the BindingNavigator (New, Delete, Save, respectively) were
mapped to the function keys F2-F4. I have tried searching on the web for
how to use function keys in C# (or Visual C#), but couldn't find much
that made any sense.



Another flow on from this problem is that when I take a look at the
source code, the method that saves the details is expecting various
arguments from the click event I believe, but if I call this method from
a function key or another button on the form, these arguments would not
be available. What would be the best way to call the saving method
(which has not been changed from the default) from within another
method. I would prefer not to replicate the code within the function so
as to avoid redundancy and potential later errors. The code for the save
method looks like this (bear in mind that this case uses the
NorthwindDataSet, not my real dataset, as this is a replication of the
issue due to NDA problems):



private void productsBindingNavigatorSaveItem_Click(object
sender, EventArgs e)

{

this.Validate();

this.productsBindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.northwindDataSet);



}



Regards,



Adrian Pavone

Applications & Technical Engineer

CVW Group
 
N

Nicholas Paldino [.NET/C# MVP]

Wingot,

Have you looked at the documentation for those events? There should be some examples there.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Thanks for the point about moving the functionality to a separate method, I remember doing that in my basic C# training, and just forgot it now.



Any chance that you could point me to a resource (or tutorial) on how to use KeyUp events (and presumably KeyDown and KeyPress as well)? I presume it is just a matter of setting up an EventHandler for keyboard input or similar?



From: Nicholas Paldino [.NET/C# MVP] [mailto:[email protected]]
Posted At: Tuesday, 20 November 2007 11:19 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Function Keys Saving
Subject: Re: Function Keys Saving



Adrian,



Regarding the mapping, you should be able to handle the KeyUp event and look for the appropriate value in the Keys enumeration (which will be passed to you in the KeyEventArgs instance passed to you (through the KeyCode, KeyValue, or KeyData property).



As for calling the click event handler, the sender and e arguments are not used by your code, so I would separate them to another method which does not take any parameters and then call that method from the click event handler, as well as your other event handlers.



If you need some sort of parameters passed to the method, then you expose only what you need.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hey,



I am using Visual Studio 2008 Beta 2 for some application development in C#, but I presume that the following question applies equally well to any environment.



I have created some data entry forms for customer details, transactions, etc. What I would like to do is make it so that the buttons across the top right of the BindingNavigator (New, Delete, Save, respectively) were mapped to the function keys F2-F4. I have tried searching on the web for how to use function keys in C# (or Visual C#), but couldn’t find much that made any sense.



Another flow on from this problem is that when I take a look at the source code, the method that saves the details is expecting various arguments from the click event I believe, but if I call this method from a function key or another button on the form, these arguments would not be available. What would be the best way to call the saving method (which has not been changed from the default) from within another method. I would prefer not to replicate the code within the function so as to avoid redundancy and potential later errors. The code for the save method looks like this (bear in mind that this case uses the NorthwindDataSet, not my real dataset, as this is a replication of the issue due to NDA problems):



private void productsBindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

this.Validate();

this.productsBindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.northwindDataSet);



}



Regards,



Adrian Pavone

Applications & Technical Engineer

CVW Group
 
W

Wingot

Ah, yes, I've just taken a look.



I've gotten so used to the Microsoft based help being useless (for
Office XP or before, for Windows XP or before), never being able to find
what I need, that I keep forgetting how good the Visual Studio help
system is J.



Thank you Nick.



Regards,

Wingot



From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Tuesday, 20 November 2007 12:00 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Function Keys Saving
Subject: Re: Function Keys Saving



Wingot,



Have you looked at the documentation for those events? There should
be some examples there.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks for the point about moving the functionality to a separate
method, I remember doing that in my basic C# training, and just forgot
it now.



Any chance that you could point me to a resource (or tutorial) on how to
use KeyUp events (and presumably KeyDown and KeyPress as well)? I
presume it is just a matter of setting up an EventHandler for keyboard
input or similar?



From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Tuesday, 20 November 2007 11:19 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Function Keys Saving
Subject: Re: Function Keys Saving



Adrian,



Regarding the mapping, you should be able to handle the KeyUp event
and look for the appropriate value in the Keys enumeration (which will
be passed to you in the KeyEventArgs instance passed to you (through the
KeyCode, KeyValue, or KeyData property).



As for calling the click event handler, the sender and e arguments
are not used by your code, so I would separate them to another method
which does not take any parameters and then call that method from the
click event handler, as well as your other event handlers.



If you need some sort of parameters passed to the method, then you
expose only what you need.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hey,



I am using Visual Studio 2008 Beta 2 for some application development in
C#, but I presume that the following question applies equally well to
any environment.



I have created some data entry forms for customer details, transactions,
etc. What I would like to do is make it so that the buttons across the
top right of the BindingNavigator (New, Delete, Save, respectively) were
mapped to the function keys F2-F4. I have tried searching on the web for
how to use function keys in C# (or Visual C#), but couldn't find much
that made any sense.



Another flow on from this problem is that when I take a look at the
source code, the method that saves the details is expecting various
arguments from the click event I believe, but if I call this method from
a function key or another button on the form, these arguments would not
be available. What would be the best way to call the saving method
(which has not been changed from the default) from within another
method. I would prefer not to replicate the code within the function so
as to avoid redundancy and potential later errors. The code for the save
method looks like this (bear in mind that this case uses the
NorthwindDataSet, not my real dataset, as this is a replication of the
issue due to NDA problems):



private void productsBindingNavigatorSaveItem_Click(object
sender, EventArgs e)

{

this.Validate();

this.productsBindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.northwindDataSet);



}



Regards,



Adrian Pavone

Applications & Technical Engineer

CVW Group
 
J

Jeffrey Tan[MSFT]

Hi Wingot,

Thanks for your feedback.

There is one problem regarding KeyDown/KeyUp events. Because if the
keyboard focus is in TextBox, the F2 key press will not be sent to the
BindingNavigator control. So if there are many controls on the form, your
keyboard focus may possible in any of these controls, which means we have
to handle KeyDown/KeyUp events for all controls, which is not a good
practice.

To resolve this problem, you may set Form.KeyPreview property to true and
then only handle Form.KeyDown/KeyUp event. This property will help the
parent form to monitor all the keyboard events in it. Then, you may handle
the event something like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
MessageBox.Show("F2");
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

jehugaleahsa

Hi Wingot,

Thanks for your feedback.

There is one problem regarding KeyDown/KeyUp events. Because if the
keyboard focus is in TextBox, the F2 key press will not be sent to the
BindingNavigator control. So if there are many controls on the form, your
keyboard focus may possible in any of these controls, which means we have
to handle KeyDown/KeyUp events for all controls, which is not a good
practice.

To resolve this problem, you may set Form.KeyPreview property to true and
then only handle Form.KeyDown/KeyUp event. This property will help the
parent form to monitor all the keyboard events in it. Then, you may handle
the event something like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
MessageBox.Show("F2");
}

}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer tohttp://msdn.microsoft.com/subscriptions/managednewsgroups/default.asp...
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) athttp://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

That is very good to know. Woo hoo!
 
W

Wingot

Hey Jeffrey,

Thanks, I was trying to figure out how to do this very thing last night.
I could get it to work if I made the textboxes have the KeyUp event, but
not for just the form.

And yes, I agree that this sort of setting should not be applied to all
the controls on the form, which is why I spent about an hour trying to
figure out how to do it. If only I'd seen your email before leaving work
:)

Thanks,
Wingot

-----Original Message-----
From: "Jeffrey Tan[MSFT]" [mailto:[email protected]]
Posted At: Tuesday, 20 November 2007 3:39 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Function Keys Saving
Subject: Re: Function Keys Saving

Hi Wingot,

Thanks for your feedback.

There is one problem regarding KeyDown/KeyUp events. Because if the
keyboard focus is in TextBox, the F2 key press will not be sent to the
BindingNavigator control. So if there are many controls on the form,
your
keyboard focus may possible in any of these controls, which means we
have
to handle KeyDown/KeyUp events for all controls, which is not a good
practice.

To resolve this problem, you may set Form.KeyPreview property to true
and
then only handle Form.KeyDown/KeyUp event. This property will help the
parent form to monitor all the keyboard events in it. Then, you may
handle
the event something like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
MessageBox.Show("F2");
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#n
otif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Wingot,

Thanks for your feedback.

I hope you have got it working by using Form.KeyPreview property. If you
still need any help, please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wingot

Nope, It's working, thanks Chris.

I gave it a try last night when I got home, and had a bit of a play with
it to make it so I could actually use the New and Delete buttons as
well.

Form.KeyPreview worked like a charm.

As a matter of interest, from the name "KeyPreview", that would imply to
me that it checks it, but then passes it through to the control if it
doesn't find a valid action. Would that be correct?

Regards,
Wingot
 
J

Jeffrey Tan[MSFT]

Hi Wingot,

Thank you for the confirmation!

Yes, your understanding is correct. After the form's event handlers have
completed processing the keystroke, the keystroke is then assigned to the
control with focus.

If you only wanted to handle the keystrokes at the Form level without
passing to the focused controls, you may set the KeyPressEventArgs.Handled
property in your form's KeyPress event handler to true. The official MSDN
link below contains the details for this property:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.keyprevie
w.aspx

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 
J

Jeffrey Tan[MSFT]

Hi Wingot,

Have you reviewed my last reply to you? Does it answer your further
question? If you still have anything unclear, please feel free to tell me,
thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 

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

Top