Solved

User Controls Slow to Initialize

Posted on 2001-07-19
18
1,134 Views
Last Modified: 2013-11-26
I'm using Visual Basic 6 against an SQL Server 7 database.

I'm new to user controls so I don't understand them that well.
This project has three user controls and they really slow down the loading of the main form.

Are there any alternatives to using User Controls?
They take a very long time to initialize.

Could I call them from somewhere else in the program or do they have to be initialized at startup?

Thanks in Advance.
John
0
Comment
Question by:jtrapat1
  • 7
  • 7
  • 2
  • +1
18 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
If they are on a container, they will load during startup.  Could you explain what the controls consist of and what they are doing during initialization.

Thanks!
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
User controls by themselves are not slow to load. You must do something that slow them. Can you show us the code of your control?
0
 

Author Comment

by:jtrapat1
Comment Utility
Dave,
Thanks for the quick response.

There are three user controls:
They're simply made up of a listview control and have a Sheridan ActiveToolbars control on the form.
It looks like they're setting up the properties for the toolbars on the main form.

I have a main module execute first which sets up the recordsets that I'm using.
The next statement is frmMain.show.
But, before the form shows, the user controls initialize.

In the initialize routine, it just sets top, left, and enables a few toolbars.
It does this for the other two controls, too.

Then, the frmMain Form_Load routine executes.
This calls the user controls and passes a couple of variables to the Let Property.
Then it sets up the toolbars and tabs for the activetoolbars tabpanel since it knows what type of user I am.

Thanks for the help.

Can you just tell me something?
I kind of took over this project and am trying to understand the code.
Do I have to use a user control to set the properties of the activetoolbars?
It seems like I could do this by calling routines from the vb program itself?

Also, if you know where I can find more help on these user controls, let me know.
I'll start reading the stuff that came with activetoolbars.

Thanks again
John
0
 

Author Comment

by:jtrapat1
Comment Utility
Dave,
Thanks for the quick response.

There are three user controls:
They're simply made up of a listview control and have a Sheridan ActiveToolbars control on them.
It looks like they're setting up the properties for the toolbars on the main form.

I have a main module execute first which sets up the recordsets that I'm using.
The next statement is frmMain.show.
But, before the form shows, the user controls initialize.

In the initialize routine, it just sets top, left, and enables a few toolbars.
It does this for the other two controls, too.

Then, the frmMain Form_Load routine executes.
This calls the user controls and passes a couple of variables to the Let Property.
Then it sets up the toolbars and tabs for the activetoolbars and tabpanel since it knows what type of user I am.

Thanks for the help.

Can you just tell me something?

I kind of took over this project and am trying to understand the code.
Do I have to use a user control to set the properties of the activetoolbars?
It seems like I could do this by calling routines from the vb program itself?

Also, if you know where I can find more help on these user controls, let me know.
I'll start reading the stuff that came with activetoolbars.

Thanks again
John
0
 

Author Comment

by:jtrapat1
Comment Utility
emoreau,
The code is pretty long:
Here's one of the controls:

Option Explicit
'***********************************************************************
Public Event AddBtnClick()
Public Event RemoveBtnClick(Position As Integer, Name As String)
Public Event UpBtnClick(Position As Integer, Name As String)
Public Event DownBtnClick(Position As Integer, Name As String)
Private mvarMaxAnalysts As Integer
Private mvarBackColor As Long
Private mvarEnabled As Boolean
Dim ItemText1 As String
Dim ItemText2 As String
Dim ItemText3 As String

'***********************************************************************
'***BEGIN FILL ARRAY - USED TO UPDATE THE DATABASE***

Public Sub GetAnalysts(AnalystArray, NumAnalysts)
Dim i As Integer
    For i = 1 To LVAnalysts.ListItems.Count
        AnalystArray(i, 1) = LVAnalysts.ListItems.Item(i).Text
        AnalystArray(i, 2) = LVAnalysts.ListItems.Item(i).SubItems(1)
        AnalystArray(i, 3) = LVAnalysts.ListItems.Item(i).SubItems(2)
        AnalystArray(i, 4) = LVAnalysts.ListItems.Item(i).SubItems(3)
    Next i
    NumAnalysts = LVAnalysts.ListItems.Count
End Sub

'***END OF FILL ARRAY***

Public Sub Clear()
    LVAnalysts.ListItems.Clear
    SetButtons
End Sub

Public Sub LoadAnalyst(Name As String, DateAssign As Date, Message As String)
Dim NewItem As ListItem
Dim i As Integer
    For i = 1 To LVAnalysts.ListItems.Count
        If Name = LVAnalysts.ListItems.Item(i).SubItems(1) Then
            MsgBox "Name already in List"
            Exit Sub
        End If
    Next i
        Set NewItem = LVAnalysts.ListItems.Add(, , LVAnalysts.ListItems.Count + 1)
        NewItem.ListSubItems.Add , , Name
        NewItem.ListSubItems.Add , , DateAssign
        NewItem.ListSubItems.Add , , Message
        LVAnalysts.ListItems(LVAnalysts.ListItems.Count).Selected = True
    SetButtons
End Sub

Public Sub SetButtons()
Dim ItemCount As Integer
    On Error GoTo eh:
    If LVAnalysts.Enabled = False Then
        TB.Tools("ID_Add").Enabled = False
        TB.Tools("ID_Remove").Enabled = False
        TB.Tools("ID_MoveUp").Enabled = False
        TB.Tools("ID_MoveDown").Enabled = False
        Exit Sub
    End If
    ItemCount = LVAnalysts.ListItems.Count
    Select Case ItemCount
        Case 0
            TB.Tools("ID_Add").Enabled = True
            TB.Tools("ID_Remove").Enabled = False
            frmBTMain.ssActiveTabs1.Tabs(4).Enabled = False
        Case mvarMaxAnalysts
            TB.Tools("ID_Add").Enabled = False
            TB.Tools("ID_Remove").Enabled = True
            frmBTMain.ssActiveTabs1.Tabs(4).Enabled = True
        Case Else
            TB.Tools("ID_Add").Enabled = True
            TB.Tools("ID_Remove").Enabled = True
            frmBTMain.ssActiveTabs1.Tabs(4).Enabled = True
    End Select
   
    Select Case LVAnalysts.SelectedItem.Index
        Case 1
            If ItemCount > 1 Then
                TB.Tools("ID_MoveUp").Enabled = False
                TB.Tools("ID_MoveDown").Enabled = True
            Else
                TB.Tools("ID_MoveUp").Enabled = False
                TB.Tools("ID_MoveDown").Enabled = False
            End If
           
        Case ItemCount
            If ItemCount > 1 Then
                TB.Tools("ID_MoveUp").Enabled = True
                TB.Tools("ID_MoveDown").Enabled = False
            Else
                TB.Tools("ID_MoveUp").Enabled = False
                TB.Tools("ID_MoveDown").Enabled = False
            End If
           
        Case Else
            If ItemCount > 2 Then
                TB.Tools("ID_MoveUp").Enabled = True
                TB.Tools("ID_MoveDown").Enabled = True
            Else
                TB.Tools("ID_MoveUp").Enabled = True
                TB.Tools("ID_MoveDown").Enabled = False
            End If
    End Select
    If mvarEnabled = False Then TB.Tools("ID_Add").Enabled = False
    Exit Sub
eh:
    TB.Tools("ID_MoveUp").Enabled = False
    TB.Tools("ID_MoveDown").Enabled = False
    TB.Tools("ID_Add").Enabled = True
End Sub

Private Sub TB_ToolClick(ByVal Tool As ActiveToolBars.SSTool)
Dim Response As Integer
    Select Case Tool.ID
        Case "ID_Add"
            RaiseEvent AddBtnClick
            SetButtons
            GetAnalysts AnalystArray, NumAnalysts
            If NumAnalysts = 1 Then Call FindOwner
        Case "ID_Remove"
            Response = MsgBox("Are you sure you want to remove " & _
                                LVAnalysts.SelectedItem.SubItems(1) & _
                                " from the list?", vbYesNo, "Delete " & _
                                LVAnalysts.SelectedItem.SubItems(1))
            If Response = vbYes Then
                RaiseEvent RemoveBtnClick(LVAnalysts.SelectedItem.Index, LVAnalysts.SelectedItem.SubItems(1))
                RemoveAnalyst LVAnalysts.SelectedItem.Index
            End If
            If LVAnalysts.ListItems.Count = 0 And frmBTMain.cmbBamCode.Text = "X" Then
                MsgBox "This Bill has been previously requested and now has no analysts assigned!"
            End If
        Case "ID_MoveUp"
            RaiseEvent DownBtnClick(LVAnalysts.SelectedItem.Index, _
                                    LVAnalysts.SelectedItem.SubItems(1))
            MoveUp LVAnalysts.SelectedItem.Index
        Case "ID_MoveDown"
            RaiseEvent UpBtnClick(LVAnalysts.SelectedItem.Index, _
                                    LVAnalysts.SelectedItem.SubItems(1))
            MoveDown LVAnalysts.SelectedItem.Index
    End Select
End Sub

Private Sub TB_ToolBarModified(ByVal Change As ActiveToolBars.Constants_Modified, ByVal ToolBar As ActiveToolBars.SSToolBar, ByVal Tool As ActiveToolBars.SSTool)
    LVAnalysts.Width = ScaleWidth
    LVAnalysts.Height = ScaleHeight
End Sub

'***********************************************************************
'***BEGIN TOOLBAR BUTTON FUNCTIONS***

Private Sub RemoveAnalyst(Analyst As Integer)
Dim i As Integer
    LVAnalysts.ListItems.Remove Analyst
    For i = 1 To LVAnalysts.ListItems.Count
        LVAnalysts.ListItems(i).Text = i
    Next i
    If LVAnalysts.ListItems.Count = 0 Then TB.Tools("ID_Remove").Enabled = False
    SetButtons
    SendAssignments = True
End Sub

Private Sub MoveDown(Analyst As Integer)
    ItemText1 = LVAnalysts.SelectedItem.SubItems(1)
    ItemText2 = LVAnalysts.SelectedItem.SubItems(2)
    ItemText3 = LVAnalysts.SelectedItem.SubItems(3)
    LVAnalysts.SelectedItem.SubItems(1) = LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).SubItems(1)
    LVAnalysts.SelectedItem.SubItems(2) = LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).SubItems(2)
    LVAnalysts.SelectedItem.SubItems(3) = "N"
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).SubItems(1) = ItemText1
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).SubItems(2) = ItemText2
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).SubItems(3) = ItemText3
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index + 1).Selected = True
    SetButtons
    SendAssignments = True
End Sub

Private Sub MoveUp(Analyst As Integer)
    ItemText1 = LVAnalysts.SelectedItem.SubItems(1)
    ItemText2 = LVAnalysts.SelectedItem.SubItems(2)
    ItemText3 = LVAnalysts.SelectedItem.SubItems(3)
    LVAnalysts.SelectedItem.SubItems(1) = LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(1)
    LVAnalysts.SelectedItem.SubItems(2) = LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(2)
    LVAnalysts.SelectedItem.SubItems(3) = LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(3)
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(1) = ItemText1
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(2) = ItemText2
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).SubItems(3) = "N"
    LVAnalysts.ListItems(LVAnalysts.SelectedItem.Index - 1).Selected = True
    SetButtons
    SendAssignments = True
End Sub

'***END OF TOOLBAR BUTTON FUNCTIONS***
'***********************************************************************
'***BEGIN USER CONTROL PROPERTIES***

Public Property Let Enabled(ByVal vData As Boolean)
    mvarEnabled = vData
    LVAnalysts.Enabled = vData
    If mvarEnabled Then
        SetButtons
    Else
        TB.Tools("ID_Add").Enabled = False
        TB.Tools("ID_Remove").Enabled = False
        TB.Tools("ID_MoveUp").Enabled = False
        TB.Tools("ID_MoveDown").Enabled = False
    End If
End Property

Public Property Get Enabled() As Boolean
    Enabled = mvarEnabled
End Property

Public Property Let MaxAnalysts(ByVal vData As Integer)
    mvarMaxAnalysts = vData
End Property

Public Property Get BackColor() As Long
    BackColor = mvarBackColor
End Property

Public Property Let BackColor(ByVal vData As Long)
    mvarBackColor = vData
    LVAnalysts.BackColor = vData
End Property

Public Property Get MaxAnalysts() As Integer
    MaxAnalysts = mvarMaxAnalysts
End Property

Private Sub UserControl_Resize()
    LVAnalysts.Width = ScaleWidth
    LVAnalysts.Height = ScaleHeight
End Sub

Private Sub LVAnalysts_GotFocus()
    SetButtons
End Sub

Private Sub LVAnalysts_ItemClick(ByVal Item As MSComctlLib.ListItem)
    SetButtons
End Sub

Private Sub UserControl_Initialize()
Dim NewItem As ListItem
    LVAnalysts.Top = 0
    LVAnalysts.Left = 0
    TB.Tools("ID_Add").Enabled = False
    TB.Tools("ID_Remove").Enabled = False
    TB.Tools("ID_MoveUp").Enabled = False
    TB.Tools("ID_MoveDown").Enabled = False
End Sub

Private Sub LVAnalysts_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
        TB.PopupMenu "ID_PopMenu"
    End If
End Sub

'***END OF USER CONTROL PROPERTIES***
'***BEGIN FIND OWNER BASED ON PRIMARY ANALYST***
Private Sub FindOwner()
Dim Done As Boolean
    GetAnalysts AnalystArray, NumAnalysts
    RsPrivileges.MoveFirst
    Done = False
    While Not RsPrivileges.EOF And Not Done
      If RsPrivileges!UserName = AnalystArray(1, 2) Then
        frmBTMain.cmbOwner.Text = RsPrivileges!Owner
        Done = True
      End If
      RsPrivileges.MoveNext
    Wend
End Sub
'***END FIND OWNER BASED ON PRIMARY ANALYST***
'***END OF USER CONTROL***
'***********************************************************************
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
The code looks like it could use some polish.

For starters I would change this type of code....

TB.Tools("ID_Add").Enabled = False
TB.Tools("ID_Remove").Enabled = False
TB.Tools("ID_MoveUp").Enabled = False
TB.Tools("ID_MoveDown").Enabled = False

TO

With TB
 .Tools("ID_Add").Enabled = False
 .Tools("ID_Remove").Enabled = False
 .Tools("ID_MoveUp").Enabled = False
 .Tools("ID_MoveDown").Enabled = False
End With

That will help.

Also, if you can get rid of those variant arrays, you will save yourself alot of processing time!
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
<<Do I have to use a user control to set the properties of the activetoolbars?>>

If the toolbars are within the control, then yes.  If the toolbars are outside the control, you should check and see why the UC is manipulating them.  Maybe you can change it, maybe not.

Also, if you know where I can find more help on these user controls, let me know.

Here is an interesting link

Improve ActiveX Control Performance
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbpj99/html/balena.asp
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
I'm sure it is not the user control that is long but rather the fact that you populate listviews. Have you traced your code to find where time is lost? How much time?
0
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
Had a look at the code and apart from Dave's suggestions the only real thing I can see slowing things down may be the FindOwner code.

You said the code loads slowly on initialisation. This may not be the actual controls themselves it may be the loading of your recordsets that is slowing things up rather than the controls.

ActiveToolbar controls from Infragistics (formerly Sheridan) seem slow at the best of times anyway. I have the same problems with ActiveThreeD.

Vin.
0
IT, Stop Being Called Into Every Meeting

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!

 

Author Comment

by:jtrapat1
Comment Utility
Dave,

I'm sure it's the user controls that are slowing down the application; I stepped thru it and that seems to be the case.

Can you let me know one more thing:
If I want to rewrite this code without the user controls, what are my options?

In the code I included above, the ucAnalystLister.ctl, this is a user control with a listview and an instance of a SSActiveToolbar on it.
I noticed it takes a long time in the Initialize and Resize Events to load at startup.
I'd like to eliminate the user controls and add the code in another way, if possible.
If I were to rewrite this, couldn't I just drag a SSActiveToolbar control onto my main form, and maybe create a module with all of the methods and properties that are in the user control now.
Then, I could access this module from my main form.
Like I said, I've never used user controls before, so I'm not sure if we need to use user controls with the activetoolbars control.

If not,how would I go about this?

I guess I'm asking:  Do I have to use a user control if I want to use the sheridan controls?

Thanks in advance.

John
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
<<If I want to rewrite this code without the user controls, what are my options?>>

UserControls are good things if you are performing redundant tasks in VB.  You can use UserControls on the Web also, but that is a separate issue.  In VB they are generally used for Code Reuse.

<<, so I'm not sure if we need to use user controls with
the activetoolbars control.>>

No you do not need to place the activetoolbars into a usercontrol.  3rd party controls like this one will work in any container (form, usercontrol, frame, etc)

<<I guess I'm asking:  Do I have to use a user control if I want to use the sheridan controls?>>

No you sure don't

-Dave

0
 

Author Comment

by:jtrapat1
Comment Utility
Dave,

I'm sure it's the user controls that are slowing down the application; I stepped thru it and that seems to be the case.

Can you let me know one more thing:
If I want to rewrite this code without the user controls, what are my options?

In the code I included above, the ucAnalystLister.ctl, this is a user control with a listview and an instance of a SSActiveToolbar on it.
I noticed it takes a long time in the Initialize and Resize Events to load at startup.
I'd like to eliminate the user controls and add the code in another way, if possible.
If I were to rewrite this, couldn't I just drag a SSActiveToolbar control onto my main form, and maybe create a module with all of the methods and properties that are in the user control now.
Then, I could access this module from my main form.
Like I said, I've never used user controls before, so I'm not sure if we need to use user controls with the activetoolbars control.

If not,how would I go about this?

I guess I'm asking:  Do I have to use a user control if I want to use the sheridan controls?

Thanks in advance.

John
0
 

Author Comment

by:jtrapat1
Comment Utility
Dave,
Okay, I replaced the three user controls with three class modules:  clsAnalyst, clsBillComments, and clsRequests.
I went back to see if it would quicken the loading of my main form but it still seems slow.
This time when I step thru line by line, there's a pause for about 15 seconds between two lines of code:
At the end of my main module,
frmBTMain.Show
(after it finishes opening all off the recordsets)
and the actually launching of this form.
Like I said, when I step thru line by line, there is no particular line that seems to slow the form.
It must be something happening in the background, maybe from the SQL Server side.
How can I check for slowdowns on the sql server side?
Do you have any suggestions?

Thanks in advance.
John
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
Is there any way you can cut back on the openening all of the recordsets at start up?  Try to determine what information you need at startup and let the others wait, if at all possible.  

You could start timing your queries

debug.print time
'make the call
debug.print time

Then if you have one that takes too long we can try to speed it up.
0
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
Think I suggested that one already Dave.

Vin.
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
Vin, Yes it has been stated a couple of times on the thread already.

jtrapat1,

Make sure you are reading everyone's posts as there is excellent information to be found in them.

Cheers
-Dave
0
 

Author Comment

by:jtrapat1
Comment Utility
Dave (and everyone who helped with this thread):

I wanted to thank everyone who helped with their comments and suggestions.
Over the weekend, I tried this program with a command object and also moving the opening of the recordsets to a different part of the program.

I finally found out what was slowing my program down.

I should've mentioned this before but I was looking in the wrong spot for the slow-down.
This application uses a lot of crystal reports.
There are about 20-30 different reports listed on the menu drop-downs.  
The way the application is currently written there are crystal report controls ON THE FORM for each crystal report to be generated.
So, there are 30 crystal controls from the tool box on the main form. (crystl32.ocx)
When I removed all of the controls, the main form launched in about a second!!
Is This Correct??
Or, is there an alternative way to optimally use the memory needed for each crystal report control?
For each control, there must be some way to instantiate thru code, a new crystal report.

I'll probably have to post this in the Crystal Reports section of this forum; so in case I don't hear back, thanks again for all the help.

John
0
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 200 total points
Comment Utility
Whenever you need to create a report just dim a new instance of it.

Dim oReport as New CRReport 'or whatever the object name is

You can add one to a form with the
FormName.Controls.Add  method...

etc
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

16 Experts available now in Live!

Get 1:1 Help Now