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

Limit text entered into a listview edit control

Rate me:
Please Sign up or sign in to vote.
2.88/5 (4 votes)
13 Sep 2005 47.6K   217   21   3
An article on limiting the number of charaters input into a listview control.

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.

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

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

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


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

Comments and Discussions

 
QuestionThe downloaded code is not working Pin
Srinesh17-Dec-07 0:17
Srinesh17-Dec-07 0:17 
GeneralQuestion Pin
silkkeng13-Sep-05 12:40
silkkeng13-Sep-05 12:40 
GeneralRe: Question Pin
NowImABeliever18-Sep-05 9:13
NowImABeliever18-Sep-05 9:13 
The best way to limit the text that you input is probably using a Regular expression. Add a using statement to your code like

Using System.Text.RegularExpressions;

Then add some code to your after label edit event similar to this

Regex reg = new Regex(@"^[0-9]+$");

if(!reg.IsMatch(e.Label))
e.CancelEdit = true;
else
{
//update column three
}

The code above limits the input to numeric only. You can just alter the expression as you see fit. If you are still having trouble getting the column to update right away you might try adding a call in the else part of the code above to ListView.Update() to force the listview control to redraw itself.

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.