Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Add Control in Form at Runtime - No Control Array


Reply
 
Thread Tools Display Modes
  #1  
Old 03-28-2003, 08:19 AM
Purdue2379 Purdue2379 is offline
Regular
 
Join Date: Mar 2003
Posts: 55
Default Add Control in Form at Runtime - No Control Array


Quote:
Originally Posted by Hasin
Hi Dear Programmers,

Some of you wanna know How to add a picturebox or textbox in a form at runtime. Here is two answer -

1. You can use control array. But in this way you must have at least one same control in your form to initialize the array (with index = 0) and you cannot unload any control that you placed in design time.

2. Another way is to use the control collection of the form you want to add a control at runtime. In this way you need not to use the control array - no need to place any instance of that control in design time. Even in this way - you can use the event of that control if yoy declare it with withevent keyword. This is done in following manner. Place the following code in a form. This code add a command button in runtime. )


'***************************
'Code Starts Here

Dim WithEvents cmd As CommandButton

Private Sub cmd_Click()
MsgBox "Hello"
End Sub

Private Sub Form_Load()
Set cmd = Me.Controls.Add("vb.commandbutton", "cmd")
cmd.Top = 1000
cmd.Left = 1000
cmd.Height = 400
cmd.Width = 1600
cmd.Caption = "EvelinDev"
cmd.Visible = True
End Sub

'Code Ends Here
'****************************


# The thing is that if you want to want to add a picturebox then inside the add method of control collection of a form - use vb.picturebox instead of vb.commandbutton.

You can use any kind of VisualBasic Intrinsic Control at runtime in this way.



Hasin Hayder
Lead Programmer
EvelinDev




How would I incorporate this code into a form where I don't want to use a command button to add a new label? What I want to do is when I update a sql table with a new record the form automatically adds two new labels in a row on the form reflecting the added information. Would I need to use sometype of Do while loop?

Here is the code I am using now that will not update the form:

Private Sub GetNew214Info()
' Get info from new Carrier214 table
'
Dim rsNew As ADODB.Recordset
Dim sSQL As String
Dim i As Integer
'
On Error GoTo GetNew214Info_Err
'
' Setup SQL statement
sSQL = "SELECT SCAC, Enabled, LastFeed, NumberFeeds " & _
"From CarrierTable " & _
"Where (Enabled = 1) " & _
"ORDER BY LastFeed DESC "

'
' Open Recordset
If Open_ADORS(cnShipStatDB, rsNew, sSQL) = True Then
'
' Check Record Count to inture there were records found
If rsNew.RecordCount > 0 Then


' Records found
'
rsNew.MoveFirst
i = 0

' Loop through records and insert information into form labels
Do While rsNew.EOF = False

'frmWatch.lblNewSCAC(i).Caption = rsNew.Fields("SCAC")
'frmWatch.lblLastEntry(i).Caption = rsNew.Fields("LastFeed")
'frmWatch.lblNumber(i).Caption = rsNew.Fields("NumberFeeds")

i = i + 1
rsNew.MoveNext

DoEvents
Loop

Else
' no records in RS
MsgBox "No records found in Recordset.", vbOKOnly
End If

' Close Recordset
Call Close_ADORS(rsNew)

End If
'
Exit Sub
'
GetNew214Info_Err:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly
'
End Sub
Reply With Quote
  #2  
Old 03-28-2003, 08:30 AM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Much, much easier to just use a label control array. Since you will
already have at least one label on your form, it just doesn't make any
sense to use this very esoteric method of adding controls at runtime.
I use the standard control array method in this example...
http://www.xtremevbtalk.com/show...threadid=20002
It queries the table for the number of fields and adds controls to match.
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 03-28-2003, 11:21 AM
Purdue2379 Purdue2379 is offline
Regular
 
Join Date: Mar 2003
Posts: 55
Default

Quote:
Originally Posted by Thinker
Much, much easier to just use a label control array. Since you will
already have at least one label on your form, it just doesn't make any
sense to use this very esoteric method of adding controls at runtime.
I use the standard control array method in this example...
http://www.xtremevbtalk.com/show...threadid=20002
It queries the table for the number of fields and adds controls to match.



I used For loop code from the Sub Form_Load that was included in your sample program. Is the For Loop all I need to populate the labels and add a new lable to the form? When I run the code is throws up an error: 360 Oject already loaded. I'm not sure where to include the code. Can you help me out? Thanks

Private Sub GetNew214Info()
' Get info from new Carrier214 table
'
Dim rsNew As ADODB.Recordset
Dim sSQL As String
Dim i As Integer
Dim temp As String, xx As Integer
'
On Error GoTo GetNew214Info_Err
'
' Setup SQL statement
sSQL = "SELECT SCAC, Enabled, LastFeed, NumberFeeds " & _
"From CarrierTable " & _
"Where (Enabled = 1) " & _
"ORDER BY LastFeed DESC "

'
' Open Recordset
If Open_ADORS(cnShipStatDB, rsNew, sSQL) = True Then
'
' Check Record Count to inture there were records found
If rsNew.RecordCount > 0 Then

' Records found
'
rsNew.MoveFirst
i = 0

' Loop through records and insert information into form labels
Do While rsNew.EOF = False

For xx = 0 To rsNew.Fields.Count - 1
If xx > 0 Then
Load frmWatch.lblNewSCAC(xx)
Load frmWatch.lblLastEntry(xx)
Load frmWatch.lblNumber(xx)
frmWatch.lblNewSCAC(xx).Top = frmWatch.lblNewSCAC(xx - 1).Top + 400
frmWatch.lblLastEntry(xx).Top = frmWatch.lblLastEntry(xx - 1).Top + 400
frmWatch.lblNumber(xx).Top = frmWatch.lblNumber(xx - 1).Top + 400

frmWatch.lblNewSCAC(xx).Visible = True
frmWatch.lblLastEntry(xx).Visible = True
frmWatch.lblNumber(xx).Visible = True

End If
Next xx
frmWatch.lblNewSCAC(i).Caption = rsNew.Fields("SCAC")
frmWatch.lblLastEntry(i).Caption = rsNew.Fields("LastFeed")
frmWatch.lblNumber(i).Caption = rsNew.Fields("NumberFeeds")

i = i + 1
rsNew.MoveNext

DoEvents
Loop


Else
' no records in RS
MsgBox "No records found in Recordset.", vbOKOnly
End If

' Close Recordset
Call Close_ADORS(rsNew)

End If
'
Exit Sub
'
GetNew214Info_Err:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly
'
End Sub
Reply With Quote
  #4  
Old 03-28-2003, 11:35 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

How many label with the same name you have in the form at design mode ???
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #5  
Old 03-28-2003, 11:44 AM
Purdue2379 Purdue2379 is offline
Regular
 
Join Date: Mar 2003
Posts: 55
Default

Quote:
Originally Posted by Mikecrosoft
How many label with the same name you have in the form at design mode ???




I have two labels: lblNewSCAC and lblLastEntry

there are 13 instances of each label

ex lblNewSCAC(0) - (13)

when I add a new record to the sql table I want it to be reflected on the form by adding a new label to each column.

ex lblNewSCAC (14) lblLastEntry(14)
Reply With Quote
  #6  
Old 03-28-2003, 11:59 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

If you have currently 13 labels at design mode then change this:

Code:
For xx = 0 To rsNew.Fields.Count - 1 [B]If xx > 13 Then[/b] Load frmWatch.lblNewSCAC(xx) Load frmWatch.lblLastEntry(xx) Load frmWatch.lblNumber(xx) frmWatch.lblNewSCAC(xx).Top = frmWatch.lblNewSCAC(xx - 1).Top + 400 frmWatch.lblLastEntry(xx).Top
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #7  
Old 03-28-2003, 12:54 PM
Purdue2379 Purdue2379 is offline
Regular
 
Join Date: Mar 2003
Posts: 55
Default

Quote:
Originally Posted by Mikecrosoft
If you have currently 13 labels at design mode then change this:

Code:
For xx = 0 To rsNew.Fields.Count - 1 [B]If xx > 13 Then[/b] Load frmWatch.lblNewSCAC(xx) Load frmWatch.lblLastEntry(xx) Load frmWatch.lblNumber(xx) frmWatch.lblNewSCAC(xx).Top = frmWatch.lblNewSCAC(xx - 1).Top + 400 frmWatch.lblLastEntry(xx).Top




That works, but I have one question. Why is it that I can change the value to any number equal to or greater than 3? I also don't know if it works fully because I don't have access to the sql table. So I don't know if it will add the labels.
Reply With Quote
  #8  
Old 03-28-2003, 01:34 PM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

Example: your error was for this reason:

I have currently 13 Labels (Array) 0 to 12 If I try to load one of these currently loaded labels, the error is raised. Why, obviously becuase I'm trying to load a label with the same name and Index.

Then the If statement protected you of this error:
Code:
If XX > Labels Upper Bound Then 'Here Load a new label End IF 'Change the current label properties

Where XX is the current Label Index.

I hope helps
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #9  
Old 03-28-2003, 02:04 PM
Purdue2379 Purdue2379 is offline
Regular
 
Join Date: Mar 2003
Posts: 55
Default

Quote:
Originally Posted by Mikecrosoft
Example: your error was for this reason:

I have currently 13 Labels (Array) 0 to 12 If I try to load one of these currently loaded labels, the error is raised. Why, obviously becuase I'm trying to load a label with the same name and Index.

Then the If statement protected you of this error:
Code:
If XX > Labels Upper Bound Then 'Here Load a new label End IF 'Change the current label properties

Where XX is the current Label Index.

I hope helps



Why is it then that I can enter 4 and the code still works?
Reply With Quote
  #10  
Old 03-28-2003, 02:11 PM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

How many records you have in the SQL Table ???
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #11  
Old 03-28-2003, 04:23 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

If you truly have 13 elements in the lblNewSCAC() control array and the
indexes are numbered 0 to 12, then this line, Load frmWatch.lblNewSCAC(xx)
will error with any value less than 13. So if it works with 4, you either
don't have 13 elements in the array, or you have changed the index
numbers to be higher than the default.
__________________
Posting Guidelines
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->