Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add labels on a form programatically

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
0
36
US
I am using this code:<br>
Set Object1(a) = Controls.Add(&quot;VB.label&quot;, b, Form1)<br>
But I want to click on a label and trip the on click event.<br>
Since there is no label in design view how do I know which label I clicked on and do something.<br>
<br>
I did this a different way before where I had a label array such as label1(0) so it was the first item in an array.<br>
then I could add more labels to that array using code somehow but I forgot how. <br>
Does any one know how to do that.<br>

 
Doug,<br>
<br>
You can use the LOAD command in Visual Basic to load another instance of a control from a control array. Just keep track of the index for the control.<br>
<br>
Load Label(1)<br>
Load Label(2)<br>
<br>
The 1 and 2 in the above example tell VB what index to make the controls. There are two things to have to do when you load intrinsic contorls at run time.<br>
<br>
1. You have position the control on the form somehwere.<br>
2. You have to make the control visible.<br>
<br>
Hope that helps,<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A>
 
that's it<br>
Thanks so much<br>

 
One other thing<br>
can I refer to the labels after I load them<br>
by<br>
ReDim Object1(MyTable.RecordCount) As VB.Label<br>
Set Object1(a) = Controls.Add(&quot;VB.label&quot;, b, Form1)<br>
<br>
but instead of .Add use some other parameter that refers to the label(1)<br>

 
See a need to set a bunch of properties and I have all this code already. So I would like to refer to the loaded &quot;labels&quot; in place of the Created &quot;labels&quot;.<br>
<br>
ReDim Object1(MyTable.RecordCount) As VB.Label<br>
ReDim Object2(MyTable.RecordCount) As VB.Label<br>
MyTable.MoveFirst<br>
<br>
Do While Not MyTable.EOF<br>
b = &quot;Label&quot; & a<br>
C = &quot;LabelC&quot; & a<br>
Set Object1(a) = Controls.Add(&quot;VB.label&quot;, b, Form1)<br>
Set Object2(a) = Controls.Add(&quot;VB.label&quot;, C, Form1)<br>
With Object1(a)<br>
.Visible = True<br>
.Top = d + 100<br>
.Width = 1500<br>
.Height = 225<br>
end with<br>

 
Doug,<br>
<br>
I'm not quite sure what it is you're trying to do, by looking at the code. From what I gather, you're reading data from a database and adding labels to the form to hold the data.<br>
<br>
You reference the label by the index number. Why not say this:<br>
<br>
With Label(a)<br>
.Visible = True<br>
.Top = d + 100<br>
.Width = 1500<br>
.Height = 225<br>
End With<br>
<br>
If you can give me a general idea of what you're tying to do I might be able to offer a better response to your question. For instance if you tell me you're reading (whatever) out of the database as a one record a piece to put the data in a label for whatever reason, I might be able to help you more. Let me know what the concept is of what you're trying to do.<br>
<br>
Thanks,<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A><br>
<br>
P.S. You can email me too, for some reason, I'm not getting the messages anymore when someone posts, so I have to keep checking the forums manually.
 
Well I can see your confusion.<br>
I am writing program that will run on the receptionist's PC and list all of the employees that work there. Next to their name have a label that say's either &quot;IN&quot; or &quot;OUT&quot; and I have a few others like &quot;doctor&quot; &quot;vacation&quot; etc.<br>
so she can glance at it to see if someone is sick or whatever. When she answers the phone she can say oh Tom is out to lunch he'll be back at 12:30 (cause she can also se e the time they left the building.<br>
I want the program to sit on the bottom of her desktop all day long.<br>
When it loads, it reads all of the employees from the datbase and loads labels accordingly.<br>
the reason I have it load labels is that as emplyee's come and go it can adjust itself based on added or deleted records.<br>
I wrote one 3 years ago that did the same thing. I have the .EXE but no code. and I don't remeber how I did it all.<br>
It by the way talks as each empolyee comes and goes it say's their name (recorded in separate .WAV files) and &quot;In the building&quot; &quot;Out for Lunch&quot; etc.<br>
The receptionist REALLY liked it becasue she did not have to look at the screen. It told her who was coming and going automatically.<br>
<br>

 
Doug,<br>
<br>
Very interesting project! I'm glad you shared that with us. What I might suggest is that you use something like the ListView control where you can just add and delete records which is much easier than adding controls to the form and deleting them. You can still have a status for each employee, just create a column for it.<br>
<br>
In the left column have the employees name, then in the right column have their status. Then just add and remove the employees from the listiview as they come and go.<br>
<br>
The other thing you could do is have two ListViews. One for employees that were &quot;IN&quot; and one for ones that were &quot;OUT&quot;. Then you could just move them between the lists as they come and go.<br>
<br>
Also, if you use a ListView control and seperate out the First and Last Name, the secretary could sort the list in whatever fashion she wanted to. ie: First name, Last Name, etc...<br>
<br>
As far as status, you could add as many columns to the listview that you needed. As the application grows you interface will nicely accomodate it. I still like the .WAV file idea you had... that shows true innovation!<br>
<br>
Just some random thoughts I had...<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A>
 
You could also you a flexgrid control. Not as easy to add and delete items but it does give you control of colors and you get the added benefit of rows and columns. Neat Project<br>
<br>
Pos.
 
Thanks ALL<br>
I did have a copy of it on my WEB site as a demo.<br>
But it got deleted. I can add it back.<br>
If you want to see it e-mail me.<br>
<A HREF="mailto:dposton@ij.net">dposton@ij.net</A><br>
I'll give you the URL.
 
Create a label : label1(0)<br>
<br>
Private Sub Form_Load()<br>
Dim l As Long<br>
Dim i As Integer<br>
Dim s As Integer<br>
Dim x As Integer<br>
FontSize = 18<br>
x = Form1.TextWidth("dit is een test")<br>
s = Form1.TextHeight("X")<br>
<br>
For i = 1 To 10<br>
Load Label1(i)<br>
Label1(i).FontSize = 18<br>
l = Label1(i - 1).Top + Label1(i - 1).Height<br>
Label1(i).Move 10, l + 100, x, s<br>
Label1(i).Caption = "test"<br>
Label1(i).Visible = True<br>
Next<br>
<br>
Eric<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top