[Gtk-sharp-list] Re: How to validate input on Gtk.Dialog?

Paul Wallimann pwallimann at hotmail.com
Fri Sep 9 12:46:45 EDT 2005


Hi Rafael,

Thanks for sharing your solution. Before I got to read your solution I have
found a way to accomplish the required functionality. What I do now is

Button b = new Button(Stock.Ok);
this.ActionArea.Add(b);
b.Clicked += OnButtonOKClicked;
b.Show(); //Required to make button visible. Why not automatically?

instead of 

this.AddButton(Stock.Ok, ResponseType.Ok);


In the handler OnButtonOKClicked I first do the input validation and if it
succeeds then I call

this.Respond(ResponseType.Ok);

which closes the dialog and returns to the caller.

Regards,
Paul



Rafael Teixeira wrote:

> Small clarification:
> 
> I use Glade and a wrapping class generator and windowWidget is of type
> Gtk.Dialog.
> 
> :)
> 
> 
> On 9/8/05, Rafael Teixeira <monoman at gmail.com> wrote:
>> I normally design a common form (window) and create a Run method somewhat
>> like
>> 
>> private string password;
>> 
>> public string Run()
>> {
>>     try {
>>         windowWidget.ShowAll();
>>         while (true) {
>>             ResponseType response = (ResponseType)windowWidget.Run();
>>             if (response == ResponseType.Ok && validate(password))
>>                 break;
>>             if (response == ResponseType.Cancel) {
>>                 password = null;
>>                 break;
>>             }
>>         }
>>     } catch (Exception ex) {
>>             password = null;
>>     }
>>     windowWidget.Hide();
>>     return password;
>> }
>> 
>> some event/signal is catched to update the 'password' variable as
>> needed, or some code like:
>> 
>> password = entryPassword.Text;
>> 
>> can be put before the first 'if'
>> 
>> :)
>> 
>> On 9/5/05, Paul Wallimann <pwallimann at hotmail.com> wrote:
>> > I am still rather new to GTK# and currently struggling with the
>> > following: I want to implement a (modal) Login Dialog that only returns
>> > to the calling method when the user either hits Cancel or enters the
>> > correct password and hits OK. When the user enters the wrong password
>> > and hits OK the dialog should remain open.
>> >
>> > I thought I could implement this by simply subclassing from Gtk.Dialog.
>> > Unfortunately, the dialog gets closed immediately after either of the 2
>> > buttons is hit and I have not found a way to execute the validation and
>> > keep the dialog open when the password is wrong. Following is my code
>> > (using gtk-sharp, Version=2.4.0.0):
>> >
>> > using System;
>> > using Gtk;
>> >
>> > namespace Testing
>> > {
>> >         public class LoginDialog : Dialog
>> >         {
>> >                 public static void Main (string[] args)
>> >                 {
>> >                         Application.Init();
>> >
>> >                         LoginDialog dlg = new LoginDialog();
>> >                         dlg.Modal = true;
>> >                         int response = dlg.Run();
>> >                         Console.WriteLine("Main: Response was {0}",
>> >                         (ResponseType)response); dlg.Destroy();
>> >                 }
>> >
>> >                 public LoginDialog()
>> >                         : base()
>> >                 {
>> >                         this.AddButton(Stock.Cancel,
>> >                         ResponseType.Cancel); this.AddButton(Stock.Ok,
>> >                         ResponseType.Ok); this.Response += new
>> >                         ResponseHandler(On_dialog_response);
>> >                 }
>> >                 private void On_dialog_response (object obj,
>> >                 ResponseArgs args)
>> >         {
>> >             Console.WriteLine ("On_dialog_response: args.ResponseId =
>> >             {0}",
>> > args.ResponseId);
>> >             if (args.ResponseId == ResponseType.Ok)
>> >                 args.RetVal = false; // keep dialog open
>> >         }
>> >                 protected override bool OnDeleteEvent(Gdk.Event e)
>> >                 {
>> >                         // This method is not called. Why?
>> >                         Console.WriteLine("OnDeleteEvent: called");
>> >                         return false;
>> >                 }
>> >         }
>> > }
>> >
>> >
>> > I would appreciate if someone could tell me how to do it correctly or
>> > point me to an appropriate resource on the internet. Thanks in advance.
>> >
>> > Regards,
>> > Paul Wallimann
>> >
>> > _______________________________________________
>> > Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
>> > http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>> >
>> 
>> 
>> --
>> Rafael "Monoman" Teixeira
>> ---------------------------------------
>> I'm trying to become a "Rosh Gadol" before my own eyes.
>> See http://www.joelonsoftware.com/items/2004/12/06.html for enlightment.
>> It hurts!
>> 
> 
> 




More information about the Gtk-sharp-list mailing list