Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

need help improving this code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 14 Oct 2007 15:52 GMT
.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.Controls )
           {
               if (c.Text == tab)
               {
                   value = true;
                   tabMain.SelectedIndex = index;
               }
               index++;
           }
           return value;
       }

any suggetions?
Nicholas Paldino [.NET/C# MVP] - 14 Oct 2007 17:06 GMT
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.

Signature

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

> .NET 2.0
>
[quoted text clipped - 27 lines]
>
> any suggetions?
Jeff - 14 Oct 2007 17:32 GMT
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.selectedindex
?

any suggestions?

Jeff
Nicholas Paldino [.NET/C# MVP] - 15 Oct 2007 00:03 GMT
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.

Signature

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

> 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
[quoted text clipped - 4 lines]
>
> Jeff

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.