Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a control.

I place the control on a form.

How do I make a button on the control the default button, handled when [Enter] is pressed.

  • *Experts*
Posted

You have to use the form's AcceptButton property which specifies a control that respond to Enter. Then simply get the button from your usercontrol:

this.AcceptButton = userControl11.Button1;

[edit]Got beaten ;)[/edit]

Posted

I dont think it has to be public in order to be the default button. Public is only required if you want to use the button from outside the scope of the form it is used in.

 

If you want to set the default button in a subclass, it has to be protected, in most cases the button can just remain private though, I think. As long as the button and the 'acceptbutton' property are set within the class itself it doesnt matter what scope they have.

Nothing is as illusive as 'the last bug'.
Posted

There is no default acceptbutton for a control..and when you set the acceptbutton in a form for a control, you need access to that button, so it has to be public. Assuming that the control was from a different assembly.

 

So, I guess I should rephrase my original question...assuming a button on a control is private, is there anyway to make it respond to [enter] on a form.

Posted

If you have no access to the control (3rd party control) and therefore have no chance to pass the form instance to the control, there is another way:

 

Find out the index of the button on the control, you want to set to default. In the load event of your form insert following line

Me.AcceptButton = yourControl.Controls(index)

(replace yourControl by the name you gave the control in your form and index by the index you detected.)

 

This should do the job.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...