Limiting the maximum text length of a ListViewItem?

I have a ListView in a win-form project in C #. Is it possible to limit the maximum length of the title of an entire ListViewItem inside the ListView?

UPDATE

I mean the input length, I am setting the element to editable so users can rename the elements

UPDATE2

That's right, he named the "text" of this element, not the title.

+3


source to share


2 answers


You can use the label after the edit event in the list. Here's an example.

private void listview1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
    try
    {
        const int maxPermittedLength = 1;

        if (e.Label.Length > maxPermittedLength)
        {
            //trim text
            listview1.Items[e.Item].SubItems[0].Text = listview1.Items[e.Item].SubItems[0].Text.Substring(0, maxPermittedLength); //or something similar

            //or

            //show a warning message

            //or

            e.CancelEdit = true; //cancel the edit
        }
    }
    catch (Exception ex)
    {

    }
}

      



Remember, this is difficult, not easy, you will have to take care of a few exceptions, but this is homework. The above code is not working code, but now you have an idea how to do it. Read the documentation well, it has a good example and warning related to this event.

+3


source


What does the name ListViewItem mean? is this the text of the subject you mean? I believe that anything recoverable is fixable and controllable. If it is the text of an element, you can write a validation method

public string SimplifyTxt(string input)
{
    if(input.Length>LIMIT_NUMBER)
    {
       //please shorten the string before display
    }
    return retStr;
}

      



and then it can be assigned as

listview1.items.add(new Listviewitem{Text=retVal});

      

0


source







All Articles