Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Indicating an empty ListView in C#

Rate me:
Please Sign up or sign in to vote.
4.39/5 (10 votes)
7 Oct 2004Public Domain2 min read 139.7K   757   66   29
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.

C#
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.

C#
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

C#
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


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBetter way Pin
ksledziewski20-Jan-11 1:50
ksledziewski20-Jan-11 1:50 
GeneralText overlap when scroll bar moved Pin
scyeoh10-Dec-07 4:33
scyeoh10-Dec-07 4:33 
Generalbug - scrolling Pin
Darchangel25-Feb-06 9:01
Darchangel25-Feb-06 9:01 
GeneralProblems with LVS_EX_DOUBLEBUFFER Pin
Anonymous7-Aug-05 22:28
Anonymous7-Aug-05 22:28 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
Uwe Keim29-Jun-06 22:16
sitebuilderUwe Keim29-Jun-06 22:16 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
Uwe Keim29-Jun-06 22:41
sitebuilderUwe Keim29-Jun-06 22:41 
GeneralRe: Problems with LVS_EX_DOUBLEBUFFER Pin
JeffTz29-Oct-09 2:41
JeffTz29-Oct-09 2:41 
General.NET 2.0 Pin
compchris9-Dec-04 1:32
compchris9-Dec-04 1:32 
GeneralRe: .NET 2.0 Pin
Anonymous27-Jul-05 9:14
Anonymous27-Jul-05 9:14 
GeneralMessage not scrolling Pin
gigatoad8-Nov-04 10:11
gigatoad8-Nov-04 10:11 
Generalscroll Pin
mkuzma15-Oct-04 2:34
mkuzma15-Oct-04 2:34 
GeneralMore flexible and consistent version Pin
mav.northwind10-Oct-04 2:51
mav.northwind10-Oct-04 2:51 
GeneralRe: More flexible and consistent version Pin
lubos_h11-Oct-04 2:55
lubos_h11-Oct-04 2:55 
GeneralRe: More flexible and consistent version Pin
mav.northwind11-Oct-04 3:05
mav.northwind11-Oct-04 3:05 
GeneralRe: More flexible and consistent version Pin
lubos_h11-Oct-04 14:07
lubos_h11-Oct-04 14:07 
GeneralRe: More flexible and consistent version Pin
mav.northwind11-Oct-04 20:46
mav.northwind11-Oct-04 20:46 
GeneralRe: More flexible and consistent version Pin
Laudeci Oliveira13-Oct-04 4:20
Laudeci Oliveira13-Oct-04 4:20 
GeneralRe: More flexible and consistent version Pin
darkbyte14-Oct-04 3:34
darkbyte14-Oct-04 3:34 
GeneralRe: More flexible and consistent version Pin
mav.northwind14-Oct-04 5:22
mav.northwind14-Oct-04 5:22 
GeneralRe: More flexible and consistent version Pin
jjeffery21-Oct-05 18:27
jjeffery21-Oct-05 18:27 
GeneralRe: More flexible and consistent version Pin
mav.northwind21-Oct-05 23:18
mav.northwind21-Oct-05 23:18 
GeneralRe: More flexible and consistent version Pin
eisernWolf16-May-06 20:45
eisernWolf16-May-06 20:45 
GeneralRe: More flexible and consistent version Pin
esskar25-Nov-07 0:12
esskar25-Nov-07 0:12 
GeneralAnother Bug Pin
jahawkins8-Oct-04 6:41
jahawkins8-Oct-04 6:41 
GeneralRe: Another Bug Pin
max29505-Aug-05 6:33
max29505-Aug-05 6:33 

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.