Link to home
Start Free TrialLog in
Avatar of sukhdev
sukhdevFlag for Australia

asked on

TabControl takes a while to display controls

Hi,

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.

Thanks.
Sukhdev.

Avatar of RonaldBiemans
RonaldBiemans

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.

sorry,

Ronald

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?

james
I have experience the same problem, but only when in development mode (DEBUG)...as soon as I compile and deploy the Application in RELEASE mode...everything is snippity-snappy fast.
Avatar of sukhdev

ASKER

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)
            SelectionThread.Start()
        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)
            SelectionThread.Start()
        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
frmSelection.Hide

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.

Regards
Sukhdev.
Avatar of sukhdev

ASKER

Hi all,

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

Thanks anyway.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial