Question

I'm building a GUI application with C# and gtk#. I've encountered an issue recently and was looking for the best solution to this problem:

I have a modal window that pops up for the user to enter a number. This window is a separate window accessed from my main window and it's set up like this:

    public class MainWindow()
    {
        public NumberEntry numEntry;

Whenever I need numerical input from the user, I call ShowAll() on the public Window property of NumberEntry like:

    numEntry.win.ShowAll();

And all of this works fine. Afterwards, to get the value they entered, I call:

    int entered = numEntry.valueEntered;

The issue is obviously that code continues executing immediately after the ShowAll() line is finished, and numEntry.valueEntered is always 0. What I'd like to do (and have been trying to do), is to suspend the main thread, and open up the number entry window in a second thread, and join back to the main thread when this is complete. Suspending the main thread seems to prevent GUI changes making the program freeze when I try to open the number entry window. I'd also like to avoid callback methods if at all possible, seeing as how this would get rather complicated after awhile. Any advice? Thanks!

Was it helpful?

Solution

Seems like when GTK window is closed all its child controls are cleared. So to get the result from the custom dialog window you may do the following (I am not gtk guru but its works for me):

1. Create a new dialog window with your controls (I used Xamarin studio). Add result properties, OK and Cancel handlers and override OnDeleteEvent method:

public partial class MyDialog : Gtk.Dialog
{

    public string Results {
        get;
        private set;
    }

    public MyDialog ()
    {
        this.Build ();
    }

    protected override bool OnDeleteEvent (Gdk.Event evnt)
    {
        Results = entry2.Text; // if user pressed on X button..
        return base.OnDeleteEvent (evnt);
    }

    protected void OnButtonOkClicked (object sender, EventArgs e)
    {
        Results = entry2.Text;
        Destroy ();
    }

    protected void OnButtonCancelClicked (object sender, EventArgs e)
    {
        Results = string.Empty;
        Destroy ();
    }
}

2. In your main window create a dialog object and attach to its Destroyed event your event handler:

protected void OnButtonClicked (object sender, EventArgs e)
{
    var dialog = new MyDialog ();
    dialog.Destroyed += HandleClose;
}

3. Get the results when dialog is closed:

void HandleClose (object sender, EventArgs e)
{
    var dialog = sender as MyDialog;
    var textResult = dialog.Results;
}

If you whant you also may specify a dialog result property and etс.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top