How to show underscore (shortcut) without holding Alt?

送分小仙女□ 提交于 2019-12-12 16:09:44

问题


I've created a form with ContextMenuStrip. I set its shortcut using Text field in following way: "&File". However, when I open this context menu by right mouse button click, underscore is shown only when I simultaneously hold Alt button. Is there a way to show underscore on a mouse click without holding Alt button?


回答1:


You can modify the text rendering behaviour (HidePrefix) via a custom ToolStripSystemRenderer:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            contextMenuStrip1.Renderer = new AccessKeyMenuStripRenderer();
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(Cursor.Position);
        }
    }

    class AccessKeyMenuStripRenderer : ToolStripSystemRenderer 
    {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextFormat &= ~TextFormatFlags.HidePrefix;
            base.OnRenderItemText(e);
        }
    }
}


来源:https://stackoverflow.com/questions/29799880/how-to-show-underscore-shortcut-without-holding-alt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!