TabControl takes a while to display controls


I have a TabControl with 4 TabPages.
Each of them has a listbox in it.... which i populate using data from a database.
Sometimes each listbox can contain a few thousand ListItems.

I am populating all my listboxes at the same time when the form loads.

The problem I'm facing is as below:

After the listobx populating process is over,
if I click on the Second TAB(which contains a listbox with about 15000+ items in it), it a takes a while for the tab to display the controls in it.

BUT now if i move to another tab and then Back to the second Tab, the controls are displayed instantly.

In other words, only when i click a Tab (that has a listbox with many items in it) for the first time, it takes times to display the controls.

Now if I hide the form and then show the form again, I get the same problem. The first time i click on tabs that have a number of items in its listbox take a while to display. Subsequent clicks on that same Tabs, displays the controls in it instantly.

Please, anyone could you let me know how I can get rid of this problem.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Sukhdev,

The reason you get this is because of the JIT compiler it will only "build" the listbox the first time you show it, that is why you only get it the first time.

I have no idea how you could speed it up, maybe a suspendlayout and resumelayout when you show the tab.

But maybe somebody else has an idea, so I'm listening to this thread aswell and maybe learn something too.



if the listbox has that many items in it, and it is taking a long time to fill it to the detriment of the main application, then fill the listbox in a seperate thread, and you will probably want to call the .add item of the listbox in method that delegates control back to the main thread?

not sure if that is the sort of thing you mean?

I have experience the same problem, but only when in development mode (DEBUG) soon as I compile and deploy the Application in RELEASE mode...everything is snippity-snappy fast.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

sukhdevAuthor Commented:
Hi again,

Actually the adding part is not a problem. I had done put the adding of items in a separate thread since the beginning.
The problem I was facing is after the adding process.

Anyway, after a lot of tries here's what I did to speed the process up. I guess it might come handy for someone:

Scenario I - Loading the form for the first time
1 Before speeding it up (this was the original - problem existed)

In the Form Load event,  i did the following in the following order
    - Detected using a boolean variable if the form was loaded for the first time, if yes do the following
    - I create a new thread
    - Started it. In this thread I added the item to all the listboxes in the tabs
    - Code shown below

    Private Sub frmSelection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If bolFirstTimeLoading Then
            SelectionThread = New Thread(AddressOf addItemsToListBox)
        End If
    End Sub

2. After speeding it up (after modifying the code and speeding the display of controls in the tab)

In the Form Load event, i modified the code slightly, which made part of the problem go away.
      - Detected using a boolean variable if the form was loaded for the first time, if yes do the following
      - Selected each Tab one by one, before adding items (done so that the tab can draw the controls)
      - Created a thread
      - Started the thread
      - Code shown below

Private Sub frmSelection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If bolFirstTimeLoading Then
            'The following four lines of code is necessary
            'These lines will make the tab display the controls within it instantly, in case of large amount of listitems
            'Without these, it controls in the TAB takes a while to display
            'Note: SelectedIndex =0 should be the last line. Because we want it to be displayed when the form shows
            TabSelection.SelectedIndex = 1
            TabSelection.SelectedIndex = 2
            TabSelection.SelectedIndex = 3
            TabSelection.SelectedIndex = 0
            SelectionThread = New Thread(AddressOf addItemsToListBox)
        End If
    End Sub

This worked just as I wanted to. Now even after doubling the items in the listbox, On clicking the Tabs the controls were shown instantly.

On closing the form, I simply made it invisible to the user, by doing

The hiding of form was done to save time, from re-adding items into the listbox
However, when showing (i.e. unhiding) the form now... the problem still existed.

Scenario II - Unhiding the same form
Not yet solved.

problem: If i click on Tab 2 (after unhiding the form) then it takes a while to display the controls.
However this happens only when i click the tab 2 for the first time.
Any other clicks on Tab 2, displays the controls in it instantly.

I've been able to solve the Scenario I, please could anybody here help me solve Scenario II also.
Maybe I've missed out something.

sukhdevAuthor Commented:
Hi all,

I think the solution i found out myself, solves the problem. so I think I'll close the question now.

Thanks anyway.
Closed, 75 points refunded.

Community Support Moderator
Experts Exchange

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.