Click here to Skip to main content
11,413,676 members (76,728 online)
Click here to Skip to main content

Indicating an empty ListView in C#

, 8 Oct 2004 Public Domain
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

lubos_h

Australia Australia
No Biography provided

Comments and Discussions

 
GeneralBetter way Pin
ksledziewski, 20-Jan-11 2:50
memberksledziewski20-Jan-11 2:50 
GeneralText overlap when scroll bar moved Pin
scyeoh, 10-Dec-07 5:33
memberscyeoh10-Dec-07 5:33 
Generalbug - scrolling Pin
Darchangel, 25-Feb-06 10:01
memberDarchangel25-Feb-06 10:01 
GeneralProblems with LVS_EX_DOUBLEBUFFER Pin
Anonymous, 7-Aug-05 23:28
sussAnonymous7-Aug-05 23:28 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
Uwe Keim, 29-Jun-06 23:16
sitebuilderUwe Keim29-Jun-06 23:16 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
Uwe Keim, 29-Jun-06 23:41
sitebuilderUwe Keim29-Jun-06 23:41 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
JeffTz, 29-Oct-09 3:41
memberJeffTz29-Oct-09 3:41 
General.NET 2.0 Pin
compchris, 9-Dec-04 2:32
membercompchris9-Dec-04 2:32 
GeneralRe: .NET 2.0 Pin
Anonymous, 27-Jul-05 10:14
sussAnonymous27-Jul-05 10:14 
GeneralMessage not scrolling Pin
Brenton House, 8-Nov-04 11:11
sussBrenton House8-Nov-04 11:11 
Generalscroll Pin
mkuzma, 15-Oct-04 3:34
membermkuzma15-Oct-04 3:34 
GeneralMore flexible and consistent version Pin
mav.northwind, 10-Oct-04 3:51
membermav.northwind10-Oct-04 3:51 
GeneralRe: More flexible and consistent version Pin
Lubos Hasko, 11-Oct-04 3:55
memberLubos Hasko11-Oct-04 3:55 
GeneralRe: More flexible and consistent version Pin
mav.northwind, 11-Oct-04 4:05
membermav.northwind11-Oct-04 4:05 
GeneralRe: More flexible and consistent version Pin
Lubos Hasko, 11-Oct-04 15:07
memberLubos Hasko11-Oct-04 15:07 
GeneralRe: More flexible and consistent version Pin
mav.northwind, 11-Oct-04 21:46
membermav.northwind11-Oct-04 21:46 
GeneralRe: More flexible and consistent version Pin
Laudeci Oliveira, 13-Oct-04 5:20
memberLaudeci Oliveira13-Oct-04 5:20 
GeneralRe: More flexible and consistent version Pin
darkbyte, 14-Oct-04 4:34
memberdarkbyte14-Oct-04 4:34 
GeneralRe: More flexible and consistent version Pin
mav.northwind, 14-Oct-04 6:22
membermav.northwind14-Oct-04 6:22 
GeneralRe: More flexible and consistent version Pin
jjeffery, 21-Oct-05 19:27
memberjjeffery21-Oct-05 19:27 
GeneralRe: More flexible and consistent version Pin
mav.northwind, 22-Oct-05 0:18
membermav.northwind22-Oct-05 0:18 
GeneralRe: More flexible and consistent version Pin
eisernWolf, 16-May-06 21:45
membereisernWolf16-May-06 21:45 
GeneralRe: More flexible and consistent version Pin
esskar, 25-Nov-07 1:12
memberesskar25-Nov-07 1:12 
GeneralAnother Bug Pin
jahawkins, 8-Oct-04 7:41
memberjahawkins8-Oct-04 7:41 
GeneralRe: Another Bug Pin
max2950, 5-Aug-05 7:33
membermax29505-Aug-05 7:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.2 | Last Updated 8 Oct 2004
Article Copyright 2004 by lubos_h
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid