Click here to Skip to main content
Click here to Skip to main content

Editable Multi-line ListBox

By , 25 Mar 2003
 

Screenshot of ListBoxXP

Introduction

If you take a look at the snapshot and run the .exe file you`ll realise that there`s nothing I need to say here. I needed this control with item editing, insertion, deletion, and moving capabilities and I asked, "why not share it with people?"

Using the Code

It`s very easy to use just add a variable based on the class and that`s all. You work with it like a normal listbox.

//
// powered by NetControl.ro
//
// .............
void CListBoxXP::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    CString szBuf="";
    COLORREF col;
    if(((int)lpDrawItemStruct->itemID==edited)&&(IsWindow(hHelper)))
        return;
    {
        GetText(lpDrawItemStruct->itemID,szBuf);
         SetTextColor(lpDrawItemStruct->hDC,RGB(0,0,0));
         RECT r;
         memcpy((void*)&r,&lpDrawItemStruct->rcItem,sizeof(RECT));
         if(itm==(int)lpDrawItemStruct->itemID)
         {
            HBRUSH br;
            br=::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.9));
            ::FillRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct-&gt;rcItem,br);
            ::DeleteObject(br);
         }
         else
         {
             FillRect(lpDrawItemStruct-&gt;hDC,<BR>                &lpDrawItemStruct->rcItem,<BR>                (HBRUSH)GetStockObject(WHITE_BRUSH));
         }<BR>
         r.left+=5;
         r.top+=2;
         col=RGB(255,255,255);
         if(lpDrawItemStruct->itemState & ODS_SELECTED)
         {
            col=RGB(220,220,220);
            HBRUSH br;
            br=::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.75));
            ::FillRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct->rcItem,br);
            ::DeleteObject(br);
         }
         if(lpDrawItemStruct-&gt;itemState & ODS_FOCUS)
         {
            HBRUSH br;
            br = ::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.5));
            ::FrameRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct->rcItem,br);
            ::DeleteObject(br);
         }
         SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);<BR>
         r.left-=3;
         r.top-=2;
         if(CountLines(szBuf) > 1)
             DrawText(lpDrawItemStruct->hDC,szBuf,<BR>                strlen(szBuf),&r,<BR>                DT_LEFT|DT_TOP|DT_WORDBREAK);
         else
             DrawText(lpDrawItemStruct->hDC,szBuf,<BR>                strlen(szBuf),&r,<BR>                DT_LEFT|DT_VCENTER|DT_SINGLELINE);
    }
}

void CListBoxXP::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
    HDC hDC;
    TEXTMETRIC tm;
    char szBuf[512]="";
    ::SendMessage(this->m_hWnd, LB_GETTEXT,<BR>        (WPARAM)lpMeasureItemStruct->itemID,(LPARAM)szBuf);<BR>
    hDC = ::GetDC(this->m_hWnd);
    CRect r;
    GetClientRect(r);
    ::GetTextMetrics(hDC, &tm);
    lpMeasureItemStruct->itemHeight=(tm.tmHeight)*CountLines(szBuf);
    ::ReleaseDC(this->m_hWnd, hDC);
}
// ...<BR>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

TomKat
Web Developer
Romania Romania
Member
I have been programming for the past 6 years in VBasic, Delphi, C, C++ .
I also have extended knowledge of webdesign (HTML, CSS, JavaScript, VBScript), webprogramming (PHP, ASP, ASP.NET (C#)) and database integration (mySql, MSSQL Server).
And when I`m not programming I`m working out or working on some personal projects .
Last but not least I`m looking for a project-based job in programming or webdesign .

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGet The edited text in User classmemberArtiGujare26 Jan '06 - 2:25 
Hi!
I am using the editable listboxXP class.
I need to save the changes done to the edit box after user clicks a "save" button.
I am using GetText to retreive the edited data from the list box
 
::onsave()
{
UpdateData(true);
CString strRemarks;
mRemarksListBox.GetText(0, strRemarks);
// save remarks
}
 
the text still shows the original text and not the updated text. Any idea how to get th edited text in my user class.
Also I have problem with font settings. The text in the editable edit box seems too small. I want to increase the font size.
Please help me.
 
Thanks and Regards,
Arti Gujare
GeneralIssues with the control with Japanese Windowsmembercdesylva27 Sep '04 - 15:57 
We have implemented a version of this control with Japanese Windows, but the horizontal scroll bar (which is at the bottom of the control), appears at the top! Any ideas? This is rather wierd. OMG | :OMG:
GeneralRe: Issues with the control with Japanese Windowsmembercdesylva27 Sep '04 - 15:58 
That is, with US Windows XP the horizontal scroll bar is at the bottom of the control (as it should be). The problem appears with Japanese Windows.
Generalneed help pleasesussHeex8 Jul '04 - 10:43 
I am kind of new to programming and I can't get this to work. Any help would be highly appreciated.
I get a message saying it can't find CNetControl.dsp and I get a lot of errors when I'm trying to compile.
I dont know c++ very much. What I want to do is include this control in a c# windows application.
 
Thanks in advance.
GeneralRe: need help pleasememberTomKat8 Jul '04 - 23:07 
Remove dependency to cnetcontrol.dps . it`s not required .
 

I am the MIGHTY KEEPER of the BOOK OF KNOWLEDGE . Contact me to get your copy .
GeneralBug on first additionmemberKarthik Murugan21 Mar '04 - 17:15 
When i add the first item by INSERT key, the edit box is hidden. Please check.
 
Karthik
Generalbug!membertang_ghost12 Mar '04 - 21:44 
can't insert string more than 500 length each line .
for example:
m_LB.AddString("1234567890...1234567890");//500 length
Generalcan't found CNetControl.dspmembertang_ghost12 Mar '04 - 21:13 
when open the DSW ,clue to browse for project CNetControl
 
i am a programmer
GeneralRe: can't found CNetControl.dspmemberMonirTigerit19 Jul '05 - 20:48 
I am facing the same problem to load the workspace file Frown | :( can abyone fix it???
Generaldebug assertionsmemberto_be_unknown4 Jun '03 - 0:41 
Hi
 
There seems to some error when moving the first line down, or the last line up.
 
In VK_RETURN:
i would be 0, and therefor GetText(i-1,txt1); is out of bound
 
In VK_BACK:
The
int n=InsertString(i,txt1);
is illegal / LB_ERR / since this index has just been deleted.
 

 
When entering new text an extra line (in output not data) is added.
 

I've added

MoveToEx(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left,lpDrawItemStruct->rcItem.bottom-1, NULL);
LineTo(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.right,lpDrawItemStruct->rcItem.bottom-1);

 
to the end of DrawItem(). It shows a line after every entry. But how can I change the color from black to a more neutral gray ?
 

 

 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 26 Mar 2003
Article Copyright 2003 by TomKat
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid