Diesel Posted May 16, 2005 Posted May 16, 2005 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. Quote
techmanbd Posted May 16, 2005 Posted May 16, 2005 in the properties of the form, 'AcceptButton'. Set it to the button you want. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
*Experts* mutant Posted May 16, 2005 *Experts* Posted May 16, 2005 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] Quote
Diesel Posted May 16, 2005 Author Posted May 16, 2005 So I have to make the button public. Well, if it's the easiest way. Quote
Wile Posted May 17, 2005 Posted May 17, 2005 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. Quote Nothing is as illusive as 'the last bug'.
Diesel Posted May 17, 2005 Author Posted May 17, 2005 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. Quote
*Experts* mutant Posted May 18, 2005 *Experts* Posted May 18, 2005 Pass the form instance into your UserControl as set it from there. That would be one other way. Quote
APaule Posted May 19, 2005 Posted May 19, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.