DaMasta
December 14th, 2005, 10:18 AM
Teaching myself some GTK and C# using monodevelop. I created a button and would like to change a label's text when clicked. Here's what I have.
using System;
using Gtk;
class buMain {
static void Main()
{
Application.Init();
Window mainWindow = new Window("Easy Backup");
mainWindow.DeleteEvent += new DeleteEventHandler (delete_backuputil);
mainWindow.SetDefaultSize (200, 150);
VBox mbBox = new VBox(false,2);
//Creates a menubar
MenuBar mb = new MenuBar();
//Stars the file portion of the menubar
Menu file_menu = new Menu();
//Creation of the open option and then appends the open option to the file portion
MenuItem open_item = new MenuItem("Open...");
file_menu.Append(open_item);
//Creation of the exit option and then appends the exit option to the file portion
MenuItem exit_item = new MenuItem("Exit");
file_menu.Append(exit_item);
exit_item.Activated += new EventHandler (exit_backuputil);
MenuItem file_item = new MenuItem("File");
file_item.Submenu = file_menu;
//Stars the edit portion of the menubar
Menu edit_menu = new Menu();
//Creation of the policy option and then appends the policy option to the edit portion
MenuItem policy_item = new MenuItem("Policy...");
edit_menu.Append(policy_item);
MenuItem edit_item = new MenuItem("Edit");
edit_item.Submenu = edit_menu;
mb.Append(file_item);
mb.Append(edit_item);
mbBox.PackStart(mb, false, false, 0);
Label testLabel = new Label("no");
mbBox.PackStart(testLabel, false, false, 0);
Button testButton1 = new Button("testing1");
testButton1.Clicked += new EventHandler(change_label);
mbBox.PackStart(testButton1, false, false, 0);
Button testButton2 = new Button("testing2");
mbBox.PackStart(testButton2, true, true, 0);
mainWindow.Add(mbBox);
mainWindow.ShowAll();
Application.Run();
}
static void change_label (object obj, EventArgs args)
{
testLabel.Text = "Yes!";
}
static void delete_backuputil (object o, DeleteEventArgs args)
{
Application.Quit ();
}
static void exit_backuputil (object o, EventArgs args)
{
Application.Quit ();
}
}
And here is the error:
backuputil.cs(53,11): error CS0246: The type or namespace name `testLabel' could not be found. Are you missing a using directive or an assembly reference?
backuputil.cs(53,11): error CS0103: The name `testLabel' does not exist in the context of `buMain'
Compilation failed: 2 error(s), 0 warnings
using System;
using Gtk;
class buMain {
static void Main()
{
Application.Init();
Window mainWindow = new Window("Easy Backup");
mainWindow.DeleteEvent += new DeleteEventHandler (delete_backuputil);
mainWindow.SetDefaultSize (200, 150);
VBox mbBox = new VBox(false,2);
//Creates a menubar
MenuBar mb = new MenuBar();
//Stars the file portion of the menubar
Menu file_menu = new Menu();
//Creation of the open option and then appends the open option to the file portion
MenuItem open_item = new MenuItem("Open...");
file_menu.Append(open_item);
//Creation of the exit option and then appends the exit option to the file portion
MenuItem exit_item = new MenuItem("Exit");
file_menu.Append(exit_item);
exit_item.Activated += new EventHandler (exit_backuputil);
MenuItem file_item = new MenuItem("File");
file_item.Submenu = file_menu;
//Stars the edit portion of the menubar
Menu edit_menu = new Menu();
//Creation of the policy option and then appends the policy option to the edit portion
MenuItem policy_item = new MenuItem("Policy...");
edit_menu.Append(policy_item);
MenuItem edit_item = new MenuItem("Edit");
edit_item.Submenu = edit_menu;
mb.Append(file_item);
mb.Append(edit_item);
mbBox.PackStart(mb, false, false, 0);
Label testLabel = new Label("no");
mbBox.PackStart(testLabel, false, false, 0);
Button testButton1 = new Button("testing1");
testButton1.Clicked += new EventHandler(change_label);
mbBox.PackStart(testButton1, false, false, 0);
Button testButton2 = new Button("testing2");
mbBox.PackStart(testButton2, true, true, 0);
mainWindow.Add(mbBox);
mainWindow.ShowAll();
Application.Run();
}
static void change_label (object obj, EventArgs args)
{
testLabel.Text = "Yes!";
}
static void delete_backuputil (object o, DeleteEventArgs args)
{
Application.Quit ();
}
static void exit_backuputil (object o, EventArgs args)
{
Application.Quit ();
}
}
And here is the error:
backuputil.cs(53,11): error CS0246: The type or namespace name `testLabel' could not be found. Are you missing a using directive or an assembly reference?
backuputil.cs(53,11): error CS0103: The name `testLabel' does not exist in the context of `buMain'
Compilation failed: 2 error(s), 0 warnings