ListView with CheckBoxes checks unwanted checkboxes

I'm using C# 2.0, and have a problem with a listview. I've turned on CheckBoxes and have my View set to List. The problem that I'm seeing is that the spacebar always checks an item, even if it's part of another items name. For example, if you create a new form, and drop a ListView called listview1 onto it, then put this code into the form Load event:

private void Form1_Load(object sender, EventArgs e)
{
listView1.CheckBoxes = true;
listView1.View = View.List;

listView1.Items.Add("Apple");
listView1.Items.Add("Banana");
listView1.Items.Add("Bananas");
listView1.Items.Add("Cat");
listView1.Items.Add("Cat Again");
listView1.Items.Add("Cat Once Again");
listView1.Items.Add("Cat Once More");
listView1.Items.Add("Catamount");
listView1.Items.Add("Dog");

listView1.Sorting = SortOrder.Ascending;
}

Type in "Cat Once More". You'll notice that "Cat" and "Cat Once Again" both get checked. You can override KeyDown, and set it to handled if it is the spacebar, but I would like to be able to check something with the space bar, just not if the user is in the middle of typing something. Is it possible to access the internal timer and typed text so I can tell if the user is typing something, or just pressing the space bar

Any suggestions would be welcome. Thanks.


Answer this question

ListView with CheckBoxes checks unwanted checkboxes

  • jayneag

    Hi,

    were you able to solve your problem

    Thank you,
    Bhanu.



  • Sarah Brian

    Well, I didn't spend too much time on this, but you could do something like this:

    class Class1 : ListView

    {

    DateTime lastKeyPress;

    public Class1()

    : base()

    {

    lastKeyPress = DateTime.Now.Subtract(new TimeSpan(1, 0, 0));

    }

    protected override void OnKeyPress(KeyPressEventArgs e)

    {

    TimeSpan timePassed = DateTime.Now.Subtract(lastKeyPress);

    if (timePassed.TotalMilliseconds <= 500)

    {

    if (e.KeyChar == ' ')

    {

    this.Items[this.SelectedItems[0].Index].Checked = false;

    }

    } else {

    if (!(e.KeyChar == ' '))

    {

    lastKeyPress = DateTime.Now;

    }

    }

    base.OnKeyPress(e);

    }

    }

    Basically, make your own timer and uncheck the selected item if there is not enough time between the space and the last key press.


  • JDev

    Bhanu,

    Not as of yet. I've been unable to find the internal text or timer, which means I'll have to just do everything manually, recreating some of the functionality. I haven't had the time to play with it. Hopefully I'll be able to do so coming up, and I'll post whatever I come up with. Right now the best way seems to be adding a timer, and a string, keeping track of what was typed, and overriding the keydown event and setting it to handled if the key is the space bar and there is an item that has a space after the text in your string in the listview that you are working with.

  • bdg

    I had considered doing a timer or something similar to what you suggested, but the problem that I see is that if you want to check Banana, you should be able to just type "Banana" and hit the spacebar. I could take care of that by keeping a string around and appending letters to it, but then I have to handle special keys like ctrl+v and such. Basically, I could get it to work by rewriting the control, but I would be likely to miss something, and remove functionality that it already has. This is probably how I will have to do it, unless I can somehow access the internals of the control.

  • ListView with CheckBoxes checks unwanted checkboxes