2013-02-04 195 views
1

我正在使用C#與GTK編寫一個包含按鈕的主窗口的應用程序。當按下其中一個按鈕時,它會調用一個Event Handler函數,該函數打開一個包含用戶填寫表單的新窗口。到目前爲止,所有工作都很好。在C#中打開GTK窗口,返回數據並關閉窗口,然後打開另一個窗口?

我遇到的問題是我需要讓這個Event Handler函數打開這個窗口,等待它被填充和關閉,然後打開第二個窗口,等待它被填充並且關閉,然後處理所有這些捕獲的數據。 我做了一個簡單的例子來說明我的意思(我在下面的例子中會更有意義打開一個窗口有兩個組合框明白,但那是因爲這只是一個例子!)

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函數後關閉窗口?

難道有人請在這裏指出正確的方向嗎?我對這個編程百靈不熟悉,所以要溫柔!

回答

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)); 
      } 
     } 
    } 

` 
相關問題