Click here to Skip to main content
6,630,901 members and growing! (20,802 online)
Email Password   helpLost your password?
Desktop Development » List Controls » ListView controls     Intermediate License: A Public Domain dedication

Indicating an empty ListView in C#

By lubos_h

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?
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:8 Oct 2004
Views:74,894
Bookmarked:49 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.87 Rating: 4.05 out of 5
1 vote, 11.1%
1
1 vote, 11.1%
2
1 vote, 11.1%
3
3 votes, 33.3%
4
3 votes, 33.3%
5

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


Member

Location: Australia Australia

Other popular List Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 28 (Total in Forum: 28) (Refresh)FirstPrevNext
GeneralText overlap when scroll bar moved Pinmemberscyeoh5:33 10 Dec '07  
Generalbug - scrolling PinmemberDarchangel10:01 25 Feb '06  
GeneralProblems with LVS_EX_DOUBLEBUFFER PinsussAnonymous23:28 7 Aug '05  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinsitebuilderUwe Keim23:16 29 Jun '06  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinsitebuilderUwe Keim23:41 29 Jun '06  
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER PinmemberJeffTz3:41 29 Oct '09  
General.NET 2.0 Pinmembercompchris2:32 9 Dec '04  
GeneralRe: .NET 2.0 PinsussAnonymous10:14 27 Jul '05  
GeneralMessage not scrolling PinsussBrenton House11:11 8 Nov '04  
Generalscroll Pinmembermkuzma3:34 15 Oct '04  
GeneralMore flexible and consistent version Pinmembermav.northwind3:51 10 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLubos Hasko3:55 11 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind4:05 11 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLubos Hasko15:07 11 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind21:46 11 Oct '04  
GeneralRe: More flexible and consistent version PinmemberLaudeci Oliveira5:20 13 Oct '04  
GeneralRe: More flexible and consistent version Pinmemberdarkbyte4:34 14 Oct '04  
GeneralRe: More flexible and consistent version Pinmembermav.northwind6:22 14 Oct '04  
GeneralRe: More flexible and consistent version Pinmemberjjeffery19:27 21 Oct '05  
GeneralRe: More flexible and consistent version Pinmembermav.northwind0:18 22 Oct '05  
GeneralRe: More flexible and consistent version PinmembereisernWolf21:45 16 May '06  
GeneralRe: More flexible and consistent version Pinmemberesskar1:12 25 Nov '07  
GeneralAnother Bug Pinmemberjahawkins7:41 8 Oct '04  
GeneralRe: Another Bug Pinmembermax29507:33 5 Aug '05  
Generalbug PinmemberGoAn1:42 8 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Oct 2004
Editor: Nishant Sivakumar
Copyright 2004 by lubos_h
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project