need help improving this code

  • Thread starter Thread starter Jeff
  • Start date Start date
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?
 
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.
 
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
 
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.
 
Back
Top