|
-
July 16th, 2001, 04:50 AM
#1
Howto create a control array in .NET
Does anyone knows how to create a control array in VB.NET?
If my eyes do not deceive me, there's no index property for a control. I also tried to do it dynamically in several ways, but all failed.
If I try to create an array of controls using the load function it fails, because it seems like (not sure) that the load function has a different meaning in .NET.
When I declare an array of controls, I can load them, but I cannot seem to place them on the form.
Dim Lbls(5)
Dim T as integer
for T=0 to 4
lbls(t) = new Label
lbls(t).Text = "Label " & T.ToString ' gotta love this notation 
lbls(t).Location.X = 10
lbls(t).Location.Y = 20 + (T * 20)
next T
This fails on the lbls(t).Location.X = 10, saying I cannot assign to this expression.
Really beats me, anybody have a clue?
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
July 16th, 2001, 05:05 AM
#2
Re: Howto create a control array in .NET
Do not know exactly (I do not have .Net), but if what I heard about is true, maybe you have to do something like ...x= csngl(10)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
July 16th, 2001, 05:39 AM
#3
Re: Howto create a control array in .NET
what is csngl?
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
July 16th, 2001, 07:09 AM
#4
Re: Howto create a control array in .NET
Hi Tom,
Just found it on the net. It can help you
Create a control dynamically at run-time
You couldn't do this at all in classic VB prior to version 6, You could fake it by using a control array, hiding the first control to simulate control creation, but there was a major drawback—all the created controls were bound to the same event procedure code! To determine which control fired the event, you could write Select Case statements based on a control index number passed to the event.
Finally, VB6 added a slightly improved version. By declaring a control in advance you could "create" it at run time. But the control's events were still bound to pre-determined code. In the classic VB example below, I've used the much more prevalent control array method. Fortunately, VB.NET has none of classic VB's limitations. The first two examples are roughly equivalent—both create a control that uses the same handler as its model. The third example shows how easy it is to create a new control and bind it to any existing method that has a matching signature—for the button example shown, the Click event signature is a subroutine that has no parameters and no return value.
Classic VB:
Private Sub cmdAddButton_Click(Index As Integer)
Static counter As Integer
counter = counter + 1
If counter < 4 Then
Load cmdAddButton(counter)
With cmdAddButton(counter - 1)
cmdAddButton(counter).Move _
.Left + .Width + _
(10 * Screen.TwipsPerPixelX), .Top
End With
cmdAddButton(counter).Visible = True
End If
End Sub
--------------------------------------------------------------------------------
VB.NET (Version 1)
Private Sub btnAddButton_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles btnAddButton.Click
Static counter As Integer = 1
Static lastButton As System.Windows.Forms.Button
counter += 1
If lastButton Is Nothing Then
lastButton = btnAddButton
End If
If counter < 4 Then
Dim b As New System.Windows.Forms.Button()
With lastButton
b.Width = .Width
b.Text = .Text
Controls.Add(b)
b.Left = .Left + .Width + 10
b.Top = .Top
lastButton = b
End With
AddHandler b.Click, AddressOf _
Me.btnAddButton_Click
End If
End Sub
--------------------------------------------------------------------------------
VB.NET ( Version 2)
Private Sub btnNewAdd_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles btnNewAdd.Click
Dim b As New System.Windows.Forms.Button()
With sender
b.Width = .width
b.Text = "Click Me!"
Controls.Add(b)
b.Left = .Left + .Width + 10
b.Top = .Top
End With
AddHandler b.Click, AddressOf Me.newButtonClick
End Sub
' This is the function called by the newly created button
Private Sub newButtonClick(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
MsgBox("A new button was clicked! The index of " _
" this button is: " & Me.Controls.GetChildIndex _
(sender))
End Sub
A few final notes on this topic.
VB.NET doesn't support classic VB's control arrays directly. Adding controls takes more code, but also adds considerable flexibility. You're perfectly free to add controls to an array or collection, which you can then use to simulate the functionality of a control array.
A control's event procedures no longer have to start with the name of the control, for example, "button1_click;" instead, the procedure may have any name.
You can dynamically change the procedures bound to control events.
Iouri Boutchkine
[email protected]
-
July 16th, 2001, 07:15 AM
#5
Re: Howto create a control array in .NET
You're great, Iouri (too bad I am already out of votes!)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
July 16th, 2001, 07:16 AM
#6
Re: Howto create a control array in .NET
Csngl = a typo for Csng
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
July 16th, 2001, 07:27 AM
#7
Re: Howto create a control array in .NET
Controls.Add, hmm, I was close, but thanks, this is just what I need.
This is one of the things I hate the most about .Net, it's improvements (probably just got to get rid of those bad habbits). But on the other hand, I really start liking this language.
by the way, how do you like Beta 2? I think is a lot better than B1, to give an example, B1 took up to 1 minute to start completely, now it only takes 2 seconds (talk about improvements). They fixed the multithreading bug for VB and some other stuff.
Anyway, thanx
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
July 16th, 2001, 07:27 AM
#8
Re: Howto create a control array in .NET
Hmm, is there a limit on votes?
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
July 16th, 2001, 10:58 AM
#9
Re: Limit on votes
You can give only 5 votes per day. Every now and then I write to WebMaster asking to modify this, but -of course - it is not a vital question and (worse!) I could easily abuse...
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
July 16th, 2001, 11:07 AM
#10
Re: Howto create a control array in .NET
Here is the link maybe useful for you:
http://www.vb-zone.com/free/articles...rj070801-4.asp
Regards,
Michi
MCSE, MCDBA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|