How to know if form.acceptbutton is assigned

B

Barney

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.
 
G

Guest

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;
}
}
}
 
S

Stoitcho Goutsev \(100\)

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)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top