Click here to Skip to main content
13,902,349 members
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 27 Apr 2004
Licenced CPOL

Column Separator Control

, 27 Apr 2004
Rate this:
Please Sign up or sign in to vote.
A control to split text into fixed width columns


I was going through my old projects recently and have found an interesting control I have wrote several years ago which was never actually used. I'm not sure if anybody still interested in MFC controls nowadays, but just in case somebody is I decided to publish this control. Maybe you'll make use of it. I hope you'll find it useful.


Have you ever used a function of Microsoft® Excel to import text data with fixed width where fields are aligned in columns with spaces between each field (Fig. 1)?

The import text dialog of Microsoft Excel

Fig. 1. The import text dialog of Microsoft Excel

Have you ever needed/wanted to use similar functionality in your own application to allow user split the text into columns with fixed width? Using a separator control presented by this article you may easily introduce this functionality into your application. An example of separator control you can se on the Fig. 2.

An example of separator control in use

Fig. 2. An example of separator control in use

General appearance

  • CSeparatorsCtrl is an MFC control that provides functionality to request a user to select/split text into fixed width columns.
  • CScaleLine is a control that represents a scale line with numbers on the top of a separator control and used by CSeparatorsCtrl.
  • CFlatDisplay is a control that displays a text and columns it is split to by user. It is also used by CSeparatorsCtrl.

You may think of facade pattern in place of CSeparatorsCtrl that uses extended functionality of CScaleLine and CFlatDisplay controls to hide away their complexity and bring to developer a plain interface of a single control.

An instance of CSeparatorsCtrl control might be created during design time in the form designer of Visual Studio with a help of custom control as well as dynamically at runtime.

This control takes advantage of using CAutoFont class that is written by Jamie Nordmeyer and might be found here:

Functionality provided by CSeparatorsCtrl class:

void SetDisplayFontSize(long size)Sets the font size of displayed text.
void SetDisplayColor(COLORREF txtColor, COLORREF lineColor, COLORREF 
Sets the colors of CFlatDisplay control:
  • txtColor - the color of text (default color is black);
  • lineColor - the color of separator lines (default color is black);
  • bgColor - the background color of text field (default color is white).
void SetScaleColor(COLORREF value)Sets the color of CScaleLine control.
void SetScaleIntervals(long nBig, long nSmall)Sets the scale intervals. nBig defines big intervals where the numbers are displayed, nSmall defines smaller intervals that are marked out only by the size of stroke.
void SetShowFrame(bool value)Defines whether to show a frame around separator control or not.
void SetText(CString text)Sets the text that is displayed in separator control.
long GetCharHeight()Returns the height of displayed characters (the monospaced font is used).
long GetCharWidth()Returns the width of displayed characters.
CDWordArray * GetSeparators()Returns the reference to dynamic DWORD array of separators selected by user.
virtual bool OnBeforeAddColumn(long nPos)The event handler that is called before column is added when user has clicked a mouse. Declared as virtual so that you may override it if inheriting from this control. If this method returns false a new column separator will not be added.
virtual bool OnBeforeRemoveColumn(long nPos)The event handler that is called before column is removed when user has clicked a mouse. If this method returns false the column separator will not be removed.

Usage of control

To use a separator control simply place a custom control on your dialog and declare a property to be an instance of CSeparatorsCtrl class. Then you may use this property after dialog is created, for example to set initial properties of separator control place the following code into OnCreate method of your dialog:

int CSepCtrlTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;
    m_SepCtrl.SetText("Text to fill up the content of separator control");
    return 0;

You may also create an instance of separator control at runtime and place it on your dialog, for example:

void CTestDynamicDlg::OnCreateCtrl()
    if (m_pSepCtrl == NULL)
        m_pSepCtrl = new CSeparatorsCtrl;
            "Doesn't matter", 
            WS_CHILD | WS_VISIBLE,
            CRect(0, 0, 400, 150),
        m_pSepCtrl->SetText("something for test -------->>>> ----->>>");
        m_pSepCtrl->SetScaleIntervals(5, 0);
        m_pSepCtrl->SetDisplayColor(RGB(0,0,0), RGB(0,155,0), RGB(200,200,200));

To list all selected by user columns you can use GetSeparators method provided by CSeparatorsCtrl class as it is shown here:

void CSepCtrlTestDlg::OnShowSeparators() 
    CDWordArray * pX = m_SepCtrl.GetSeparators();
    CString str;
    for (int i = 0; i < pX->GetSize(); i++)
        str.Format("Col: %d", pX->GetAt(i));


  • Version 1.0 - once upon a time in 2001, published 28 Apr 2004.


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


About the Author

Vladimir L.
Software Developer (Senior)
Russian Federation Russian Federation
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionDo you have a C# style as that? Pin
zhongguohebeijing23-Dec-07 19:52
memberzhongguohebeijing23-Dec-07 19:52 
AnswerRe: Do you have a C# style as that? Pin
xwandragon7811-May-09 4:30
memberxwandragon7811-May-09 4:30 
QuestionDo you have a C# style as that? Pin
zhongguohebeijing23-Dec-07 19:51
memberzhongguohebeijing23-Dec-07 19:51 
QuestionDrag separators? Pin
njkayaker11-May-04 9:40
membernjkayaker11-May-04 9:40 
AnswerRe: Drag separators? Pin
Vladimir L.11-May-04 10:36
memberVladimir L.11-May-04 10:36 
GeneralRe: Great text! Pin
Member 108164511-May-04 2:52
memberMember 108164511-May-04 2:52 
GeneralGreat text! Pin
Alexeys129-Apr-04 23:53
memberAlexeys129-Apr-04 23:53 
GeneralJust what I was looking for... Pin
Kochise28-Apr-04 21:05
memberKochise28-Apr-04 21:05 
QuestionRe: Just what I was looking for... Pin
Mike Voissem2-Nov-05 11:45
memberMike Voissem2-Nov-05 11:45 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 28 Apr 2004
Article Copyright 2004 by Vladimir L.
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid