473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Customized Shortcuts

64
Hi there,

Im trying to implement a way to set the shortcuts defined by the user.

Im using NHibernate and my model class returns me fields in Keys type (Keys is an Enum that contains the name of the keys. example: Keys.F1)

I have implemented it in a static way and the code is the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. internal void set_shortcuts (KeyEventArgs e)
  3. {
  4.             switch (e.KeyCode)
  5.             {
  6.                 case Keys.F1: this.button_save.PerformClick(); break;
  7.                 case Keys.F2: this.button_cancel.PerformClick(); break;
  8.             }
  9. }   
  10.  
Now I have a form and the user redefined these shortcuts and I dont know how to do to set them. To be more specific:

Expand|Select|Wrap|Line Numbers
  1. internal void set_shortcuts (KeyEventArgs e)
  2. {
  3.             Keys save_shortcut = Shortcuts.load("save_button");
  4.             Keys cancel_shortcut = Shortcuts.load("cancel_button");
  5.  
  6.             switch (e.KeyCode)
  7.             {
  8.                 case save_shortcut: this.button_save.PerformClick(); break;
  9.                 case cancel_shortcut: this.button_cancel.PerformClick(); break;
  10.             }
  11. }   
  12.  
  13.  
I know that the SWITCH statment only accepts constants but i didnt figure out a way to do this.

I hope you can help me

Fernando Mello
Mar 19 '10 #1
3 1915
tlhintoq
3,525 Expert 2GB
I know that the SWITCH statment only accepts constants but i didnt figure out a way to do this.
Expand|Select|Wrap|Line Numbers
  1. Keys Temp = e.Keycode; // Now you have a constant you can Switch
  2. switch (Temp)
  3. {
  4.    // blah blah blah
  5. }
Expand|Select|Wrap|Line Numbers
  1. case Keys.F1: this.button_save.PerformClick(); break;
Oh please don't get in the habit of putting your methods in the click handlers then calling them. As your programs grow this becomes really ugly and hard to manage.

Expand|Select|Wrap|Line Numbers
  1. this.button_save_Click(object sender, eventargs e)
  2. {
  3.    DoSave();  // That's it. Have the save button call the save method
  4. }
  5.  
  6. internal virtual void DoSave()
  7. {
  8.    // Do your save function
  9.    // Now that this can be virtual, your inherited classes can override this
  10. }
  11.  
  12. void set_shortcuts(KeyEventArgs e)
  13. {
  14.    Keys Temp = e.Keycode
  15.    switch (Temp)
  16.    {
  17.     case Keys.F1:
  18.            DoSave();
  19.            break;
  20.     case Keys.F2:
  21.            DoCancel();
  22.            break;
  23.    }
  24. }
  25.  
  26. void MoreComplexProcess()
  27. {
  28.     DoValidation();
  29.     WarnUser();
  30.     DoSave();
  31.     UpdateGUI();
  32.     AddLogEntry("Complex operation complete");
  33. }
  34.  
  35. void TheUglyButtonClickWay()
  36.     this.button_validate.PerformClick();
  37.     this.button_save.PerformClick();
  38.     // etc.  See where this is going?
  39.     // If you rename a button, you break your calls
  40.     // Other classes would have to have access to the controls in order to call 
  41.     //   Instead of calling the public method of "Save()", "Load()", "ResetScore()"
  42. }
  43.  
  44.  
Mar 19 '10 #2
GaryTexmo
1,501 Expert 1GB
Darnit, the reply button doesn't work off your post, T. Blah, oh well.

I think the OP meant you have to use constants in the case part of the switch statement, which is correct. Per their second code block, they want to switch on a variable assigned to the key, which I'm pretty sure you can't do.

That said, you can change the way the problem is approached. I've done key bind setups like this for various projects before. What I used was a hash table mapping a Key to an Action enum. For example...

Expand|Select|Wrap|Line Numbers
  1. enum Action
  2. {
  3.   Unbound,
  4.   Save,
  5.   Cancel //,
  6.   // etc...
  7. }
Now you can create a hash table and associate a key with an action. For example...
Expand|Select|Wrap|Line Numbers
  1. myHashTable.Add(Keys.F1, Action.Save);
When you look up your pressed keys, you can use that key as the hashtable key and find the action associated with it. Then you simply switch on the action and do what you're already doing.

Hope that helps, good luck!
Mar 19 '10 #3
Sfreak
64
Thanks both of you! GaryTexmo got what I really would like to know... works perfectly now. I didnt think of using a hashtable. Now it works fine :)

tlhintoq thanks for the tips too! Im not calling the methods, i know it becomes a mess, I try to use in my sw several Design Patterns as MVC and others but my software is written in portuguese and that was a way to show you what i was trying to do. Thanks for the tip.

Fernando
Mar 19 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Luke Wright | last post by:
Hi, I have built a setup project to install an application I am developing. I would like to be able to prompt the user whether to install Shortcuts in the following locations: * Start Menu *...
0
by: Blake | last post by:
Hello, Does anyone know if there is any way to add a condition to your setup project so that the user can choose if they want a desktop shortcut or a program menu by selecting a checkbox. I...
0
by: DotNetJunkies User | last post by:
I am using the BrowseFolderDialog control in a Winform app to get a user selected path. Under Network Places, there are Folder Shortcuts to network resources. If a user selects one of these Folder...
4
by: blabore | last post by:
I have a MDI windows forms application that uses a toolbar on the parent form. I've also setup a context menu on this form to be used as the dropdown menu for one of the toolbar buttons. However,...
3
by: Marius Rus | last post by:
I have an application write in c# and i want to offer to the user to create himself shortcuts with icons for the main menu items. I will very much appreciate if will receive an helping hand. Thank...
0
by: GeorgeF | last post by:
How can I conditionally deploy shortcuts to my application? I have created a custom installer dialog box to give my users the option of creating desktop/start menu shortcuts, but the shortcuts do...
1
by: noleander | last post by:
Hello. Ive got a VC++ program. Inside the program Ive got some code that has a full file path: C:\aa\bb\cc\dd\file.txt One of the folders in that path (cc) is a shortcut pointing to some...
5
by: PAzevedo | last post by:
With ToolStripMenuItem objects i created a 'Tools' menu and from that menu droped an Item to which i assigned a ShortcutKey of 'Keys.Control | Keys.E', and the shortcut doesn't work. This...
331
by: Xah Lee | last post by:
http://xahlee.org/emacs/modernization.html ] The Modernization of Emacs ---------------------------------------- THE PROBLEM Emacs is a great editor. It is perhaps the most powerful and...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.