473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to return the name of a tab at a particular index?

I want to set a tab page to null and I have the index of the tab page. Is there a way I can get the name of a tab at an index, or even better address the tab by its index so I could, for example, say (NOT CODE) TabPage[tabIndex] = null; ?

Expand|Select|Wrap|Line Numbers
  1. if (tabControl1.TabCount == 1)
  2.             {
  3.                 MessageBox.Show("Unable to close the last tab, please open another tab before attempting to close this one.");
  4.             }
  5.             else
  6.             {
  7.                 int currentlySelelected = (Convert.ToInt32(tabControl1.SelectedIndex));
  8.                 tabControl1.TabPages.RemoveAt(currentlySelelected);
  9.                 tabControl1.TabPages[currentlySelelected] = null;
  10.             }
Trying to use something like this but either the tab is removed so I can't null it, or it's nulled so I can't remove it. Not sure how to overcome this.
Mar 7 '11 #1

✓ answered by GaryTexmo

You don't need to null it... per se. When you remove it from the tab control, it's no in the TabPages collection. Assuming nothing else references it, the garbage collector will eventually get rid of it.

That said, yes you can still do this. You're trying to set something to null that no longer exists in the collection. It's simple enough to fix this, just create a TabPage object and have it be a reference to the one you want to destroy.

Expand|Select|Wrap|Line Numbers
  1. TabPage destroyMe = tabControl1.TabPages[currentlySelected];
  2. tabControl1.TabPages.RemoveAt(currentlySelected);
  3. destroyMe.Dispose();
Still, anything else that refers to that particular tab page will still be trying to access it, but at least it will be in the disposed state now, helping you figure out where that might be.

Again though, this really isn't necessary as long as you're not keeping multiple references to your tab pages. Once you remove them, they're gone and you can't get them back.

8 1789
GaryTexmo
1,501 Expert 1GB
You don't need to null it... per se. When you remove it from the tab control, it's no in the TabPages collection. Assuming nothing else references it, the garbage collector will eventually get rid of it.

That said, yes you can still do this. You're trying to set something to null that no longer exists in the collection. It's simple enough to fix this, just create a TabPage object and have it be a reference to the one you want to destroy.

Expand|Select|Wrap|Line Numbers
  1. TabPage destroyMe = tabControl1.TabPages[currentlySelected];
  2. tabControl1.TabPages.RemoveAt(currentlySelected);
  3. destroyMe.Dispose();
Still, anything else that refers to that particular tab page will still be trying to access it, but at least it will be in the disposed state now, helping you figure out where that might be.

Again though, this really isn't necessary as long as you're not keeping multiple references to your tab pages. Once you remove them, they're gone and you can't get them back.
Mar 7 '11 #2
It's bad programming, but to ensure that the tab can't be opened multiple times I do a if(DesignTab != null), and if it's null I go on to create it. I'm not aware of a "already exists" check.
Mar 7 '11 #3
GaryTexmo
1,501 Expert 1GB
I don't really know the details of what you're trying to do, but if you've got two tabs you should be able to compare them. Can you provide more details about what you're trying to do? I'm not sure what to suggest here because I don't know the context.
Mar 7 '11 #4
Well I can have any number (>1) of tabs at any one time. I want to make it so that the user can only open one instance of a particular tab at any one time. How would I use the comparison?

Thanks for all your help by the way, I'm learning as I go along.
Mar 8 '11 #5
I seem to have done what I was out to achieve. Thanks for your help Gary :)
Mar 8 '11 #6
GaryTexmo
1,501 Expert 1GB
I'm not sure if you can compare them directly... a simple test will tell you if you can :)

Expand|Select|Wrap|Line Numbers
  1. TabPage p1 = tabControl1.TabPages[0];
  2. TabPage p2 = tabControl1.TabPages[0];
  3.  
  4. if (p1 == p2) MessageBox.Show("Equal!");
I'm actually thinking you can't do that, it's going to try to compare the references and come out not equal... but it's worth trying.

If that doesn't work, you can always give your tab pages unique names...

Expand|Select|Wrap|Line Numbers
  1. TabPage firstPage = new TabPage();
  2. firstPage.Name = "first page";
  3.  
  4. TabPage secondPage = new TabPage();
  5. secondPage.Name = "second page";
  6.  
  7. tabControl1.TabPages.Add(firstPage);
  8. tabControl1.TabPages.Add(secondPage);
Now, with a slightly modified version of the above...

Expand|Select|Wrap|Line Numbers
  1. TabPage p1 = tabControl1.TabPages[0];
  2. TabPage p2 = tabControl1.TabPages[0];
  3.  
  4. if (p1.Name == p2.Name) MessageBox.Show("Equal!");
... You should get a valid test.

(Assuming you still need that... you mentioned you got it figured out, but I figured I'd follow up anyway :D)
Mar 8 '11 #7
Plater
7,872 Expert 4TB
I would say it is a better plan to do this:
Expand|Select|Wrap|Line Numbers
  1. TabPage destroyMe = tabControl1.TabPages[currentlySelected]; 
  2. tabControl1.TabPages.Remove(destroyMe); 
  3. destroyMe.Dispose(); 
  4.  
Mar 8 '11 #8
GaryTexmo
1,501 Expert 1GB
Actually, I agree. That would also imply that the first test above is sufficient... curiosity has gnawed at me long enough, so I'm going to sneak into VS and verify it...

... and it does! Which now that I think about it... duh. I think I was hung up on old C++ syntax and now I'm a bit red in the face.
Mar 8 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
1
by: Bob | last post by:
Hi ! I got a problem here ... i used a ShellLinkObject is retrive some data from ..lnk files (path to execute, working directory, etc ...) but i need to get the icon path (in some case this is the...
3
by: Melissa | last post by:
I have forty-two small subforms on a form. I need help with the code that will return the name of the subform control when I click on any subform control. I know how to select all the subform...
2
by: Jeremy | last post by:
Hello, Is anyone aware of a function that will return the name of all forms contained in a database WITHOUT opening them. I have code that does it (posted below), but you must open and close the...
3
by: Greg Merideth | last post by:
This piece of code creates a fault at the attempt to get the modulename of the process when (in server2k or xp) the code gets to the "System" prcoess. Process machineProcesses =...
6
by: Srikanth | last post by:
Hi, list.remove(item) removes the first item from the list, but how do I say to remove a particular index from a list without using it's value? Let's say I have 4 items in my list as And...
0
by: Matthew Peter | last post by:
For example, how do I get this to work? def func(): print "This is", __?__ return __caller__ def echo(): print "This is ", __?__ return func()
0
by: max.vit | last post by:
I am trying to retrieve the user name (network login) of clients running a local Access DB. We use Access 2000 / 2003 and Windows XP SP 2. I have used 2 different ways to access this information...
4
by: MattGaff | last post by:
I have a list of images in the resource file of a VB project and a particular picture box on my form gets populated with an image depending on certain conditions: picNotify.Image = My.Resources. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.