473,795 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.AcceptBut ton = button1;
with this row
form1.AcceptBut ton = 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.Locatio n = new Point (10, 10);

button2.Text = "button2";
button2.Locatio n = 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.HelpButto n = true;

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

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

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

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

// Set the start position of the form to the center of the screen.
form1.StartPosi tion = FormStartPositi on.CenterScreen ;

button1.Click +=new EventHandler(bu tton1_Click);
button2.Click +=new EventHandler(bu tton2_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.ShowDialo g();
}

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

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

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

//Tony



Aug 1 '06 #1
1 4781
Tony,
So I tried with changing this row
form1.AcceptBut ton = button1;
with this row
form1.AcceptBut ton = 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***@removethi s.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
2039
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
6023
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. Situation 1: (works fine) If the user enters invalid text in a text box, presses Enter in the text box, the validation displays a msgbox. They now correct the text, press Enter in the text box, it validates correctly this time and the
7
2648
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 my button service in the dialog form, I have: Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSave.Click
2
7198
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 user can reenter them. How to I prevent the form from closing? MTIA wardeaux
15
4058
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 CALLING FORM! This is very bad for me, because in my application, Form1 responds to an ENTER keypress by calling Form2. If the user closes Form2 via an ENTER, then Form1 just reopens it again, kind of trapping the user in Form2 (they can still close...
1
1787
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. It's AcceptButton is set to btnExport and the CancelButton is set to btnCancel. btnExport on FormFinish has no event code whatsoever (not even for click.) When the user either clicks the button or presses enter on the
0
1081
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 AcceptButton-Property to Button2, compile the application with the IDE. After the compilation the property is already set to Button2. Now i add a new UserControl to the project and drop it on the Form(AcceptButton = Button2). When i compile the project the property...
2
1658
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 the item they want to choose then they press enter to accept the form. The problem is when you go into the form butSearch is set to AcceptButton for the form, once you have selected the search criteria to populate the data grid then we want to...
1
2481
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.