473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with the AcceptButton property

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



Aug 1 '06 #1
1 4730
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Aug 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jac | last post by:
Hey, How can I set the defaut accept button in an aspx-page. In some screens I see in acceptbutton but I see know difference between this button properties and the others. Thanks Jac
2
by: lostdreamz | last post by:
Ok, my situation is like this. I have a form with a few text boxes each with a 'validating' event, OK & Cancel buttons, and have the AcceptButton/CancelButton/DialogResult properties all set. ...
7
by: Frank Maxey | last post by:
I am fairly new to VB.Net and am having a curious problem. I have an entry dialog form called from a main form. The calling form needs to check the DialogResult field for an OK response. In...
2
by: Wardeaux | last post by:
All, I have login form used with ShowDialog(). I have the AcceptButton property set so that the "Enter" key is mapped. I also need to keep the dialog open when the UserID and PWD are invalid so...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
1
by: Jeff | last post by:
Hello all. I'm trying to figure out exactly what is going on here. I'm getting different results on different forms, and it's driving me nuts. For example, I have a form called FormFinish....
0
by: Karsten Sosna | last post by:
Hello NG, i have a problem with the AcceptButton in VB.Net and C#. I have an empty project with one form. On this form i take two buttons(Button1 and Button2). Now i set the the...
2
by: sonicm | last post by:
Hi, I have a search for where we want the user to enter the search criteria in at the top and press enter, then the results are shown in a datagrid which we set the focus to. Once they select...
1
by: Al Biheiri | last post by:
how do i create an AcceptButton in c# user presses enter on textbox = button1.click something like AcceptButton = button1
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.