开发者

Is there a way to remove Alt-character shortcuts from controls at runtime?

I have a dialog with a number of Alt-Letter shortcuts on labels for textboxes/etc. This dialog can present data in either an editable or a read-only mode. I've received a request to hide the unde开发者_高级运维rlines for the shortcuts if the dialog is in read only mode. Other than editing the label text at runtime (ugh) is there any way to remove them?

If you don't know what I'm referring to by alt-Letter shortcuts see this question.


You could just iterate the controls and remove the ampersands. For example:

public partial class dlgSample : Form {
    public dlgSample(bool isReadOnly) {
        InitializeComponent();
        if (isReadOnly) ZapMnemonics(this.Controls);
    }

    private void ZapMnemonics(Control.ControlCollection ctls) {
        foreach (Control ctl in ctls) {
            if (ctl is Label || ctl is Button) ctl.Text = ctl.Text.Replace("&", "");
            ZapMnemonics(ctl.Controls);
        }
    }
}


If you change the value of the UseMnemonic property, then the ampersand shows up in the label, so I'm not sure how you're going to be able to remove the underlines without changing the label.


You could try setting the KeyPreview property of the form to true and then add a handler for the form's KeyDown event. If the keypress is one of the shortcut keys then set the Handled property of the KeyEventArgs parameter to true. Or you can set the .SuppressKeyPress property of the KeyEventArgs variable to true.

You can check the .Alt property of the KeyEventArgs variable to make sure the Alt key is pressed at the same time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜