Problem with the AcceptButton property

T

Tony Johansson

Hello!

I'm reading a book which mentioned about this AcceptButton property for a
form which seems quite easy so I try to figure out how it works but I have
run into some small problems.

I have this small easy program below which show two buttons called button1
and button2.
In this program when the Enter key is clicked event handler for button1 is
called.
I want to change that I want insted the event handler for button2 to be
called when the
Enter key is clicked.
So I tried with changing this row
form1.AcceptButton = button1;
with this row
form1.AcceptButton = button2;
but event handler for button1 is still called when the Enter key is clicked
I can't understand why?

Can somebody explain why not the event handler for button2 is called when I
haved said
that button2 should be used for AcceptButton in the form.


//Start code
//*************
using System;
using System.Windows.Forms;
using System.Drawing;

public class frmApp : Form
{
public void CreateMyForm()
{
Form form1 = new Form();

Button button1 = new Button ();
Button button2 = new Button ();

button1.Text = "button1";
button1.Location = new Point (10, 10);

button2.Text = "button2";
button2.Location = new Point (10,50);

// Set the caption bar text of the form.
form1.Text = "My Dialog Box";

// Display a help button on the form.
form1.HelpButton = true;

// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;

// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;

// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;

// Set the accept button of the form to button2.
form1.AcceptButton = button2;

// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;

button1.Click +=new EventHandler(button1_Click);
button2.Click +=new EventHandler(button2_Click);

// Add button1 to the form.
form1.Controls.Add(button1);

// Add button2 to the form.
form1.Controls.Add(button2);

// Display the form as a modal dialog box.
form1.ShowDialog();
}

[STAThread]
static void Main(string[] args)
{
frmApp myForm = new frmApp();
myForm.CreateMyForm();
}

private void button1_Click(object sender, EventArgs e)
{
int tal = 0;
tal=9;
}

private void button2_Click(object sender, EventArgs e)
{
int tal = 0;
tal=9;
}
}

//Tony
 
J

Jani Järvinen [MVP]

Tony,
So I tried with changing this row
form1.AcceptButton = button1;
with this row
form1.AcceptButton = button2;
but event handler for button1 is still called when the Enter key is
clicked

You are in fact able to set the AcceptButton property of a form at runtime
without problems, but here the problem is that you have only buttons on your
form.

Wwhen you move the focus to a button (say, by pressing the Tab key), the
Enter key will execute that button. But if you are focused on, say, a
textbox, then Enter key will execute the default button, i.e. that button
which was set with the AcceptButton property.

To make your code work properly, try adding a textbox to your dialog box.
That should solve the issue.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top