How to Make Default Button?

csharp United States
  • 9 years ago
    hellow friends,
             can enybody know how to make a default button so that when user enter some text in textbox and then pess Enter then button_click event should fires.
             if any body have idea then pls help me.
    Nitin
  • 9 years ago

    Each form has an AcceptButton property and a CancelButton property.  Whichever button you assign to the AcceptButton property will be "pressed" when the user hits the ENTER key and whichever you assign to the CancelButton property will be "pressed" when the user hits the ESC key.


    Note that when you assign a button to the CancelButton property, its DialogResult property is set to Cancel by default.  This means that if the ESC key is pressed or the Cancel button is clicked, the form's DialogResult property will be set to Cancel, it will be closed automatically and, if it was called using ShowDialog, that function will return Cancel.
    When you assign a button to the AcceptButton property, its DialogResult property is unchanged.  If you want the form to close automatically when it is clicked, you should set it's DialogResult property to an appropriate value, which would usually be OK.  If you want to do some processing before closing the form, you can still do this in the button's Click event handler.  If you may or may not want to close the form automatically, you should leave the button's DialogResult property set to None and only set the form's DialogResult property to something like OK if you want to close the form.  Note that whenever a form's DialogResult property is set, like when a button that has its own DialogResult property set is clicked, the current method is completed and the form is closed.  If it was called using ShowDialog, that function will return the value of the form's DialogResult property.

  • 9 years ago

    jmcilhinney,
               tanks for replying but i want to have default button in a usercontrol, but the usercontrol didnot have Accept Button property so what should i do to do that.
               i think u understood my problem, if posiible reply soon.
    Nitin.

  • 9 years ago

    If your UserControl exposes that button publicly, like through a property or the actual variable, you should be able to assign the button to the form's AcceptButton property.  If you want to create a default button for the control itself, I'd say you would have to use events to trap the ENTER key and then click the appropriate button.

  • 9 years ago

    Dear jmcilhinney,
           thanks a lot, i has to use multiple usercontrol in a single form using tab control so if i made all buttons public then i have problem when to use which control's button as default button. but i use the keyUP property of each texbox on each usercontrol and look for Enter Kye, if prssed then the button_click() fired from code,
           this might not look  good technic but it solves my problem hence thanks once again.if u have any other goog way to do that then let me know.
           thanking you,


    Nitin

  • 9 years ago

    You can assign any button to the form's AcceptButton property at any time, so why don't you just change the default button as each different TabPage is selected?

  • 8 years ago

    if your OK button is buttonOK
    then write below code in the initialisation of your dialogbox.


    this.AcceptButton = buttonOK;


    Or you may do this from GUI workspace, simple highlight the Dialogbox and accept button value
    to the AcceptButton property of the dialogbox.


    Similiarly you can assign cancel button as well by assigning button value to this.CancelButton property.


    Hope this helps.


    Regards,
    ManDorG

  • 8 years ago

    Quote:
    [1]Posted by mandorg on 22 Nov 2005 06:44 AM[/1]
    if your OK button is buttonOK
    then write below code in the initialisation of your dialogbox.


    this.AcceptButton = buttonOK;


    Or you may do this from GUI workspace, simple highlight the Dialogbox and accept button value
    to the AcceptButton property of the dialogbox.


    Similiarly you can assign cancel button as well by assigning button value to this.CancelButton property.


    Hope this helps.


    Regards,
    ManDorG


    Only 5 months late.

  • 7 years ago

    What about the scenario where you are using MasterPages?  Since the Form is part of the masterpage and not each page that is using the masterpage, you can't set the form.acceptbutton.  How would you make the Enter key the default for a submit button on each different page that is using the MasterPage.  The only thing I've found so far is to just Javascript, but....

    Jack

  • 2 years ago

    Each form has an AcceptButton property and a CancelButton property. Whichever button you assign to the AcceptButton property will be "pressed" when the user hits the ENTER key and whichever you assign to the CancelButton property will be "pressed" when the user hits the ESC key.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay