3

I usually do database and web development but at the moment I'm learning desktop development with Mono and Gtk# using using the MonoDevelop IDE. I'm also using the Stetic GUI Designer inside MonoDevelop to create the GUI.

What are the best practices when using a GUI designer to encapsulate extra logic that is related to a widget used in the GUI designer?

For example I may use a TreeView widget but I'll also need to have logic specific to how the cells render, deleting selections of rows, etc. My first thought would be to simply extend the Treeview like so:

public class MyTreeView : TreeView {}

But I don't think I can do that and still use the GUI Designer (correct me if I'm wrong).

What I'm doing now is making a class that has the TreeView as a member of it:

public class MyTreeView 
{
    private widget;

    private MyTreeView(TreeView treeView)
    {
        this.widget = treeView;
    }
}

Am I on the right track or is there some special and fancy pattern or C# language feature that I don't know of that I should be using instead?

0

1 Answer 1

3

You have a few patterns you can follow with .NET development. This is coming from a WinForms background - I've not used GTK# although it's probable that the skills are transferable. Both methods you've discussed are acceptable but which is correct depends on what you're trying t achieve.

  1. Derive a class and override the functionality you require. This is the choice to make when you want to override functionality of a well-designed control. For example, you might override the OnPaint to change how the control paints. How far you can go with deriving depends on how well designed the base control is. An example here could be overriding the ComboBox to draw an icon next to items.

  2. Use a custom control if you want full management of the control. This particular type is for when you are implementing your own control and you don't want any extra functionality. For example, you might be making your own list view control from scratch.

  3. Use a user control if you want to combine different controls into the one surface. For example, you might compartmentalize parts of a form using this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.