Activating tab Pages

Y

Yeghia Dolbakyan

Hi Guys

I have a winform with tab control+tab pages. The logic of application
requires to work with controls in all pages on application load and
displaying first page initially. The problem I have is that listview control
in one of those pages do not work correctly until I select that page
manually. I've checked with Spy++ and found that those are created either on
mouse click or if TablConrol.SelectedPage property is set but here there
exists another problem. If I selected two pages sequentially only last page
is created,
tabCtrl.SelectedPage = 3;
tabCtrl.SelectedPage = 0;
e.g. I don;t have correct behaviour with listview control on third page.
BTW, The problem with listview is that selecting item do not add that item
to SelectedItems collection until I move to third page at least one time.

Do you have any idea?

Regards
Yeghia
 
B

Bruce Wood

Two things.

First, and easiest to answer, when you change something about a control
(any control) in your code, WinForms doesn't redraw the control right
away. It marks the control as "dirty" (needing to be redrawn) and then
redraws it when you return from your class methods back to Windows. So,
in your case, the following happens:

tabCtrl.SelectedPage = 3;
// Tab control marked as "dirty", selected page is now 3
tabCtrl.SelectedPage = 0;
// Tab control still marked "dirty", selected page is now 0.
.... some more stuff ...
} // Here you return from your method, back to Windows
// Now the tab control is redrawn with selected page 0.

So, page 3 is never redrawn, because you never returned from your
routine with SelectedPage set to 3.

There is a way to force Windows to redraw things immediately: it's
called Application.DoEvents(), but it is not very safe to use. Use at
your own risk!

Second, TabControls are problematic. They are, in my opinion, badly
implemented. There's a whole lot of clever code in them that delays all
sorts of things until each specific tab page is displayed. If any of
your code depends upon the state of a control on a tab page somewhere
then it's not going to work very well.

So, if you insist on using TabPages (I do, sometimes), then you have to
clearly separate your _model_ from your _display_. That is, somewhere
you need to store the selected item for the list view somewhere else,
and not depend upon the ListView storing that particular piece of
information. In other words, have a SelectedItem property some other
class that changes whenever the ListView changes its SelectedItem, and
read the value from that other class, not from the ListView.

I tend to write a ...Model class for each Form that I design, where the
model stores all of the information and the form just presents it. I
then hook the two together using events. (An event from the model
causes an update to the form; an event from the form causes an update
to the model.) The model then contains the current information, while
the form is just for displaying and interacting with the user.

It's more work, to be sure, but if you try to cut that corner with
TabPages you will go nuts.
 

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