[Gtk-sharp-list] Proper design for creating windows

Milen Dzhumerov gamehack at 1nsp1r3d.co.uk
Thu Jul 14 17:54:16 EDT 2005


Matthew Beckler wrote:

>Hello everyone,
>
>I have seen in various places that you can create your programs by
>extending the basic Gtk.Window, but you can also just create a new
>instance of a window. I am wondering which is the 'more correct' way to
>do this. I have included equivalent examples for both ways.
>
>Inheriting from Gtk.Window:
>
>class MyWindow : Gtk.Window
>{
>    	public MyWindow () : base ("Driver")
>    	{
>		this.Title = "This is the title of MyWindow"; 
>		this.SetDefaultSize (400, 300);
>		this.DeleteEvent += new DeleteEventHandler
>(OnMyWindowDelete);
>
>    		Label label = new Label("This label is the only thing
>here");
>		this.Add(label);
>    		this.ShowAll ();
>    	}
>    	
>    	private void OnMyWindowDelete (object o, DeleteEventArgs args)
>    	{
>    		Application.Quit ();
>		args.RetVal = true;
>    	}
>    	
>	[STAThread]
>	static void Main(string[] args) 
>	{
>		Application.Init();
>		new MyWindow();
>		Application.Run();
>	}
>}
>
>Creating a new Gtk.Window:
>
>class MyWindow
>{
>	private static void OnMyWindowDelete (object o, DeleteEventArgs
>args)
>	{
>		Application.Quit ();
>		args.RetVal = true;
>	}
>    
>	[STAThread]
>	static void Main(string[] args) 
>	{
>		Application.Init();
>
>		Gtk.Window win = new Window("This is the title of
>MyWindow");
>		win.Title = "This is the title of MyWindow"; 
>		win.SetDefaultSize (400, 300);
>		win.DeleteEvent += new DeleteEventHandler
>(OnMyWindowDelete);
>
>		Label label = new Label("This label is the only thing
>here");
>		win.Add(label);
>		win.ShowAll ();
>		Application.Run();
>	}
>}
>
>Again, I am wondering which way I should be doing things.
>
>Thanks in advance,
>
>Matthew
>
>_______________________________________________
>Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
>
>
>  
>
Hello,

Personally I think the 1st is better, because you can split your code 
into different files, eg have a file mainwindow.cs which just takes care 
of the main window. This way you can separate the application into 
modules and manage it more efficiently. It would be much easier just to 
edit this file when you want to change something on the main window 
instead of opening a huge main.cs and crawling through all the different 
bits etc. So clearly model #1 is the winner for any real life 
application. If you're just playing with eg GTK#, i think model #2 is 
much better - just chuck all the functions, objects etc in one file and 
play with it. But generally, avoid spaghetti code.

Regards


More information about the Gtk-sharp-list mailing list