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