473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ListView changing size

Hi all,

I'm looking to determine when the size of a Column is changed in a ListView
in detail view. Is there an event i can wire up to?

Thanks,
Michael C., MCDBA
Nov 16 '05 #1
5 6679
> I'm looking to determine when the size of a Column is changed in a
ListView
in detail view. Is there an event i can wire up to?


not directly.

you should create a class that inherits from the ListView and override its
WndProc. inside the WndProc you should catch incoming WM_NOTIFY messages and
check the value of the "code" field in the NMHDR structure. if you get
HDN_BEGINTRACKA or HDN_BEGINTRACKW then you know that the column is beeing
resized by the user. you can even block changes if you need to.

above solution assumes that you are familiar with Win32 stuff - WndProc and
messages. if you have any problems, please send another message to the
newsgroup and I will respond with the actual c# code.

regards,
Wiktor Zychla
Nov 16 '05 #2
Hi Wiktor,

I'm a *little* familiar with Win32 API programming in .NET. Mostly network
and database related stuff though. I'd love to see the C# code if you have
it available. Specifically I'd like to throw an event when a column is
re-sized so that I can capture the column width and save it for later use.

Thanks,
Michael C., MCDBA

"Wiktor Zychla" <us**@nospam.com.eu> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
I'm looking to determine when the size of a Column is changed in a ListView
in detail view. Is there an event i can wire up to?


not directly.

you should create a class that inherits from the ListView and override its
WndProc. inside the WndProc you should catch incoming WM_NOTIFY messages

and check the value of the "code" field in the NMHDR structure. if you get
HDN_BEGINTRACKA or HDN_BEGINTRACKW then you know that the column is beeing
resized by the user. you can even block changes if you need to.

above solution assumes that you are familiar with Win32 stuff - WndProc and messages. if you have any problems, please send another message to the
newsgroup and I will respond with the actual c# code.

regards,
Wiktor Zychla

Nov 16 '05 #3
> it available. Specifically I'd like to throw an event when a column is
re-sized so that I can capture the column width and save it for later use.


below is an extract from my actual code. as I've said, you can stick to
BeginTrack and EndTrack events. two collections, cNoResize and cNoDrag, can
contain numbers of columns that you do not want the user to resize or drag.

the code will not compile immediately because of attached win32 specific
consts and structs at the end that you have to put in another class. note
also that this is unsafe code.

put a note in the newsgroup if you still have problems. put a note also if
you succeed.

Regards
Wiktor Zychla

namespace Controls
{
public class ListView_0Class : System.Windows.Forms.ListView
{

public delegate void ListViewHeaderNotifEventHandler( object sender,
ListViewHeaderNotifEventArgs e );

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginTrack;

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndTrack;

public ArrayList cNoResize = new ArrayList();
public ArrayList cNoDrag = new ArrayList();

public ListView_0Class() {}

protected override void OnPaintBackground( PaintEventArgs e )
{
}

unsafe protected override void WndProc( ref Message m )
{
HDITEM hditem;
NMHDR nm;
NMHEADER nmheader;

switch ( m.Msg )
{
case (int)Msg.WM_NOTIFY:

nm = (NMHDR) m.GetLParam(typeof(NMHDR));
switch(nm.code)
{
case (int)HeaderControlNotifications.HDN_BEGINTRACKA:
case (int)HeaderControlNotifications.HDN_BEGINTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoResize.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginTrack != null )
BeginTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_BEGINDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoDrag.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginDrag != null )
BeginDrag( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDTRACKA:
case (int)HeaderControlNotifications.HDN_ENDTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( EndTrack != null )
EndTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
hditem = *((HDITEM*)nmheader.pItem.ToPointer());

if ( cNoDrag.Contains( hditem.iOrder ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( EndDrag != null )
EndDrag( this, new ListViewHeaderNotifEventArgs( nmheader.iItem ) );
break;

default:
break;
}
break;
}
base.WndProc( ref m );
} // end void WndProc

} // end class

public class ListViewHeaderNotifEventArgs : EventArgs
{
public ListViewHeaderNotifEventArgs( int iColumn )
{
this.iColumn = iColumn;
}
public int iColumn;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr hwndFrom;
public int idFrom;
public int code;
}
#endregion

// NMHEADER
#region
[StructLayout(LayoutKind.Sequential)]
public struct NMHEADER
{
public NMHDR hdr;
public int iItem;
public int iButton;
public IntPtr pItem;
}
#endregion

// HDITEM
#region
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct HDITEM
{
public uint mask;
public int cxy;
public IntPtr pszText;
public IntPtr hbm;
public int cchTextMax;
public int fmt;
public int lParam;
public int iImage;
public int iOrder;
}
#endregion

// Header Control Notifications
#region
public enum HeaderControlNotifications
{
HDN_FIRST = (0-300),
HDN_BEGINTRACKA = (HDN_FIRST-6),
HDN_ENDTRACKA = (HDN_FIRST-7),
HDN_BEGINTRACKW = (HDN_FIRST-26),
HDN_ENDTRACKW = (HDN_FIRST-27),
HDN_ITEMCLICKW = (HDN_FIRST-22),
HDN_BEGINDRAG = (HDN_FIRST-10),
HDN_ENDDRAG = (HDN_FIRST-11),
}
#endregion
Nov 16 '05 #4
Thanks Wiktor! Excellent code, it works great! The only thing I had to
change was adding a const definition for WM_NOTIFY. Other than that, it's
very efficient and works a lot better than my previous solution!

Thanks!

Michael C., MCDBA

"Wiktor Zychla" <us**@nospam.com.eu> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...
it available. Specifically I'd like to throw an event when a column is
re-sized so that I can capture the column width and save it for later use.
below is an extract from my actual code. as I've said, you can stick to
BeginTrack and EndTrack events. two collections, cNoResize and cNoDrag,

can contain numbers of columns that you do not want the user to resize or drag.
the code will not compile immediately because of attached win32 specific
consts and structs at the end that you have to put in another class. note
also that this is unsafe code.

put a note in the newsgroup if you still have problems. put a note also if
you succeed.

Regards
Wiktor Zychla

namespace Controls
{
public class ListView_0Class : System.Windows.Forms.ListView
{

public delegate void ListViewHeaderNotifEventHandler( object sender,
ListViewHeaderNotifEventArgs e );

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginTrack;

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndTrack;

public ArrayList cNoResize = new ArrayList();
public ArrayList cNoDrag = new ArrayList();

public ListView_0Class() {}

protected override void OnPaintBackground( PaintEventArgs e )
{
}

unsafe protected override void WndProc( ref Message m )
{
HDITEM hditem;
NMHDR nm;
NMHEADER nmheader;

switch ( m.Msg )
{
case (int)Msg.WM_NOTIFY:

nm = (NMHDR) m.GetLParam(typeof(NMHDR));
switch(nm.code)
{
case (int)HeaderControlNotifications.HDN_BEGINTRACKA:
case (int)HeaderControlNotifications.HDN_BEGINTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoResize.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginTrack != null )
BeginTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_BEGINDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoDrag.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginDrag != null )
BeginDrag( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDTRACKA:
case (int)HeaderControlNotifications.HDN_ENDTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( EndTrack != null )
EndTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
hditem = *((HDITEM*)nmheader.pItem.ToPointer());

if ( cNoDrag.Contains( hditem.iOrder ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( EndDrag != null )
EndDrag( this, new ListViewHeaderNotifEventArgs( nmheader.iItem ) ); break;

default:
break;
}
break;
}
base.WndProc( ref m );
} // end void WndProc

} // end class

public class ListViewHeaderNotifEventArgs : EventArgs
{
public ListViewHeaderNotifEventArgs( int iColumn )
{
this.iColumn = iColumn;
}
public int iColumn;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr hwndFrom;
public int idFrom;
public int code;
}
#endregion

// NMHEADER
#region
[StructLayout(LayoutKind.Sequential)]
public struct NMHEADER
{
public NMHDR hdr;
public int iItem;
public int iButton;
public IntPtr pItem;
}
#endregion

// HDITEM
#region
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct HDITEM
{
public uint mask;
public int cxy;
public IntPtr pszText;
public IntPtr hbm;
public int cchTextMax;
public int fmt;
public int lParam;
public int iImage;
public int iOrder;
}
#endregion

// Header Control Notifications
#region
public enum HeaderControlNotifications
{
HDN_FIRST = (0-300),
HDN_BEGINTRACKA = (HDN_FIRST-6),
HDN_ENDTRACKA = (HDN_FIRST-7),
HDN_BEGINTRACKW = (HDN_FIRST-26),
HDN_ENDTRACKW = (HDN_FIRST-27),
HDN_ITEMCLICKW = (HDN_FIRST-22),
HDN_BEGINDRAG = (HDN_FIRST-10),
HDN_ENDDRAG = (HDN_FIRST-11),
}
#endregion

Nov 16 '05 #5
I am interested in using the code that you wrote in the previous post;
however, I think there is a problem. The line
case (int)Msg.WM_NOTIFY:

in the WndProc override won't compile. What is Msg?

Thanks for your help.
Pat

Wiktor Zychla wrote:
it available. Specifically I'd like to throw an event when a column is re-sized so that I can capture the column width and save it for later use.
below is an extract from my actual code. as I've said, you can stick

to BeginTrack and EndTrack events. two collections, cNoResize and cNoDrag, can contain numbers of columns that you do not want the user to resize or drag.
the code will not compile immediately because of attached win32 specific consts and structs at the end that you have to put in another class. note also that this is unsafe code.

put a note in the newsgroup if you still have problems. put a note also if you succeed.

Regards
Wiktor Zychla

namespace Controls
{
public class ListView_0Class : System.Windows.Forms.ListView
{

public delegate void ListViewHeaderNotifEventHandler( object sender, ListViewHeaderNotifEventArgs e );

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler BeginTrack;

[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndDrag;
[Category("Behavior")]
public event ListViewHeaderNotifEventHandler EndTrack;

public ArrayList cNoResize = new ArrayList();
public ArrayList cNoDrag = new ArrayList();

public ListView_0Class() {}

protected override void OnPaintBackground( PaintEventArgs e )
{
}

unsafe protected override void WndProc( ref Message m )
{
HDITEM hditem;
NMHDR nm;
NMHEADER nmheader;

switch ( m.Msg )
{
case (int)Msg.WM_NOTIFY:

nm = (NMHDR) m.GetLParam(typeof(NMHDR));
switch(nm.code)
{
case (int)HeaderControlNotifications.HDN_BEGINTRACKA:
case (int)HeaderControlNotifications.HDN_BEGINTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoResize.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginTrack != null )
BeginTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_BEGINDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( cNoDrag.Contains( nmheader.iItem ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( BeginDrag != null )
BeginDrag( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDTRACKA:
case (int)HeaderControlNotifications.HDN_ENDTRACKW:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
if ( EndTrack != null )
EndTrack( this, new ListViewHeaderNotifEventArgs(
nmheader.iItem ) );
break;

case (int)HeaderControlNotifications.HDN_ENDDRAG:

nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER));
hditem = *((HDITEM*)nmheader.pItem.ToPointer());

if ( cNoDrag.Contains( hditem.iOrder ) )
{
m.Result = (IntPtr)Convert.ToInt32(true);
return;
}
else if ( EndDrag != null )
EndDrag( this, new ListViewHeaderNotifEventArgs( nmheader.iItem ) ); break;

default:
break;
}
break;
}
base.WndProc( ref m );
} // end void WndProc

} // end class

public class ListViewHeaderNotifEventArgs : EventArgs
{
public ListViewHeaderNotifEventArgs( int iColumn )
{
this.iColumn = iColumn;
}
public int iColumn;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr hwndFrom;
public int idFrom;
public int code;
}
#endregion

// NMHEADER
#region
[StructLayout(LayoutKind.Sequential)]
public struct NMHEADER
{
public NMHDR hdr;
public int iItem;
public int iButton;
public IntPtr pItem;
}
#endregion

// HDITEM
#region
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct HDITEM
{
public uint mask;
public int cxy;
public IntPtr pszText;
public IntPtr hbm;
public int cchTextMax;
public int fmt;
public int lParam;
public int iImage;
public int iOrder;
}
#endregion

// Header Control Notifications
#region
public enum HeaderControlNotifications
{
HDN_FIRST = (0-300),
HDN_BEGINTRACKA = (HDN_FIRST-6),
HDN_ENDTRACKA = (HDN_FIRST-7),
HDN_BEGINTRACKW = (HDN_FIRST-26),
HDN_ENDTRACKW = (HDN_FIRST-27),
HDN_ITEMCLICKW = (HDN_FIRST-22),
HDN_BEGINDRAG = (HDN_FIRST-10),
HDN_ENDDRAG = (HDN_FIRST-11),
}
#endregion


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
0
by: Martin Streller | last post by:
Hello, The code below represents a simple ownerdrawn, Listview class in C#. Its purpose is to avoid the flicker of the MS ListView. So I can't fall back to their one. Does anybody know why I...
6
by: Vanessa | last post by:
With this program I can do one selection, but upon the second I get an error where ///////////////// is indicated. Please help. using System; using System.Drawing; using System.Collections;...
4
by: Paddy | last post by:
How do I select a subitem from a listview after clicking on the first column? Couldn't find it in MSDN. Thank you. Paddy.
0
by: willow1480 | last post by:
I am developing a small little Service Control Application. I am using a listview control with checkboxes and getting the list of services I want to control from a text file. When you check a...
0
by: Andrew | last post by:
If item is a ListViewItem and str is a string, why do the following two lines not have the same effect ? item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text = str;...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
12
by: J L | last post by:
When I fill a listview, I resize the columns to fit the data. I need to know if the data will fit vertically or if there will be a vertical scroll bar. I need to know this so I can allow for it on...
1
by: lewisjosh2 | last post by:
I'm a newbie to windows forms and I'm having trouble finding any information about how to create a custom listview. In my app, I'm using a 'large icon' view and basically what I want to do is: ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.