Using C# for User Interface Support
❘
347
The test code begins by importing CLR and gaining access to the
Dialogs
namespace. This example
demonstrates one of the benefits of using a namespace, easy access to multiple classes. It’s a good
way to organize a library of forms to make them easy to access and to avoid naming conflicts.
The
TestMessages()
function contains the code to test the
Dialogs.Dialogs
class. This code
begins by creating a
Dialogs.Dialogs
instance,
MyDialog
. In this case, the application begins by
creating a simple message box and displaying it onscreen. This message box lacks a title and con-
tains only an OK button. When the user clicks OK, the program prints the dialog result to screen.
The second test is a little more complex. This time the code relies on the most complex form of the
ShowMessage()
method to display a dialog box that contains a
message, title, icon, and multiple buttons as shown in Figure 16-15.
Notice that the figure shows that the message box also has the
middle button selected by default. Pressing Enter will automatically
select this default option. Normally, message boxes select the first
button as the default. Depending on which button the user clicks,
the application will display a message with the appropriate dialog
result. You could also use this dialog result as part of an
if...else
statement to choose an appropriate course of action.
The
TestForm()
method begins by creating an instance of
Dialogs.
TestForm
,
MyForm
. The
dir()
function will show you that
MyForm
now has access to all of the func-
tionality normally associated with a Windows Forms class, but without importing any of the bulk
associated with the
System.Windows
.Forms
assembly. As with any Windows Form, you call
ShowDialog()
to display the form. However,
the result of displaying the form is going to be something that IronPython can’t use directly. The way
to overcome this problem is to call
ShowDialog().ToString()
. In this case, the output is a string that
describes which button the user has clicked.
This portion of the example shows how to process the form data locally. When the user clicks OK,
the dialog result is
‘OK‘
and the
if
statement succeeds. The code accesses the
MyForm.txtName.Text
and
MyForm.txtColor.Text
properties to determine what the user has typed. When the
if
state-
ment fails, the code displays a message telling you that the user clicked Cancel. Figure 16-16 shows
typical output from this example.
Do'stlaringiz bilan baham: |