need help improving this code

J

Jeff

..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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jeff

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top