3

I have a ToolstripLabel for which I want to set UseMnemonic to false. But it doesn't have this property. Is there a way to do this?

I tried casting it to label, but it didn't work.

Jerry
  • 4,258
  • 3
  • 31
  • 58

1 Answers1

6

You can't get to it. The basic flaw is that the Text of the label is rendered with a TextFormatFlags value that's missing the HidePrefix option. Getting that value changed is not possible, it is buried inside internal code.

A workaround is to use your own renderer and fix the problem by altering the text before it is rendered. Make that look like this:

    public Form1() {
        InitializeComponent();
        toolStrip1.Renderer = new MyRenderer();
    }

    private class MyRenderer : ToolStripProfessionalRenderer {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) {
            if (e.Item is ToolStripItem) e.Text = e.Text.Replace("&", "&&");
            base.OnRenderItemText(e);
        }
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This appears to alter the expected render size of the `ToolStripItemLabel` and then it causes clipping issues on neighboring items. – test Sep 06 '22 at 23:23