By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

How to know if form.acceptbutton is assigned

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.