Click here to Skip to main content
Licence Public Domain
First Posted 7 Oct 2004
Views 96,137
Bookmarked 57 times

Indicating an empty ListView in C#

By | 7 Oct 2004 | Article
Have you wondered how you could show text in ListView control if it's empty, just like Microsoft does it in Outlook Express and in some other applications?

Introduction

Have you wondered how you could show text in ListView control if it's empty, just like Microsoft does it in Outlook Express and in some other applications?

Background

OnPaint() won't work

If you're thinking about OnPaint() event, forget it. ListView control is just a wrapper around the control in ComCtl and it doesn't fire this event.

UserControl: 100% pure C# solution but complicated

Another clean solution would be to make your own UserControl with one ListView and one label on the top which could be visible only if ListView would be empty, but there is one little problem with column resizing which draws a dark line on the control which goes under the label control which doesn't look very professional. We could possibly cover whole listview background, but then we would find problems with not showing scrollbars... well, fixing one problem creates another one... and so on...

Using the code

Probably the best solution is to override WndProc method with something like this. If m.MSG == 20 listview calls function to redraw background, so we just draw some string on top of it if listView doesn't contain any items.

protected override void WndProc(ref Message m)
{
  base.WndProc(ref m);
  if (m.Msg == 20)
  {
    if (this.Items.Count == 0)
    {
      _b = true;
      Graphics g = this.CreateGraphics();
      int w = (this.Width - g.MeasureString(
        _msg, this.Font).ToSize().Width)/2;
      g.DrawString(_msg, this.Font, SystemBrushes.ControlText, w, 30);
    }
  }
}

Well now it does what we need but sooner or later we will find out that this solution is not perfect. If you will try to resize the whole control, or only one of the columns, or if you add a new item to the listView and part or whole of your text string will be outside of the column's area, listView will became pretty messy. Improved overrided WndProc method, which is fixing all those problems, is here.

protected override void WndProc(ref Message m)
{
  base.WndProc(ref m);
  if (m.Msg == 20)
  {
    if (this.Items.Count == 0)
    {
      _b = true;
      Graphics g = this.CreateGraphics();
      int w = (this.Width - g.MeasureString(_msg, 
        this.Font).ToSize().Width)/2;
      g.DrawString(_msg, this.Font, 
        SystemBrushes.ControlText, w, 30);
    }
    else
    {
      if (_b)
      {
        this.Invalidate();
        _b = false;
      }
    }
  }

  if (m.Msg == 4127) this.Invalidate();
}

Yes, and we shouldn't forget to redraw the whole control on Resize event if listView is empty and showing our text string

private void ListView2_Resize(object sender, EventArgs e)
{
  if (_b) Invalidate();
}

That's it

Up-to-date version of this article can be found on http://www.hasko.com.au/blog/

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

lubos_h



Australia Australia

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBetter way Pinmemberksledziewski1:50 20 Jan '11  
GeneralText overlap when scroll bar moved Pinmemberscyeoh4:33 10 Dec '07  
Generalbug - scrolling PinmemberDarchangel9:01 25 Feb '06  
GeneralProblems with LVS_EX_DOUBLEBUFFER PinsussAnonymous22:28 7 Aug '05  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinsitebuilderUwe Keim22:16 29 Jun '06  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinsitebuilderUwe Keim22:41 29 Jun '06  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinmemberJeffTz2:41 29 Oct '09  
General.NET 2.0 Pinmembercompchris1:32 9 Dec '04  
GeneralRe: .NET 2.0 PinsussAnonymous9:14 27 Jul '05  
GeneralMessage not scrolling PinsussBrenton House10:11 8 Nov '04  
Generalscroll Pinmembermkuzma2:34 15 Oct '04  
GeneralMore flexible and consistent version Pinmembermav.northwind2:51 10 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLubos Hasko2:55 11 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind3:05 11 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLubos Hasko14:07 11 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind20:46 11 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLaudeci Oliveira4:20 13 Oct '04  
GeneralRe: More flexible and consistent version Pinmemberdarkbyte3:34 14 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind5:22 14 Oct '04  
GeneralRe: More flexible and consistent version Pinmemberjjeffery18:27 21 Oct '05  
GeneralRe: More flexible and consistent version Pinmembermav.northwind23:18 21 Oct '05  
GeneralRe: More flexible and consistent version PinmembereisernWolf20:45 16 May '06  
GeneralRe: More flexible and consistent version Pinmemberesskar0:12 25 Nov '07  
GeneralAnother Bug Pinmemberjahawkins6:41 8 Oct '04  
GeneralRe: Another Bug Pinmembermax29506:33 5 Aug '05  
Yes you're 100% right
 


 
using(Graphics g = this.CreateGraphics())//the using directive makes sure the graphics object gets cleaned.
{
int w = (this.Width - g.MeasureString(m_NoItemText, this.Font).ToSize().Width)/2;
if(w > this.Width) w = this.Width;
g.DrawString(m_NoItemText, this.Font, SystemBrushes.ControlText, w, 30);
}
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120529.1 | Last Updated 8 Oct 2004
Article Copyright 2004 by lubos_h
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid