Stephen
Stephen

Reputation: 1617

How do I specify the default button of a TextBox that is referred to by AcceptsReturn?

I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)

I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.

I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".

What am I missing?

Upvotes: 0

Views: 2187

Answers (4)

JasonG
JasonG

Reputation: 137

Looks like an old question, but I will provide my solution.

    private void ChangeDefaultButton()
    {
        if (this.TextBox.Focused)
        {
            this.AcceptButton = button;
        }
        else
        {
            this.AcceptButton = button1;
        }
    }

And then add this method to the Focus Events of the text boxes. Like...

    private void TextBox_Enter(object sender, EventArgs e)
    {
        ChangeDefaultButton();
    }

And

    private void TextBox_Leave(object sender, EventArgs e)
    {
        ChangeDefaultButton();
    }

Upvotes: 0

Pedram
Pedram

Reputation: 313

I know this is an Old Question, but for someone who might to to lazy or just a beginner , handler might look like too much work ( though it isn't really )

But there is an easier work around for this, you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.

I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.

Upvotes: 0

Botonomous
Botonomous

Reputation: 1764

The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.

Upvotes: 1

M.Babcock
M.Babcock

Reputation: 18965

A Form has a default button, but a specific control does not (out of the box anyway).

In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.

Upvotes: 2

Related Questions