TabControls TabPages collection cause error when removing pages

P

Peter Row

Hi,

I have created a UserControl which is subsequently hosted on a standard
form.
My control has a TabControl on it but it has no TabPages configured.
At runtime I create X pages and put a single RichTextBox on each page.

In my controls loaded event I call my Render() method which configures
everything appropriately. When setting up the TabControl the first thing
it says is (not real var names):
myTabCtrl.TabPages.Clear();

for (int i = 0; i < someInt; i++)
{
...create page
... add page to tab control
... add richtextbox to page
}

On my control there is a button which pops up a configuration dialog
whereby various settings that affect the usercontrol can be set.
Due to this I have to reinitialise by usercontrol when the dialog closes.

So thus when returning from it I call my Render() method.
When it gets to the line:
myTabCtrl.TabPages.Clear();

I get an "object reference not set to an instance of an object"
exception occur. I tried changing the line to:

if (myTabCtrl.TabPages.Count != 0) myTabCtrl.TabPages.RemoveAt(0);

....which will work in my test data's case; this again causes an object
not set error.

Anybody have any ideas why I can call the Clear() method when there
are no pages, but when I call it when there are I get the above error?

Regards,
Peter
 
D

Daniel Bass

Peter,

did you sort this out?
Why do you want to clear tabpages, when you don't have any to clear?
if you plan on calling this method as a refresh routine, say, where you want
to recreate the tab pages, then somethings wrong with the logic, because you
should create the pages once when the dialog is first initialised, then c#
will delete them all appropriately when your object is destroyed.

Daniel.
 
P

Peter Row

Hi,

No I haven't sorted this out yet.

Whether this is logical or not it should be possible but for some reason
causes an error.

My reasons for wanting to clear the TabPages when refreshing (after some
config dialog changes) is because (a) I want to reuse as much code as
possible (b) I don't want to have to write lots of conditional code.

After the tab control is initially configured with the tab pages (and each
of
the tab pages with a richtextbox control) the first page has data streamed
into it's richtextbox. As a means of not getting what I don't want I only
do this for the first tab any other tabs do not get any data inserted into
them until the user clicks/views the page.

It is possible that the user may have viewed all pages when they change
some config options hence instead of adding conditional code and extra
methods to my pages to see if they need to change their data or simply
carry on fetching as the user scrolls the richtextbox I want to clear all
the
pages, recreate them and set the initial data for the first page (as my
Render() method already does).

Anyhow as my original post said the call to:
myTabCtrl.TabPages.Clear()
....fails with an object reference not set exception.
However initially when the control first loads and this line is executed
it works fine, go figure!

So the situation is thus:
I can clear the TabPagesCollection when it's empty but when it
has some pages in it I get the above error!

At present I have left this in the hope of getting a response here
whilst I work on another project.

Regards,
Peter
 
S

Steven Cudd

I am having a similar problem. I have several pages in the tab
control. When I do the .TabPages.Clear() I get a "Cannot find table
-1" error. Basically, I want to reuse the control. I simply want to
clear out the existing pages and add several new ones.

Any ideas?

Steven
 
M

meh

Hi Steven;
I'm not sure this will help but try....

tabControl1.TabPages.Remove()


meh
 

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