为保存在C#中的文件创建快捷键 [英] create shortcut key for files saving in C#

查看:182
本文介绍了为保存在C#中的文件创建快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要为保存在C#程序中的文件创建快捷键

例如:在c#中保存的文件是ctrl + s,就像我想为我的文件保存创建快捷方式.在keyDown事件中

解决方案

添加此代码. br/>

 私有  void  Form1_KeyDown(对象发​​件人,KeyEventArgs e)
       {
           //  ctrl + s 
           如果(e.KeyData == Keys.ControlKey& e.Modifiers == Keys.S)
           {
               // 保存文件.
           }
       } 




希望它能对您有所帮助.


假设您有一个带有File命令的菜单,那么您应该会发现它已经起作用了(如果您使用了插入标准项目"操作).如果不是,则要将快捷键设置为菜单项,将其突出显示,然后在快捷键"下的属性"窗格中查看

您只能为菜单项输入CTRL +组合键-但是,如果您有一个按钮,则在按钮文本的字母前加上一个&号将把该键激活为ALT +组合键:保存并全部"将允许ALT + A充当按键.

如果您没有与命令关联的控件或菜单​​项,那么您将不得不处理KeyPress事件并自行检测该键.不过请注意,您需要为所有可以引起用户关注的控件处理此事件-否则它将不能一直工作,这将非常烦人.将它添加为Form Load事件中的处理程序可能是最简单的方法,前提是您可以使其递归地捕获面板内的控件等.


hi
i need to create shortcut keys for files saving in my c# program

for ex: files saving in c# is ctrl+s, like that i want create shortcut for my file saving also.

解决方案

on keyDown event add this code..

private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           //for ctrl + s
           if (e.KeyData == Keys.ControlKey && e.Modifiers == Keys.S)
           {
               //save your file.
           }
       }




hope it helps..


Assuming you have a menu with File commands, you should find this already works (if you used the "insert standard items" action). If not, then to set a shortcut key to a menu item, highlight it, and look in the Properties pane under ShortcutKeys

You can only enter CTRL+key combinations for menu items - but if you have a button, then prefixing a letter of the button text with an ampersand will activate that key as an ALT+key combination: "Save &All" would allow ALT+A to act as a key click.

If you don''t have controls, or menu items associated with your commands, then you will have to handle the KeyPress event and detect the key yourself. Do note though, that you need to handle this event for all controls that can have the user focus - or it won''t work all the time, which would be very annoying. Adding it as a handler in your Form Load event is probably the easiest way, provided you make it recursive to catch controls inside panels and so forth.


这篇关于为保存在C#中的文件创建快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆