471,354 Members | 1,169 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 4009
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by RubiconXing | last post: by
3 posts views Thread by Hsg | last post: by
6 posts views Thread by bole2cant | last post: by
1 post views Thread by Jeff | last post: by
1 post views Thread by Al Biheiri | last post: by
2 posts views Thread by sravan_reddy001 | last post: by

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.