< C++ .NET WinForm, Dialog Box & Controls 1 | Main | C++ .NET WinForm, Dialog Box & Controls 3 >


 

 

Windows Form, Dialog Boxes and More Controls 2

 

 

 

  1. The DialogResult Property

  2. Using Data with Dialog Boxes

 

 

The DialogResult Property

 

It’s common for dialog boxes to contain a set of buttons, such as Yes, No, and Cancel, or Abort, Retry, and Ignore - that the user can use to pass information back to the application. The DialogResult property is used to pass a value back to the calling code that shows which button was clicked to dismiss the dialog box. The property will take one of the values from the DialogResult enumeration, whose values are shown in the following table.

 

DialogResut property - setting the value

 

The following Table list down the DialogResult values and their descriptions. Windows users should be familiar with those values.

 

Member

Description

Abort

Represents the return value Abort and is usually set by a button labeled Abort.

Cancel

Represents the return value Cancel and is usually set by a button labeled Cancel.

Ignore

Represents the return value Ignore and is usually set by a button labeled Ignore.

No

Represents the return value No and is usually set by a button labeled No.

None

Nothing is returned from the dialog box, so the modal dialog box continues running.

OK

Represents the return value OK and is usually set by a button labeled OK.

Retry

Represents the return value Retry and is usually set by a button labeled Retry.

Yes

Represents the return value Yes and is usually set by a button labeled Yes.

 

Table 1

 

You might be slightly puzzled by the description of the None entry and we need to explain how DialogResult works. A user will typically dismiss a dialog box by clicking a button, and you’ve already seen how the handler for the OK button was used to close the About box in the previous exercise. If you want to pass back a return value from a modal dialog box, assign one of the values from the table to the form’s DialogResult property. However, doing so has the effect of assigning the value and immediately closing the form. So you could replace the OK button handler in the previous exercise with the following code (provided that the DialogResult property to None):

private:System::Void OKButton_Click(System::Object ^ sender, System::EventArgs ^ e)

{

    // Send back 'OK' and close the form

    DialogResult = System::Windows::Forms::DialogResult::OK;

}

Or just leave it empty after setting the DialogResult property value to OK. The caller can check the value returned from ShowDialog to find out what the dialog box returned. In fact, it can be even easier to close the form and send a value back to the caller. Button controls have a DialogResult property. If this property is set to a value other than None, clicking the button closes the parent form and sends the appropriate result back.

 

11. Select the button, open the Properties editor and set the DialogResult property to OK (or other than None). Then, display the events, and delete the OKButton_Click entry. Note that this doesn’t delete the handler function itself, but simply unlinks it from the button so we need to delete the event handler function manually later.

 

Setting the DialogResult property value to OK

 

 

Deleting the OKButton_Click event handler manually

 

12.     Then you can tidy up the code by manually deleting the OKButton_Click function from Form1.h as shown below.

 

Deleting the event handler function manually

 

13. Build and run your application. When you select the About menu item and click the OK button of the About box, the About box will be dismissed as usual without adding any code.

 

Using Data with Dialog Boxes

 

It’s common to use dialog boxes to obtain information from the user. You often have to load data into the dialog box before displaying it and then extract the data that the user entered, usually when the user clicks OK. The following exercise is an example of a dialog box that displays information, lets the user change it, and then reads the changes when it is dismissed.

 

14.     Open the CppForm project if it isn’t already open. Right-click the project name in Solution Explorer, and choose Add and then Add New Item from the context menu. Make sure that the Windows Form entry is selected in the Templates pane under the UI Categories, and add a new form named MyDialog.

 

Adding new Item from the context menu to the project

 

Adding new Windows Form to the existing project

 

15. Open the code window for the main form, Form1.h, and add an #include "MyDialog.h" line so that the new form can be referenced from the main form. Keep in mind that if you delete the form and recreate it, the filename will be incremented. So if you delete the form and recreate it the filename should be MyDialog1.h and the number will be incremented if you delete and recreate it again.

 

Adding a header file of the new dialog box object to the mainWinForm file

 

16. The dialog box is going to contain controls to gather a user’s personal details, which in this case will be name, phone number, and department. Each of these items will need a control and a label, and you’ll also need OK and Cancel buttons to dismiss the dialog box. Add the eight controls to the MyDialog form, arranging them to look like the following figure. We have three Labels, two TextBox, one ComboBox and two Button.

 

Dialog box with various controls in action

 

17. Put the Text properties for the labels as shown, Name:, Phone: and Department:. The name and phone fields are represented by TextBox controls Named nameBox and phoneBox respectively and the department is represented by a ComboBox control Named deptCombo. If needed, remove the default text from the Text property of the TextBox and ComboBox controls so that they are blank. It is removed by default.

 

Adding controls to the dialog box

 

18. Use the Properties editor to set up the form and the controls. Set the form to be 280, 200 pixels in Size and rearrange all the controls. Use a Fixed3D FormBorderStyle and the Text is Personal Details. Set the TextAlign for the labels to MiddleRight.

 

Setting the controls properties values

 

19. The combo box will hold a list of departments, and you can add these strings using the Properties editor. Select the ComboBox, find the Items property in the Properties editor, and click the ellipses (...) button to the right of the property. The String Collection Editor, which you can use to specify the strings that will be displayed in the combo box will open. Add the strings IT, Admin, R&D, Catering, and Manufacturing, one per line, and close the editor when you’re done.

 

Adding string to the ComboBox using String Collection Editor

 

20. Give the OK and Cancel buttons the Names OKButton and CancelButton, respectively, and set their DialogResult properties to OK and Cancel respectively as shown below.

 

Setting the OK and Cancel buttons properties

 

Because each button has its DialogResult member set, when they’re clicked, they’ll both dismiss the dialog box and return a DialogResult to the calling code. Here we do not add any handler for both buttons.

 

21. Now set the AcceptButton and CancelButton properties of the form. If set, the AcceptButton holds a reference to the button that is the default button on the form. Set the AcceptButton property to OKButton. Pressing the Enter key has the same effect as clicking the default button, and since we’ve set the DialogResult for OKButton to OK, pressing Enter will dismiss the dialog box and send DialogResult::OK back to the caller. Then, set the CancelButton property to CancelButton The form’s CancelButton property holds a reference to the button that maps onto the Esc key, thus giving the user a way to dismiss the dialog box from the keyboard.

 

Setting the AcceptButton property to OKButton value

 

 

 

 

 

 

Setting the CancelButton property to CancelButton value

 

 

AcceptButton and CancelButton values already set

 

22. To let the calling code get and set the values in the controls, add properties to the class under the public, like the following.  Select the form, right click and select View Code, then add the code manually.

 

Viewing source code through the View Code Context Menu

 

                // Properties for accessing control data

      property String^ Name

      {

            String^ get() { return nameBox->Text; }

            void set(String^ n) { nameBox->Text = n; }

      }

 

      property String^ Phone

      {

            String^ get() { return phoneBox ->Text; }

            void set(String^ p) { phoneBox ->Text = p; }

      }

 

      property int Dept

      {

            int get() { return deptCombo->SelectedIndex; }

            void set(int d) { deptCombo->SelectedIndex = d; }

      }

 

Adding properties source codes manually

 

There’s a pair of get and set methods for each field in the dialog box, and these will let the user of the MyDialog class manipulate the data without giving them complete access to the TextBox and ComboBox objects.

 

23. Add the code to display the dialog box. Display the main form (Form1.h), add a new menu item to the File menu, and give it the text Display Dialog.... Remember that the ellipses (...) on a menu item name means that the item is going to display a dialog box. In the menu editor, select the Exit menu item, right click and select Insert. Select MenuItem, so that it is between the About and Exit items. Go to the Properties page and give the menu item the Name dialogMenuItem.

 

  Adding new menu item using Context menu

 

Display Dialog... new menu item added

 

24.     Double-click the new menu item to create a handler for it, and edit the code as shown below for the handler so that it displays the dialog box.

 

private: System::Void toolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e)

{

   // Create the dialog

   MyDialog^ box1 = gcnew MyDialog();

   // Fill in the initial data

   box1->Name = L"Jane Doe";

   box1->Phone = L"123-4567";

   box1->Dept = 1;

   // Show the dialog

   if (box1->ShowDialog() == System::Windows::Forms::DialogResult::OK)

   {

         // If it came back with OK, display the name

         MessageBox::Show(box1->Name, L"Name was...");

   }

}

 

Adding codes for the menu item click event manually

 

After the dialog box object has been created, you use the properties to put the initial data into the controls. The dialog box is then shown to the user using ShowDialog, and when it returns, you can check the return value and see whether it was DialogResult::OK or DialogResult::Cancel. If it was OK (We already set the DialogResult property to OK for the OK button for MyDialog form), you can then use the Name property to extract the final data from the dialog box object.

 

25.     Build and run the code, and when you select the Display Dialog... menu item, you should see the dialog box display with its initial data shown below.

 

New dialog box with simple data invoke from menu item in action

 

26.     If you click the OK button, the Name was… message box will be displayed and if you click the Cancel button, the form will dismiss and return to the main form.

 

Message box displayed from the Dialog Box button click event

 

27.     Try change the default name and click the OK button.

 

Changing data in the text box control of the dialog box

 

Message box as a result of dimissing the dialog box

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

 


 

< C++ .NET WinForm, Dialog Box & Controls 1 | Main | C++ .NET WinForm, Dialog Box & Controls 3 >