1
votes

I have a Form which has a TextBox and a Button. I have set the Form's AcceptButton property to my Button, and set the TextBox's AcceptsReturn property to false:

class Window : Form
{
    private TextBox textBox1;
    private Button btn;
    public Window()
    {
        this.Size = new Size(200, 200);
        this.AcceptButton = this.btn;

        textBox1 = new TextBox();
        textBox1.Location = new Point(10, 10);
        textBox1.Width = 50;
        textBox1.AcceptsReturn = false;
        this.Controls.Add(textBox1);

        btn = new Button();
        btn.Text = "Test";
        btn.Location = new Point(textBox1.Right + 10, 10);
        btn.Click += btn_Click;
        this.Controls.Add(btn);
    }

    void btn_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Works");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.Run(new Window());
    }
}

However, when pressing the Enter key while the TextBox has focus, the AcceptButton of the Form is not activated. I worked around it using the KeyDown event of the TextBox like so:

textBox1.KeyDown += (s,e) => { if (e.KeyCode == Keys.Enter) btn.PerformClick(); } ;

And although it works I am curious as to why the earlier method using the AcceptButton property failed.

2
Possibly a typo, but your code sample shows that your TextBox has the AcceptsReturn set to true. - Kevin Burdett
I wonder this.AcceptButton = this.btn; <=> this.AcceptButton = null; The creation of the button is executed too late. - Jeroen Heier
Why not let visual studio write all this code for you? - Charlie
In a typical windows forms application all this control initialization code is written inside InitializeComponent function which gets called from constructor. All the code inside InitializeComponent function is auto-generated by visual studio as you continue to set properties of various controls on the form using properties window. Such mishaps will not happen if you follow that route. You possibly tried doing it all by yourself from scratch. When I looked at my auto-generated code it was all good by default. Hence I was wondering how did you end up creating this problem in first place :-) - RBT

2 Answers

2
votes

The problem is that you set the Form's AcceptButton to btn before you instantiate it. Move this.AcceptButton = this.btn; to any line AFTER btn = new Button();.. btn is pointing to a null reference up until new Button(). Once you instantiate btn you can use it to set AcceptButton.

2
votes

You just wrote one line in the wrong place.That's the answer:

    public Form1()
    {
        InitializeComponent();

        this.Size = new Size(200, 200);

        textBox1 = new TextBox();
        textBox1.Location = new Point(10, 10);
        textBox1.Width = 50;
        textBox1.AcceptsReturn = true;
        this.Controls.Add(textBox1);

        btn = new Button();
        btn.Text = "Test";
        btn.Location = new Point(textBox1.Right + 10, 10);
        btn.Click += btn_Click;
        this.Controls.Add(btn);

        this.AcceptButton = this.btn;
    }

I hope this helps you!