473,406 Members | 2,710 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,406 software developers and data experts.

How to know if form.acceptbutton is assigned

Hi,
This is my first query in google groups. I expect to donīt do it
wrong, and excuse me for my english, i havenīt used it for years...

Iīm making a user component that inherits from a textbox. I would like
to use the acceptbutton property of its parent form when the "enter"
key is pressed. Something like that.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
this.ParentForm.AcceptButton.PerformClick();
break;
}
}
}

The problem is that if the acceptbutton isnīt assigned it throws an
exception. I can put it like that, but it spends to much time till the
exception is thrown.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
try
{
this.ParentForm.AcceptButton.PerformClick();
}
catch(Exception ex){};
break;
}
}
}
How can i know if the acceptbutton is assigned?
Thanks.

Feb 24 '06 #1
2 4084
Howdy,

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
Button button = this.ParentForm.AcceptButton;
if (button != null)
button.PerformClick();
break;
}
}
}

--
Milosz Skalecki
MCP, MCAD
"Barney" wrote:
Hi,
This is my first query in google groups. I expect to donÂīt do it
wrong, and excuse me for my english, i havenÂīt used it for years...

IÂīm making a user component that inherits from a textbox. I would like
to use the acceptbutton property of its parent form when the "enter"
key is pressed. Something like that.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
this.ParentForm.AcceptButton.PerformClick();
break;
}
}
}

The problem is that if the acceptbutton isnÂīt assigned it throws an
exception. I can put it like that, but it spends to much time till the
exception is thrown.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
try
{
this.ParentForm.AcceptButton.PerformClick();
}
catch(Exception ex){};
break;
}
}
}
How can i know if the acceptbutton is assigned?
Thanks.

Feb 24 '06 #2
Barney,

You can check form.AcceptButton != null, but the purpose of the accept
button is to be the default button when someone presses the Enter key in the
form. Your problem is that the text box swallows this Enter key and it
doesn't go to the AcceptButton.

If this is the only problem you have I'd suggest instead of writing that
code just to set textbox's AcceptsReturn property to *false*. This way when
the focus is in the textbox and Enter is pressed the default button will
fire the Click event. If the user wants to enter a newline in the text the
user needs to press Ctrl+Enter

--
HTH
Stoitcho Goutsev (100)
"Barney" <ba******@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,
This is my first query in google groups. I expect to donīt do it
wrong, and excuse me for my english, i havenīt used it for years...

Iīm making a user component that inherits from a textbox. I would like
to use the acceptbutton property of its parent form when the "enter"
key is pressed. Something like that.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
this.ParentForm.AcceptButton.PerformClick();
break;
}
}
}

The problem is that if the acceptbutton isnīt assigned it throws an
exception. I can put it like that, but it spends to much time till the
exception is thrown.

private void txtAutocomp_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
{
try
{
this.ParentForm.AcceptButton.PerformClick();
}
catch(Exception ex){};
break;
}
}
}
How can i know if the acceptbutton is assigned?
Thanks.
Feb 24 '06 #3

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

Similar topics

3
by: RubiconXing | last post by:
Hi All Beginner question - please be patient with me :-) I am new to c#. If one creates a child modal (and also non-modal) form what is the best way of returning the collected data back to the...
3
by: Hsg | last post by:
Hi all.. in C# , and specally in Forms ... i had creat a form , the user enter Inputs , and then press the Button ... is there a way that the user will have teh choice to press the Button on...
9
by: Ale K. | last post by:
What's the best way to make a form return a value.... i want to try to avoid using module variables for doing this... Thanks. Alex.
6
by: bole2cant | last post by:
The error I get is: Cannot access a disposed Object named "Form2". Object name "Form2". Here is the sequence which gives the error. Start program. Form1 comes up and has a button to choose...
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...
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....
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
2
by: sravan_reddy001 | last post by:
i had designed a win app(an address book) when the user wants to enter the contact he will type in all the details and "HE SHOULD CLICK ON THE ADDENTRY BUTTON PROVIDED" i want to simplify this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.