ListView - How to programatically select an item 
Author Message
 ListView - How to programatically select an item

I have a listView control which I am using everything almost OK. However, I
have added code to create a new item in the list which appears at the end
when I click in an area of the control which does not have an Item.

So far so good. . .

Now I want to continue to select the item, and bring it into edit.

I cannot seem to acheive this. I have tried using the select and focused
properties to True but this does not work. I was wondering if I got the
screen co-ordinates from the client ( done that ) that I could somehow force
a click event to occure over the said item,. However, I dont know how to do
this.

Any help will be greatly appreciated.

Thanks - Terry



Tue, 13 Dec 2005 15:21:30 GMT  
 ListView - How to programatically select an item
Terry,

I'm not sure how you are adding the new item in your code so put together a
small code snippet that works for me;

    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListView1.Click

        Dim MyItem As New ListViewItem()

        MyItem.Text = "NewItem"     'Give the new item a default title

        Me.ListView1.Items.Add(MyItem)     'Add it to the list

        MyItem.Selected = True     'Select it

        MyItem.BeginEdit()     'Turn on editing

    End Sub

Make sure you turn off multi select and turn on label edit on the listview
control.

Good luck


Quote:
> I have a listView control which I am using everything almost OK. However,
I
> have added code to create a new item in the list which appears at the end
> when I click in an area of the control which does not have an Item.

> So far so good. . .

> Now I want to continue to select the item, and bring it into edit.

> I cannot seem to acheive this. I have tried using the select and focused
> properties to True but this does not work. I was wondering if I got the
> screen co-ordinates from the client ( done that ) that I could somehow
force
> a click event to occure over the said item,. However, I dont know how to
do
> this.

> Any help will be greatly appreciated.

> Thanks - Terry



Wed, 14 Dec 2005 01:28:37 GMT  
 ListView - How to programatically select an item
Hello and thanks for your reply,

Under the mouse down event this does not work properly, please see the
text below. I cured this problem ( worked around it ) by putting  the
following private variable in the form declaration

Dim HasNewListViewItem as Boolean

Then when I come out of mouse down and into mouse up I get the last item
on the list and select and beginedit.

I am very interested to find out an explaination for this phenomenon.

Please read my detailed explaination below and see if you can make sense
of why this is happening

======   Phenonmenon Description ===========

Lets say I have 10 rows and 3 items.

With the code in the MouseDown event, if I click in rows 5 to 10  the
new
item is added but not selected or focused. If I put my cursor directly
below
the last item ( in row 4 ) on the list then it works.

If I move this code to the MouseUp event instead. I can click in rows 4
to
10 and it allways creates and selects the new item. However, if I then
move
the mouse it fires the mouse up event again ( at least the code runs )
and
generates an additional item. moving the mouse after this does not
create
more items unless we click again and the procedure repeats again.

Many Thanks - Terry

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Wed, 14 Dec 2005 17:29:13 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. jump listview to the hilight item (selected item)...

2. Why does the previous selected item in a ListView get selected for a short moment

3. ListView control - Keeping selected items looking selected

4. Drop-down Listbox - programatically setting the selected item

5. highlight listview item programatically

6. How to Programatically Set the ForeColor of ListView Items

7. Setting Listview item programatically

8. ListView Control - Removing selected items from the list

9. How to do Next item and Previous item programatically

10. List of selected items in a listview

11. Select automatically an item in a ListView

12. How to select all items in a ListView (searching for a faster way)

 

 
Powered by phpBB® Forum Software