[Gtk-sharp-list] Child windows in a seperate dll file.

Michael Hutchinson m.j.hutchinson at gmail.com
Tue May 6 00:48:31 EDT 2008


On Tue, Apr 29, 2008 at 1:42 PM, Induane <oldspiceap at gmail.com> wrote:
>
>  Greetings, I'm new to programming and thus to C#.  In fact I'm only a few
>  weeks into my learning process.  I've written an app for work and now am
>  rewriting it so that its not so hideous and ugly.  At the moment its at like
>  8000 lines and all in one class with just a ton of methods.

Wow. that's pretty big. I generally find it's unusual for a file to
pass 1000 lines, and very rare to pass 2000.

I think your problem is that you are not structuring your code in an
object-oriented way. When your are designing your code, structure it
as objects with behaviors, properties etc inherent to those objects.
If there is common functionality that you need to share between
similar objects, put it in a base class, etc.

>  Anyways I've started rewriting it and decided to split the eventhandlers
>  into their own dll file so that it would be easier to maintain and cleaner.

Why are you splitting event handlers into a separate dll file? What
kind of handlers are you splitting out? Event handlers are generally
pretty specific to the situation, so this doesn't make much sense to
me.

You can have more than one .cs file compile into a single dll or exe

>  This worked to a degree but I'd like to create dialogs that come up and
>  unfortunately I cannot seem to make that happen as it cannot compile due to
>  being unable to locate the parent window.

Note that the signature of an EventHandler (object sender, EventArgs
args) passes you the object that emitted the event as the first
argument.

>  I have the program split up into
>  three files now:
>
>  First is a simple file for the exe:
>
>  using System;
>  using drawInterface;
>  // We'll be using these later I think.
>  // using Gtk;
>  // using GtkSharp;
>
>  namespace ApplicationWrapper
>  {
>
>         class applicationWrapper
>         {
>
>                 static void Main()
>                 {
>
>                 sowInterface drawSow = new sowInterface();
>                 drawSow.MainInterface();
>
>                 }
>
>         }
>
>  }
>
>
>
>  Simple enough even for someone as slow as me.  Now onto the code to draw the
>  interface (almost nothing yet but its a rewrite ;) don't make fun!
>
>  using System;
>  using Gtk;
>  using GtkSharp;
>  using eventHandlers;
>
>  namespace drawInterface
>  {
>
>
>         public class sowInterface
>         {
>
>                 public void MainInterface ()
>
>                         {
>
>                         Application.Init ();
>
>                         Window window = new Window ("Statement of Work Generator");
>
>                         /* Set a handler for delete_event that immediately
>                         * exits GTK. */
>                         window.DeleteEvent += OnDelete;
>
>                         // sets windows size by default.  We'll see if we need this later on.
>                         // window.SetDefaultSize (200,200);
>
>                         /* Set the border width of the window and show the window itself.*/
>                         window.BorderWidth= 5;
>
>
>                         /* Create a big ass table and place it inside the main window.*/
>                         Table table = new Table (26, 4, false);
>                         table.SetColSpacing(0, 16);
... snip
>
>                         window.ShowAll();
>                         Application.Run();
>
>                         }
>
>                         OnDelete2 OnDelete = new OnDelete2();
>
>                         /*
>
>                         // Handles exiting when you click the X button - temp disabled as trying
>  to split into dll
>                         static void OnDelete (object obj, DeleteEventArgs e)
>                         {
>                         Application.Quit();
>                         }
>
>                         */
>
>
>         }
>
>
>
>  }

For a start, you could have this class subclass Gtk.Window.

public class MainInterface : Gtk.Window
{
    public MainInterface () : base ("My window title")
    {
        //construct the GUI
        //hook up the event handlers
    }

    //define the event handlers
}

then your main class

public class MyApplication
{
    public static void Main ()
    {
        Application.Init ()
        MainWindow win = new MainWindow ();
        Application.Run ()
    }
}

>
>
>
>
>
>  and now the file I'm using for the event handler:
>
>
>  using System;
>  using Gtk;
>  using GtkSharp;
>
>  namespace eventHandlers
>  {
>
>         public class event_handlers
>         {
>
>                 public static void OnDelete2 (object obj, DeleteEventArgs e)
>                 {
>                         /*
>                         MessageDialog md = new MessageDialog (window,
>                         DialogFlags.DestroyWithParent,
>                         MessageType.Question,
>                         ButtonsType.YesNo, "Are you sure you want to quit?");
>
>                         ResponseType result = (ResponseType)md.Run ();
>
>                         if (result == ResponseType.Yes)
>                         {
>                         Application.Quit();
>                         }
>                         else
>                         {
>                         md.Destroy();
>                         }
>                         }
>                         */
>                         Application.Quit();
>
>         }
>
>  }

And again, you can encapsulate this into a class:

public class QuitDialog : MessageDialog
{
    public QuitDialog (Window parent)
        : base (window,
                   DialogFlags.DestroyWithParent,
                   MessageType.Question,
                   ButtonsType.YesNo,
                   "Are you sure you want to quit?")
    {
    }

    public void RunDialog ()
    {
        ShowAll ();
        ResponseType result = (ResponseType)Run ();
        if (result == ResponseType.Yes) {
            Application.Quit();
        }
        Hide ();
    }

    public override Dispose ()
    {
        Destroy ();
        base.Dispose ();
    }
}

Note that this is a nice re-usable chunk of code that will go quite
nicely in its own file. You can compile it into your main exe or into
a separate dll if you want to share it between apps.

You can use this with

using (QuitDialog qd = new QuitDialog ((Window) sender)) {
    qd.RunDialog ();
}



>
>
>  As you can see I've commented out the confirmation dialog box.  I had though
>  instancing it in my main drawinterface would make it work but it doesn't.
>  Can anyone point me in the right direction?

Look up the difference betwen static and instance members. A static
member in the event_handlers class has no way to access members of an
instance of the main app class without a pointer to that instance.

>  Also eventually part of the code will eventually have certain events calling
>  methods that change the values and labels of other widgets so I need to
>  figure out how to do that across libraries as well.

If your widgets are public properties of the main window, you can
access them via a pointer to an instance of that window, e.g.
  MainWindow main = (MainWindow)sender;
  main.SomeLabel.Text = "Hello";

However, I strongly advise against breaking up a class into pieces
without fixing the inderlying structure. You should structure your
code into objects with the behaviours specific to those objects baked
into them.

>  Many thanks and kind regards.  Hopefully someone can point me in the right
>  direction.

I suggest taking a look at MonoDevelop and its GTK# designer. It
designs widgets and windows based on these principles.

In particular http://www.monodevelop.com/Creating_custom_widgets_with_MonoDevelop

>
>  Brant Watson
>
>  P.S. I've also attached an archive that includes my old (horrible) source
>  code as well as the beginnings that are listed above and the makefile to
>  compile the new version.  Eventually I'd like to add a preferences dialog as
>  well and make that its own library so that it can be worked on
>  independently.  So I'll need to be passing things back and fourth like a
>  madman.
>  http://www.nabble.com/file/p16966107/StatementofWork.tar.gz
>  StatementofWork.tar.gz
>  --
>  View this message in context: http://www.nabble.com/Child-windows-in-a-seperate-dll-file.-tp16966107p16966107.html
>  Sent from the Mono - Gtk# mailing list archive at Nabble.com.
>
>  _______________________________________________
>  Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
>  http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>



-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Gtk-sharp-list mailing list