ListView VS TreeView

  • Thread starter Thread starter alan
  • Start date Start date
A

alan

Hello,

Can ListView have some event just like the event BeforeSelect of
TreeView?
When a user try to select another item in the ListView, I would like to
do some checking first. If the result is true, then prevent the current item
jump to another one. In TreeView, I can set e.Cancel = true in the event
BeforeSelect. However, can I do similar thing in ListView?
Thanks.
 
Hi alan,

Thanks for posting in this group.
As a workaround, I think you can override listview's OnSelectedIndexChanged
method, then you can do your customize selection and de-selection.
For example, you do like this:

protected override void OnSelectedIndexChanged(EventArgs e)
{
//de-select all the original selected items
for(int i=0;i<this.SelectedItems.Count;i++)
{
Console.WriteLine("SelectedItems:{0}",this.SelectedItems.Text);
this.SelectedItems.Selected=false;
}
//select the first item.
this.Items[0].Selected=true;
return;
}

It works well on my machine, hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your reply first.
However, if I try to change the selected value of any item in the
OnSelectedIndexChanged, it will fire the event again which will continue
looping.
 
How about setting a flag and checking for the flag before changing the
selection?

Like,

protected override void OnSelectedIndexChanged(EventArgs e)
{
// internalTrigger is a bool flag
if (internalTrigger)
return;

internalTrigger= true;

//de-select all the original selected items
for(int i=0;i<this.SelectedItems.Count;i++)
{
Console.WriteLine("SelectedItems:{0}",this.SelectedItems.Text);
this.SelectedItems.Selected=false;
}
//select the first item.
this.Items[0].Selected=true;

internalTrigger = false;
return;
}



alan said:
Thanks for your reply first.
However, if I try to change the selected value of any item in the
OnSelectedIndexChanged, it will fire the event again which will continue
looping.

"Jeffrey Tan[MSFT]" said:
Hi alan,

Thanks for posting in this group.
As a workaround, I think you can override listview's OnSelectedIndexChanged
method, then you can do your customize selection and de-selection.
For example, you do like this:

protected override void OnSelectedIndexChanged(EventArgs e)
{
//de-select all the original selected items
for(int i=0;i<this.SelectedItems.Count;i++)
{
Console.WriteLine("SelectedItems:{0}",this.SelectedItems.Text);
this.SelectedItems.Selected=false;
}
//select the first item.
this.Items[0].Selected=true;
return;
}

It works well on my machine, hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

 
Yes, a flag can greatly decrease the unwanted loop call(On my machine, it
decreases from 30 to 3 times).
For multi-threading safety, I think you need to use Interlocked class to
mutex the flag's change.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top