need help improving this code

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

    #1

    need help improving this code

    ..NET 2.0

    I've created a window consisting of a treeview control and a tabcontrol.

    When a leave in the treeview is clicked then the code should check if the
    specific tabpage already exists in the tabcontrol and give it focus if it
    exists. Or else if it doesn't exists then add the tagpage to the tabcontrol.

    Below is the code which checks if a tabpage already exists and if it does
    then give it focus... If you have a suggestion on improving the code below
    or other suggestions (maybe .NET has some built in features which can handle
    this??) then please share them with me

    private bool GetTab(string tab)
    {
    bool value = false;
    int index = 0;
    foreach (TabPage c in tabMain.Control s )
    {
    if (c.Text == tab)
    {
    value = true;
    tabMain.Selecte dIndex = index;
    }
    index++;
    }
    return value;
    }

    any suggetions?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: need help improving this code

    Jeff,

    Yes, instead of searching all the tab pages at the same time, why not
    set the Tag property of the TreeViewNode to the tab page itself when it is
    created. Then, when you check the leaf, if the Tag property has the
    reference, just set the focus to that, otherwise, if it doesn't, create and
    add the tab page and set the Tag property. This way, you don't have to
    search the tab page list every time.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Jeff" <it_consultant1 @hotmail.com.NO SPAMwrote in message
    news:Ok9dJHnDIH A.4836@TK2MSFTN GP06.phx.gbl...
    .NET 2.0
    >
    I've created a window consisting of a treeview control and a tabcontrol.
    >
    When a leave in the treeview is clicked then the code should check if the
    specific tabpage already exists in the tabcontrol and give it focus if it
    exists. Or else if it doesn't exists then add the tagpage to the
    tabcontrol.
    >
    Below is the code which checks if a tabpage already exists and if it does
    then give it focus... If you have a suggestion on improving the code below
    or other suggestions (maybe .NET has some built in features which can
    handle this??) then please share them with me
    >
    private bool GetTab(string tab)
    {
    bool value = false;
    int index = 0;
    foreach (TabPage c in tabMain.Control s )
    {
    if (c.Text == tab)
    {
    value = true;
    tabMain.Selecte dIndex = index;
    }
    index++;
    }
    return value;
    }
    >
    any suggetions?
    >

    Comment

    • Jeff

      #3
      Re: need help improving this code

      thanks, but when I have a reference to the tabpage in the treeviewitem's tag
      property. How do I give this tabpage focus? All I got is the reference to
      the tabpage, and I AFAIK I cannot use that value on tabcontrol.sele ctedindex
      ?

      any suggestions?

      Jeff


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: need help improving this code

        Jeff,

        You don't have to store the tab page. You can store the index of the
        tab page in the Tag property (if it is null, then you know there is no tab
        page) and then set the selected index to that.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Jeff" <it_consultant1 @hotmail.com.NO SPAMwrote in message
        news:ukom5%23nD IHA.2004@TK2MSF TNGP06.phx.gbl. ..
        thanks, but when I have a reference to the tabpage in the treeviewitem's
        tag property. How do I give this tabpage focus? All I got is the reference
        to the tabpage, and I AFAIK I cannot use that value on
        tabcontrol.sele ctedindex ?
        >
        any suggestions?
        >
        Jeff
        >

        Comment

        Working...