在C#中打开GTK窗口,返回数据并关闭窗口,然后打开另一个窗口?

时间:2013-02-04 22:56:44

标签: c# gtk

我正在使用C#和GTK编写一个带有按钮的主窗口的应用程序。当按下其中一个按钮时,它会调用一个事件处理程序功能,该功能会打开一个新窗口,其中包含一个供用户填写的表单。到目前为止,所有工作都很好。

我遇到的问题是我需要让这个事件处理程序功能打开这个窗口,等待它被填写并关闭,然后打开第二个窗口,等待它被填写并关闭,然后处理所有这些捕获的数据。 我做了一个简单的例子来说明我的意思(我在下面的例子中理解,打开一个带有两个组合框的窗口更有意义,但那是因为这只是一个例子!)

using System;
using Gtk;

namespace WaitingTest
{
    public class MainClass
    {       
        static void Main (string[] args)
        {
            Application.Init ();
            MenuWindow myWindow = new MenuWindow();

            Application.Run();
        }
    }

    public class MenuWindow
    {
        static string favDrink = "Water";
        static string favCheese = "Chedder";
        static Label output = new Label();

        public MenuWindow()
        {
            Application.Init ();

            Window test = new Window ("Main Window");

            VBox vb = new VBox();
            Button getData = new Button("Get Data");
            Button quitApp = new Button("Quit");

            getData.Clicked += OnGetData;
            quitApp.Clicked += OnExit;

            vb.PackStart(getData, false, false, 1);
            vb.PackStart(quitApp, false, false, 1);
            vb.PackStart(output, false, false, 1);

            test.Add(vb);
            test.ShowAll();

            Application.Run();
        }

        static void OnGetData(object sender, EventArgs args)
        {
            // Open up Window to select drink
            WindowOne w1 = new WindowOne();

            // Open up Window to select cheese
            WindowTwo w2 = new WindowTwo();

            // Display results in label
            output.Text="Drink: "+favDrink+"\nCheese: "+favCheese;
        }

        public static void SetFavDrink(string d)
        {
            favDrink = d;
            Console.WriteLine(favDrink);
        }

        public static void SetFavCheese(string c)
        {
            favCheese = c;
            Console.WriteLine(favCheese);
        }

        static void OnExit(object sender, EventArgs args)
        {
            Application.Quit();
        }

        protected void OnDeleteEvent(object sender, DeleteEventArgs a)
        {
            Application.Quit ();
            a.RetVal = true;
        }   
    }

    class WindowOne
    {
        ComboBox drink = ComboBox.NewText();
        static string choice;

        public WindowOne ()
        {
            Window w1 = new Window ("Window One");

            VBox vb = new VBox();

            Button sendDrink = new Button("Send Drink");

            sendDrink.Clicked += OnSend;
            drink.Changed += new EventHandler(onDrinkChanged);

            drink.AppendText("Beer");
            drink.AppendText("Red Wine");
            drink.AppendText("White Wine");
            drink.AppendText("Whiskey");

            vb.PackStart(drink, false, false, 1);
            vb.PackStart(sendDrink, false, false, 1);

            w1.Add(vb);
            w1.ShowAll();
        }

        void OnSend(object sender, EventArgs args)
        {
            WaitingTest.MenuWindow.SetFavDrink(choice);
        }

        void onDrinkChanged(object o, EventArgs args)
        {
            ComboBox combo = o as ComboBox;
            if (o == null)
                return;

            TreeIter iter;

            if (combo.GetActiveIter (out iter))
                choice = ((string) combo.Model.GetValue (iter, 0));
        }
    }

    class WindowTwo
    {
        ComboBox cheese = ComboBox.NewText();
        static string choice;

        public WindowTwo ()
        {
            Window w2 = new Window ("Window Two");

            VBox vb = new VBox();

            Button sendCheese = new Button("Send Cheese");

            sendCheese.Clicked += OnSend;
            cheese.Changed += new EventHandler(onCheeseChanged);

            cheese.AppendText("Gorgonzola");
            cheese.AppendText("Edam");
            cheese.AppendText("Brie");
            cheese.AppendText("Feta");

            vb.PackStart(cheese, false, false, 1);
            vb.PackStart(sendCheese, false, false, 1);

            w2.Add(vb);
            w2.ShowAll();
        }

        void OnSend(object sender, EventArgs args)
        {
            WaitingTest.MenuWindow.SetFavCheese(choice);
        }

        void onCheeseChanged(object o, EventArgs args)
        {
            ComboBox combo = o as ComboBox;
            if (o == null)
                return;

            TreeIter iter;

            if (combo.GetActiveIter (out iter))
                choice = ((string) combo.Model.GetValue (iter, 0));
        }
    }
}

上述情况是,两个窗口立即打开,标签立即使用默认数据更新,而不是在两个窗口中选择的数据。所以我有两个问题:

1)如何让它打开WindowOne,等到检索到数据,然后打开WindowTwo,等到检索到数据,然后更新标签?

2)如何编辑“OnSend”功能,以便在调用SetFavDrink / SetFaveCheese函数后关闭窗口?

有人可以指点我这里正确的方向吗?我是这个编程云雀的新手,所以要温柔!

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的(注意这可能会被清理......):

using System;
using Gtk;


    namespace WaitingTest
    {
        public class MainClass
        {       
            static void Main (string[] args)
            {
                Application.Init ();
                new MenuWindow();
                Application.Run();
            }
        }
        public class MenuWindow
        {
            public static string favDrink = "Water";
            public static string favCheese = "Chedder";
            public static Label output = new Label();

            public MenuWindow()
            {
                Application.Init ();

                Window test = new Window ("Main Window");

                VBox vb = new VBox();
                Button getData = new Button("Get Data");
                Button quitApp = new Button("Quit");

                getData.Clicked += OnGetData;
                quitApp.Clicked += OnExit;

                vb.PackStart(getData, false, false, 1);
                vb.PackStart(quitApp, false, false, 1);
                vb.PackStart(output, false, false, 1);

                test.Add(vb);
                test.ShowAll();

                Application.Run();
            }

            static void OnGetData(object sender, EventArgs args)
            {
                // Open up Window to select drink
                new WindowOne();
            }

            public static void SetFavDrink(string d)
            {
                MenuWindow.favDrink = d;
                Console.WriteLine(favDrink);
            }

            public static void SetFavCheese(string c)
            {
                MenuWindow.favCheese = c;
                Console.WriteLine(favCheese);
            }

            static void OnExit(object sender, EventArgs args)
            {
                Application.Quit();
            }

            protected void OnDeleteEvent(object sender, DeleteEventArgs a)
            {
                Application.Quit ();
                a.RetVal = true;
            }   
        }

        class WindowOne
        {
            ComboBox drink = ComboBox.NewText();
            static string choice;
            static Window w1 = new Window ("Window One");
            public WindowOne ()
            {


                VBox vb = new VBox();

                Button sendDrink = new Button("Send Drink");

                sendDrink.Clicked += OnSend;
                drink.Changed += new EventHandler(onDrinkChanged);

                drink.AppendText("Beer");
                drink.AppendText("Red Wine");
                drink.AppendText("White Wine");
                drink.AppendText("Whiskey");

                vb.PackStart(drink, false, false, 1);
                vb.PackStart(sendDrink, false, false, 1);

                w1.Add(vb);
                w1.ShowAll();
            }

            void OnSend(object sender, EventArgs args)
            {
                WaitingTest.MenuWindow.SetFavDrink(choice);
                WindowOne.w1.Destroy();
                new WindowTwo();

            }

            void onDrinkChanged(object o, EventArgs args)
            {
                ComboBox combo = o as ComboBox;
                if (o == null)
                    return;

                TreeIter iter;

                if (combo.GetActiveIter (out iter))
                    choice = ((string) combo.Model.GetValue (iter, 0));
            }
        }

        class WindowTwo
        {
            ComboBox cheese = ComboBox.NewText();
            static string choice;
            static Window w2 = new Window ("Window Two");
            public WindowTwo ()
            {


                VBox vb = new VBox();

                Button sendCheese = new Button("Send Cheese");

                sendCheese.Clicked += OnSend;
                cheese.Changed += new EventHandler(onCheeseChanged);

                cheese.AppendText("Gorgonzola");
                cheese.AppendText("Edam");
                cheese.AppendText("Brie");
                cheese.AppendText("Feta");

                vb.PackStart(cheese, false, false, 1);
                vb.PackStart(sendCheese, false, false, 1);

                w2.Add(vb);
                w2.ShowAll();
            }

            void OnSend(object sender, EventArgs args)
            {
                WaitingTest.MenuWindow.SetFavCheese(choice);
                WindowTwo.w2.Destroy();
                MenuWindow.output.Text="Drink: "+MenuWindow.favDrink+"\nCheese: "+MenuWindow.favCheese;
            }

            void onCheeseChanged(object o, EventArgs args)
            {
                ComboBox combo = o as ComboBox;
                if (o == null)
                    return;

                TreeIter iter;

                if (combo.GetActiveIter (out iter))
                    choice = ((string) combo.Model.GetValue (iter, 0));
            }
        }
    }

`