Click here to Skip to main content
13,287,856 members (38,329 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 26 Mar 2012

Preferences Form Settings Dialog

, 27 Mar 2012
Rate this:
Please Sign up or sign in to vote.
How to build a standard preferences Form with tree node and content columns.


A lot of desktop applications use a typical preferences dialog composed by a section node menu and the relative content selected on the right. 

Let's see how make it.

Using the code

First off, we start creating a form, an horizontal split container within a tree view and a TableLayoutPanel on the right columns.

Then we place two buttons, Save and Close on the bottom part of the TableLayoutPanel content.

This will look like this: 

Typically, every single panel called by the menu section will have inside code for saving, validating and load settings at startup.

This is all granted by this interface

interface IUserControlPrefPanel
    bool IsValid();
    void Save();
    void LoadSettings();

Now we can start building up all custom panels needed using User Control and our interface IUserControlPrefPanel.

Once designed all the panels and implemented the three functions for each of these, the main form will have a code like this

public Form1()

private UserControlPanelN0 userControlPanelN0 = new UserControlPanelN0();
private UserControlPanelN1 userControlPanelN1 = new UserControlPanelN1();

private UserControl mActivePanel;

private bool statusChanged = false;

private void Form1_Load(object sender, EventArgs e)

    userControlPanelN0.Visible = false;
    userControlPanelN1.Visible = false;

    treeViewMenu.SelectedNode = treeViewMenu.Nodes[0];


private void AddEnableSaveOnChanges(Control obj)
    foreach (Control control in obj.Controls)
        control.TextChanged += EnableSave;

        // some events can occurs with your controls inside panel
        if (control.GetType() == typeof(CheckBox))
            ((CheckBox)control).CheckedChanged += EnableSave;

        if (control.GetType() == typeof(ListView))
            ((ListView)control).SelectedIndexChanged += EnableSave;

        // containers
        if (control.Controls.Count > 0)

private void EnableSave(object sender, EventArgs e)
    statusChanged = true;
    this.buttonSave.Enabled = true;

private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs e)
    UserControl newPanel = null;
    switch (e.Node.Index)
        case 0: newPanel = userControlPanelN0; break;
        case 1: newPanel = userControlPanelN1; break;
        // etc...
    if (newPanel != null)
        if (mActivePanel != null)
        newPanel.Dock = DockStyle.Fill;                
        tableLayoutPanelRight.SetColumnSpan(newPanel, 2);

        mActivePanel = newPanel;

private void buttonCancel_Click(object sender, EventArgs e)
    bool exit = !statusChanged;

    if (!exit)
        if (MessageBox.Show("Settings changed, close anyway?", 
                "Confirm exit", MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question) == DialogResult.Yes)
            exit = true;

    if (exit)

private void buttonSave_Click(object sender, EventArgs e)
    bool isValid = true;

    isValid = isValid && userControlPanelN0.IsValid();
    isValid = isValid && userControlPanelN1.IsValid();

    if (isValid)
        buttonSave.Enabled = false;
        MessageBox.Show("No valid settings specified", "ERROR", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

Notice that AddEnableSaveOnChanges take all the interested controls inside the panel binding the EnableSave function on any value changes. 

Here are some previews:

That's all!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fabrizio Stellato
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player
Audio Pitch & Shift
Ultimate Music Tagger
Modern Log Viewer
Pitch Tuner
Modern Audio Tagger
Win Log Inspector
Win Log Analyzer

You may also be interested in...

Comments and Discussions

QuestionWorks Great Pin
Luis Ricardo Delgado Cortés12-Feb-14 17:59
memberLuis Ricardo Delgado Cortés12-Feb-14 17:59 
AnswerRe: Works Great Pin
Fabrizio Stellato12-Feb-14 23:46
memberFabrizio Stellato12-Feb-14 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 27 Mar 2012
Article Copyright 2012 by Fabrizio Stellato
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid