Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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