ListView VS TreeView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alan

    #1

    ListView VS TreeView

    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.


  • Jeffrey Tan[MSFT]

    #2
    RE: ListView VS TreeView


    Hi alan,

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

    protected override void OnSelectedIndex Changed(EventAr gs e)
    {
    //de-select all the original selected items
    for(int i=0;i<this.Sele ctedItems.Count ;i++)
    {
    Console.WriteLi ne("SelectedIte ms:{0}",this.Se lectedItems[i].Text);
    this.SelectedIt ems[i].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.

    Comment

    • alan

      #3
      Re: ListView VS TreeView

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

      ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
      news:XIKesrvuDH A.3088@cpmsftng xa07.phx.gbl...[color=blue]
      >
      > Hi alan,
      >
      > Thanks for posting in this group.
      > As a workaround, I think you can override listview's[/color]
      OnSelectedIndex Changed[color=blue]
      > method, then you can do your customize selection and de-selection.
      > For example, you do like this:
      >
      > protected override void OnSelectedIndex Changed(EventAr gs e)
      > {
      > //de-select all the original selected items
      > for(int i=0;i<this.Sele ctedItems.Count ;i++)
      > {
      > Console.WriteLi ne("SelectedIte ms:{0}",this.Se lectedItems[i].Text);
      > this.SelectedIt ems[i].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.
      >[/color]


      Comment

      • Vijaye Raji

        #4
        Re: ListView VS TreeView

        How about setting a flag and checking for the flag before changing the
        selection?

        Like,

        protected override void OnSelectedIndex Changed(EventAr gs e)
        {
        // internalTrigger is a bool flag
        if (internalTrigge r)
        return;

        internalTrigger = true;

        //de-select all the original selected items
        for(int i=0;i<this.Sele ctedItems.Count ;i++)
        {
        Console.WriteLi ne("SelectedIte ms:{0}",this.Se lectedItems[i].Text);
        this.SelectedIt ems[i].Selected=false ;
        }
        //select the first item.
        this.Items[0].Selected=true;

        internalTrigger = false;
        return;
        }



        "alan" <abc@hotmail.co m> wrote in message
        news:OxG9buwuDH A.2260@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Thanks for your reply first.
        > However, if I try to change the selected value of any item in the
        > OnSelectedIndex Changed, it will fire the event again which will continue
        > looping.
        >
        > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
        > news:XIKesrvuDH A.3088@cpmsftng xa07.phx.gbl...[color=green]
        > >
        > > Hi alan,
        > >
        > > Thanks for posting in this group.
        > > As a workaround, I think you can override listview's[/color]
        > OnSelectedIndex Changed[color=green]
        > > method, then you can do your customize selection and de-selection.
        > > For example, you do like this:
        > >
        > > protected override void OnSelectedIndex Changed(EventAr gs e)
        > > {
        > > //de-select all the original selected items
        > > for(int i=0;i<this.Sele ctedItems.Count ;i++)
        > > {
        > > Console.WriteLi ne("SelectedIte ms:{0}",this.Se lectedItems[i].Text);
        > > this.SelectedIt ems[i].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[/color][/color]
        rights.[color=blue][color=green]
        > >[/color]
        >
        >[/color]


        Comment

        • Jeffrey Tan[MSFT]

          #5
          Re: ListView VS TreeView


          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.

          Comment

          Working...