Selected item, Help needed

Trembru

New member
Joined
Oct 19, 2005
Messages
4
Programming Experience
Beginner
I am building a very basic file manager as part of another App. It has a treeview type control (ExpTree) and a listview. It has an Add button which when clicked, copies the selected item to a special folder. My problem is that I want to be able to select items from either the treeview or the listview. I have written code to select the item for both controls (works well), but how do I determine which control was clicked?. I have considered using a function to return the last of the two controls to have focus, but is this the best way ? I would appreciate some advice from those more experienced than me (that's probably all of you). Thanks in advance.
 
Look i'm not quite sure that i understand your question but i will try to guess for a while :D

For the ListView control - add these lines to the selectedIndexChanged event:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListView1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListView1.SelectedIndexChanged
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] lvitem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] ListView1.SelectedItems
MessageBox.Show(lvitem.SubItems(0).Text) [/SIZE][SIZE=2][COLOR=#008000]'will let you know which item has been selected
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("User has selected ListView Control") [/SIZE][SIZE=2][COLOR=#008000]'will notify you that user has selected listview rather but treeview
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

For the TreeView control - add these lines to the AfterSelect event:
VB.NET:
[COLOR=#0000ff][SIZE=2][/SIZE][/COLOR][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TreeView1_AfterSelect([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.TreeViewEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TreeView1.AfterSelect
MessageBox.Show(TreeView1.SelectedNode.Text) [/SIZE][SIZE=2][COLOR=#008000]'will let you know which node has been selected
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("User has selected TreeView Control") [/SIZE][SIZE=2][COLOR=#008000]'will notify you that user has selected treeview rather but listview
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

HTH
Regards ;)
 
Hi Kulrom, Thanks for the reply. On the form I have an 'add' button, which when clicked, copies the selected item in either the treeview or listview, to a special App folder. I have written code for both the treeview and listview selected item events but I need to add code such as an IF Treeview ELSEIF listview clicked, to decide which version of selected item code is run. I had thought of using a function to determine which (treeview or listview) had the focus last but I feel there must be a better way.
Regards
 
Thanks Kulrom, I have solved my problem by using the event handlers you suggested to set the value of a boolean variable which I then used in an If-Then-ElsIf .
Many thanks for the inspiration.
 
Kulrom, thanks for this! It helped me out a great deal with a similar problem I'm working on!

Look i'm not quite sure that i understand your question but i will try to guess for a while :D

For the ListView control - add these lines to the selectedIndexChanged event:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListView1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListView1.SelectedIndexChanged
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] lvitem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] ListView1.SelectedItems
MessageBox.Show(lvitem.SubItems(0).Text) [/SIZE][SIZE=2][COLOR=#008000]'will let you know which item has been selected
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("User has selected ListView Control") [/SIZE][SIZE=2][COLOR=#008000]'will notify you that user has selected listview rather but treeview
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

For the TreeView control - add these lines to the AfterSelect event:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TreeView1_AfterSelect([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.TreeViewEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TreeView1.AfterSelect
MessageBox.Show(TreeView1.SelectedNode.Text) [/SIZE][SIZE=2][COLOR=#008000]'will let you know which node has been selected
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("User has selected TreeView Control") [/SIZE][SIZE=2][COLOR=#008000]'will notify you that user has selected treeview rather but listview
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

HTH
Regards ;)
 
Back
Top