Click here to Skip to main content
6,629,377 members and growing! (20,555 online)
Email Password   helpLost your password?
Desktop Development » List Controls » ListView controls     Intermediate

Limit text entered into a listview edit control

By David Polson

An article on limiting the number of charaters input into a listview control.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:13 Sep 2005
Views:27,384
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.73 Rating: 2.88 out of 5
1 vote, 25.0%
1

2

3
1 vote, 25.0%
4
2 votes, 50.0%
5

Introduction

This article describes how to limit the number of characters that a user can enter into an edit control of a list view control that is set for detail view. By default there is no simple straightforward way to do this, like a property to set.

Using the code

The default ListView control that .NET has doesn't have the ability for you to limit the amount of text than you can enter into the edit control. This is easily solved...

First you have to include a using statement for System.Runtime.InteropServices. This allows you to use the DllImport attribute. Next you can define a call into the Windows API using that attribute.

[DllImport("user32.dll", CharSet=CharSet.Ansi)]
private static extern IntPtr SendMessage(IntPtr hWnd, 
        int msg, int len, IntPtr order);

Add an event for the BeforeLabelEdit to your list view.

private void lvListView_BeforeLabelEdit(object sender, 
             System.Windows.Forms.LabelEditEventArgs e)
{
    //Limit input to six characters maximum on the edit control

    IntPtr editWnd = IntPtr.Zero;
    editWnd = SendMessage(lvListView.Handle, 
                          LVM_GETEDITCONTROL, 0, IntPtr.Zero);
    SendMessage(editWnd, EM_LIMITTEXT, 6, IntPtr.Zero);
}//lvListView_BeforeLabelEdit

Finally add the couple of constants that the above function is using.

private const int EM_LIMITTEXT = 0xC5;
// ListView messages

private const int LVM_FIRST = 0x1000;
private const int LVM_GETEDITCONTROL = (LVM_FIRST + 24);

That's it. This little article I hope helps repay all the programmers that contributed articles that I have used throughout the last few years.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

David Polson


Member

Occupation: Web Developer
Location: United States United States

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 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
QuestionThe downloaded code is not working PinmemberSrinesh1:17 17 Dec '07  
GeneralQuestion Pinmembersilkkeng13:40 13 Sep '05  
GeneralRe: Question PinmemberNowImABeliever10:13 18 Sep '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 13 Sep 2005
Editor: Smitha Vijayan
Copyright 2005 by David Polson
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project