Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ListView Label Edit - control selected text

I am looking at the .Net Framework ListView (I've been trying BetterListView, the express version, and keep running up against things I can't do, so I figure I might as well run up against things I can't do in the better documented MS ListView!) and there's something that's stumping me.

I'd like the items to be editable, but I would like, when editing starts, for the selected text to be only part of the item text rather than all of it.

An example of this would be in Windows Explorer, when you have file extensions visible, and you start to rename a file - the file name is selected (blue background) but the file extension is not, such that if the user starts to type immediately then the name will be replaced but the extension stays as is.

I can think of workarounds, but wondered if there's any way to do that.

like image 561
James Carlyle-Clarke Avatar asked Dec 18 '12 10:12

James Carlyle-Clarke


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

One long trawl through the appropriate messages, and the answer is...

private void listView1_BeforeLabelEdit(object sender, LabelEditEventArgs e)
{
    IntPtr editWnd = IntPtr.Zero;
    editWnd = SendMessage(listView1.Handle, LVM_GETEDITCONTROL, 0, IntPtr.Zero);
    int textLen = Path.GetFileNameWithoutExtension(listView1.Items[e.Item].Text).Length;
    SendMessage(editWnd, EM_SETSEL, 0, (IntPtr) textLen);
}

public const int EM_SETSEL = 0xB1;
public const int LVM_FIRST = 0x1000;
public const int LVM_GETEDITCONTROL = (LVM_FIRST + 24);

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

That does exactly what I was after. Thanks for the other responders taking the time to answer.

like image 150
James Carlyle-Clarke Avatar answered Sep 20 '22 06:09

James Carlyle-Clarke