Solved

TabControl takes a while to display controls

Posted on 2004-10-27
177 Views
Last Modified: 2010-04-23
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.

0
Question by:sukhdev
    6 Comments
     
    LVL 25

    Expert Comment

    by: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
    0
     
    LVL 1

    Expert Comment

    by:jameskearney

    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
    0
     
    LVL 7

    Expert Comment

    by:natloz
    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.
    0
     

    Author Comment

    by:sukhdev
    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.
    0
     

    Author Comment

    by:sukhdev
    Hi all,

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

    Thanks anyway.
    0
     

    Accepted Solution

    by:
    Closed, 75 points refunded.

    modulo
    Community Support Moderator
    Experts Exchange
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now