C #: How to deselect and defocus a text field?

I am writing a simple Calc-based application in C # WinForms. My problem is that I have a text box to display the results that cannot be clicked / focused, because the buttons are attached to the keyboard. The text field should have a ContextMenuStrip, but only this action should be processed in the text field. Also, this application requirement is that it cannot have any custom controls. Something like the "Calc result" output window. Any tips?

+3
source share
6 answers

Create a custom text field:

public class TextBoxWithoutFocus : TextBox
{
    private const int WM_SETFOCUS = 0x7;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETFOCUS)
            return;

        base.WndProc(ref m);
    }
}

. ( , ). WM_CHAR (0x102), .

UPDATE (, ):

public class ButtonWithoutFocus : Button
{
    public ButtonWithoutFocus()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}
+3

, . , , ContextMenuStrip.

+2

Try both TextBox.Enabled = False;andTextBox1.ReadOnly = True;

+1
source

Does it TextBox.Enabled = falsefit your needs?

0
source

try it

TextBox1.ReadOnly = True;
0
source

to defocus just do the following:

TextBox.Select(0, 0);
0
source

All Articles