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; ? - if (tabControl1.TabCount == 1)
-
{
-
MessageBox.Show("Unable to close the last tab, please open another tab before attempting to close this one.");
-
}
-
else
-
{
-
int currentlySelelected = (Convert.ToInt32(tabControl1.SelectedIndex));
-
tabControl1.TabPages.RemoveAt(currentlySelelected);
-
tabControl1.TabPages[currentlySelelected] = null;
-
}
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.
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. - TabPage destroyMe = tabControl1.TabPages[currentlySelected];
-
tabControl1.TabPages.RemoveAt(currentlySelected);
-
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
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. - TabPage destroyMe = tabControl1.TabPages[currentlySelected];
-
tabControl1.TabPages.RemoveAt(currentlySelected);
-
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.
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.
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.
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.
I seem to have done what I was out to achieve. Thanks for your help Gary :)
I'm not sure if you can compare them directly... a simple test will tell you if you can :) - TabPage p1 = tabControl1.TabPages[0];
-
TabPage p2 = tabControl1.TabPages[0];
-
-
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... - TabPage firstPage = new TabPage();
-
firstPage.Name = "first page";
-
-
TabPage secondPage = new TabPage();
-
secondPage.Name = "second page";
-
-
tabControl1.TabPages.Add(firstPage);
-
tabControl1.TabPages.Add(secondPage);
Now, with a slightly modified version of the above... - TabPage p1 = tabControl1.TabPages[0];
-
TabPage p2 = tabControl1.TabPages[0];
-
-
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)
I would say it is a better plan to do this: -
TabPage destroyMe = tabControl1.TabPages[currentlySelected];
-
tabControl1.TabPages.Remove(destroyMe);
-
destroyMe.Dispose();
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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()
|
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...
|
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.
...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |