在C#中打开GTK窗口,返回数据并关闭窗口,然后打开另一个窗口
本文关键字:窗口 然后 另一个 数据 GTK 返回 | 更新日期: 2023-09-27 17:57:29
我使用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函数后关闭窗口?
有人能给我指一下这里的正确方向吗?我是这个编程百灵鸟的新手,所以要温柔!
我认为这就是你想要的(注意,这可能会被清理…):
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));
}
}
}
`