Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building Robust UIs in Mono with Gtk# : Page 5

Learn to use the native Gtk# GUI toolkit to build Mono-based desktop applications today.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Displaying Dialog Boxes
Now that the dialog class is complete, the last step is to flesh out the code that runs in the ToolsEnterName_Activated() method when users click the "Enter Name" menu item. The code needs to display the UserNameDialog and fetch the data within the Entry widgets. Consider this first implementation, which simply prints the values to the Console window if (and only if) the user clicks on the OK button:

void ToolsEnterName_Activated(object sender, EventArgs args) { // Run the dialog and grab the ResponseType. UserNameDialog dlg = new UserNameDialog(); ResponseType rsp = (ResponseType)dlg.Run(); // Figure out which button was clicked. if(rsp == ResponseType.Ok) { Console.WriteLine("Hello {0} {1}!," dlg.FirstName, dlg.LastName)); } // Destroy the dialog (just to keep things tidy). dlg.Destroy(); }

Authors Note: Technically speaking, you do not need to explicitly destroy dialog objects, as they will be garbage collected like any other .NET heap allocated type.

After allocating the UserNameDialog object, the preceding code displays the dialog by calling Run(). The Run() method displays a modal dialog; therefore users must dismiss the dialog by clicking on one of Button widgets (or the 'X' button on the dialog window). The return value from the Run() method will be one of the ResponseType values assigned to the buttons earlier. At this point you can check for the ResponseType.Ok value and format a friendly salutation.

To spruce up the current Activated event handler, the improved version below retrofits the code to make use of the Gtk.MessageDialog type (which represents a simple message box):

void ToolsEnterName_Activated(object sender, EventArgs args) { UserNameDialog dlg = new UserNameDialog(); ResponseType rsp = (ResponseType)dlg.Run(); if(rsp == ResponseType.Ok) { // Hide the UserNameDialog object from view. dlg.Hide();

Figure 9. The MessageDialog in Action: After extracting the first and last name values users enter into the UserNameDialog, this MessageDialog displays them.
// Create a MessageDialog to show the salutation. MessageDialog md = new MessageDialog (this, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Ok, string.Format("Hello {0} {1}!," dlg.FirstName, dlg.LastName)); // This time we don't care which button they click on. // Any button will simply destroy the MessageDialog. md.Run (); md.Destroy(); } // Now destroy the UserNameDialog. dlg.Destroy(); }
The call to Hide() is the more interesting statement of the method, as this keeps the dialog in memory but not visible on the screen. This is important, because you need to extract the first and last name values to format the salutation. Figure 9 shows one possible MessageDialog configuration.

That wraps up this initial look at the Gtk# programming API. Obviously, this introductory article could cover only the basics of working with Gtk#, but it should give you a good start for further exploration.

Andrew Troelsen is a .NET trainer and consultant for Intertech Training, a .NET and J2EE education and development company. He is the best-selling author of numerous books on .NET and COM (including the award winning C# and the .NET Platform) and speaks at numerous corporate and academic conferences around the country.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date