C# Listview Setting Icon Spacing


  • I have seen a coouple of articles on changing the Icon spacing in alist view but none of them seem to work.


    for example when I invoke:

      ListView_SetIconSpacing(listView1, 48, 48 );  // icon sizes of 47x47 with one pixel spacing on 

                                                                         // listview: listView1


    I get an error in the compiler:


    Error 3 The name 'ListView_SetIconSpacing' does not exist in the current context F:\projects\VS_Projects\Large Screen DeviceApplication1\DeviceApplication1\Form1.cs 148 17 Large Screen DeviceApplication1

    So I guess the question I have is what do I need to include in a using directive to put the call for 'ListView_SetIconSpacing'  into the current context?



    There is another call I found named: 

    LVM_SETICONSPACING(this.listView1, 48, 48);


    I'm not sure what I need to do to make this call either..












    Thursday, May 15, 2008 2:51 PM

All replies

  • I don't think you are looking at the C# listview in the examples you identify. There is no SetIconSpacing method. If there was, it would be dot notation, not underscore:  myListview.SetIconSpacing(...


    Thursday, May 15, 2008 3:02 PM

    The article I found is at


    And it lays out the function call as...


    DWORD ListView_SetIconSpacing(
      HWND hwndLV, 
      int cx, 
      int cy 
    It seems to me that it is not part of the normal listview control as you have to pass in a pointer to the listview you want changed as a parameter.
    Hopefully this is not an old function that is no longer supported.
    Thursday, May 15, 2008 4:39 PM

    Hi Will_Richards34TB,

    You can try to P/Invoke the SendMessage to send the LVM_SETICONSPACING message to set the spacing between icons in ListView controls.

    Code Snippet

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

            private static extern Int32 SendMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, Int32 lParam);


            const int LVM_FIRST = 0x1000;

            const int LVM_SETICONSPACING = LVM_FIRST + 53;


            public void SetSpacing(Int16 x, Int16 y)


                SendMessage(this.listView1.Handle, LVM_SETICONSPACING, 0, x * 65536 + y);




    For more information, please check the following link:

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Tuesday, May 20, 2008 10:06 AM

    Ok I have attempted the code you suggested but I get an error when I compile. The error points to the DLLImport message but I'm not sure how to implement that (Never seen it before)

    So where can I read up on it and what does it do?


    Is there a refrence that i need to include to make it functional?





    Tuesday, June 03, 2008 1:37 PM
  • Put "using System.Runtime.InteropServices" at the top of your source code file.  Without the quotes.

    ListView_SetIconSpacing() is only valid in C++ code.  Repeating my earlier request: post .NET compact framework questions to the Smart Devices forum.
    Wednesday, June 04, 2008 2:51 AM
  • >>  using System.Runtime.InteropServices;

    Yeah I had that Statement in my Code already, so thats not it...



    >>"ListView_SetIconSpacing() is only valid in C++ code"


       So I can't do this in C#???   


    What if I put "ListView_SetIconSpacing()" in a DLL that I write via C++ and then call it Via C#

    I'd hate to have to recode everything simply because of an incompatability between .net environments.


    Why is it not compatable with C#?

    how about VB will it work there or not?


    Wednesday, June 04, 2008 1:27 PM
  • It's just a macro for SendMessage().  You're doing something wrong if you can't P/Invoke SendMessage from C#.  Can't tell what from your posts, you haven't even given us an error message.
    Wednesday, June 04, 2008 2:31 PM

    Well ok the error message is…..


    System.MissingMethodException was unhandled

      Message="Can't find PInvoke DLL 'user32.dll'."


        at DeviceApplication1.Form1..ctor()

        at DeviceApplication1.Program.Main()




    The code looks like this….


    using System.Runtime.InteropServices;

    using System;

    using System.Threading;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Drawing.Imaging;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

    using System.Xml;

    using Microsoft.WindowsCE.Forms;

    using Microsoft.WindowsCE;


    namespace DeviceApplication1


        public partial class Form1 : Form



            // private const int LVM_SETVIEW = 0x108E;

            private const int LVM_FIRST = 0x1000;

            private const int LVM_SETICONSPACING = LVM_FIRST + 53;


            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


            public Form1()


               int I = (48 * 65536) + 48;  // the icon spacing

    // set up the windws environment built and called by VS


                //Addjust the ListViewtiles

               SendMessage(this.listView1.Handle, LVM_SETICONSPACING, 0, I);





    Wednesday, June 04, 2008 2:51 PM
  • Again, this is a compact device hangup.  I think the DLL is coredll.dll, not sure.  Repeating myself for the 3rd time, do yourself and us a favor and post to the Smart Devices forum.
    Wednesday, June 04, 2008 4:14 PM
  • I'm sorry I originaly posted this as I thought it was more a C# related problem.

    I have seen where moderators have moved threads to other forums,

    But I do not see that option here...

       Maybe you can do that for me.


    Wednesday, June 04, 2008 5:12 PM

    >>Again, this is a compact device hangup.  I think the DLL is coredll.dll, not sure.


    Ok so what do you mean?  Should I include coredll.dll?


    Wednesday, June 04, 2008 5:43 PM

    So now my question turn to am I calling the correct procedure?


     What I mean is I have set the Value of LVM_SETICONSPACING  equal to LVM_FIRST + 53;

    is that the value for that procedure? or am I using the wrong value?


    const int LVM_FIRST = 0x1000;

    const int LVM_SETICONSPACING = LVM_FIRST + 53;





    Tuesday, June 10, 2008 1:12 PM