ICreate.Controls.Bars Send comments on this topic.
Walkthrough - Accept Cancel CommandButtons

Glossary Item Box

This walkthrough describes how to use CommandButtons as the AcceptButton and CancelButton of a modal Form. In this walkthrough you will create a Form used to input member information. The Form contains an Ok and Cancel CommandButton. The Ok button is the AcceptButton of the Form. The AcceptButton is clicked when the enter key is pressed. The Cancel button is the CancelButton of the Form. The CancelButton is clicked when the cancel key is pressed. The Form for this walkthrough is shown below.

  1. Create the form with the edit controls as shown above.
  2. Place two CommandButton controls on the form.
  3. Set the Name of the one CommandButton to buttonOk.
  4. Set the Name of the other CommandButton to buttonCancel.
  5. Select buttonOk and click its Smart tag. The Smart tag panel shown below is displayed.

  6. Set ButtonStyle = Modern.
  7. Set the Text = Ok.
  8. Set the Image = Ok.png.
  9. Set the BackColor = 255,224,192.
  10. Set the AutomaticHeight property to true. By default the AutomaticSize property is true and the CommandButton resizes itself according to the size of its text and image. Setting AutomaticHeight true instructs the CommandButton to only adjust its height according to the text and image. Note that AutomaticSize and AutomaticHeight properties cannot both be true.
  11. Set the Width = 200.
  12. In the property grid set the DialogResult to OK
  13. Select buttonCancel and click its Smart Tag. The Smart Tag panel is displayed.
  14. Set the ButtonStyle = Modern.
  15. Set the Text = Cancel.
  16. Set the Image = Cancel.png
  17. Set the BackColor = 255,192,192.
  18. Set the AutomaticHeight property to true and the Width to the same value as the buttonOk.
  19. Set the DialogResult = Cancel.
  20. Select the Form.
  21. Set the Form's AcceptButton property to the buttonOk.
  22. Set the Form's CancelButton property to the buttonCancel.
  23. Add the following code to display and process the Member information.
    Visual Basic

    Dim dialog As MemberForm = New MemberForm()

    If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

    MessageBox.Show("Store data")

    End If

    C#

    MemberForm dialog = new MemberForm();

    if (dialog.ShowDialog() == DialogResult.OK)

    {

    MessageBox.Show("Store data");

    }