Antoine Posted August 20, 2003 Posted August 20, 2003 Hello, I've worked with Visual Basic 6 for a long time, and now I'm trying to get VB .NET working. But, I cannot find the "Index" property in de properties windows. E.g. I want to create (during designtime), 7 labels with indexes 0 to 6.... How can I do this ? Is this function renamed or something, I can't find anything on MSDN either Regards, Antoine Quote
*Experts* Volte Posted August 20, 2003 *Experts* Posted August 20, 2003 Control arrays no longer exist in .NET; you can make arrays of controls, but only through code, like so:Dim myLabel(6) As Label Dim i As Integer For i = 0 to 6 myLabel(i) = New Label() With myLabel(i) .Location = New Point(10, i * 18) .Size = New Size(150, 18) 'other property settings and stuff End With Me.Controls.Add(myLabel(i)) Next Quote
Antoine Posted August 20, 2003 Author Posted August 20, 2003 That's bad....... :( But thanks for your information ! Quote
*Experts* Nerseus Posted August 20, 2003 *Experts* Posted August 20, 2003 Here's an article that may help: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/controlarrays.asp -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Antoine Posted August 26, 2003 Author Posted August 26, 2003 (edited) Thanks that works :D Edited August 26, 2003 by Antoine Quote
Antoine Posted August 26, 2003 Author Posted August 26, 2003 Okay, I have created 12 buttons during runtime, now I want to add some activity to them. How can I generate an event when clicking a run-time created button ? THanks allready ! Antoine Quote
*Gurus* divil Posted August 26, 2003 *Gurus* Posted August 26, 2003 Use the AddHandler statement to assign a method in your code to an event on a control created at runtime. If you look AddHandler up in the help there should be an example. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* mutant Posted August 26, 2003 *Experts* Posted August 26, 2003 You need to have a sub that accepts the right arguments for the event you wish to have and add a handler using AddHandler: AddHandler object.Event, AddressOf subthatwill handle Quote
Antoine Posted August 27, 2003 Author Posted August 27, 2003 Yes I did that, but is that possible on an array to ? Regards, Antoine Quote
wyrd Posted August 27, 2003 Posted August 27, 2003 I don't think so, but you can always try. If not, you'll have to loop through all the objects in the array and do it that way. Quote Gamer extraordinaire. Programmer wannabe.
Antoine Posted August 27, 2003 Author Posted August 27, 2003 I don't think so, but you can always try. If not, you'll have to loop through all the objects in the array and do it that way. I allready thought so :( , from my opinion VB.NET really sucks.... but I don't have a choice :-\ . But thank you for the information. I've tried it but then I have to make e.g. 12 "Sub's" for an array, while I could have made it in one if array's would've worked.... Thank you all for your information !!! Regards, Antoine Quote
*Gurus* divil Posted August 27, 2003 *Gurus* Posted August 27, 2003 You can still use one sub to handle all the controls in the array, I don't understand what your problem is. You were shown how to assign events using AddHandler, you can do this for as many controls as you like for the same subroutine. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders dynamic_sysop Posted August 27, 2003 Leaders Posted August 27, 2003 here's a quick example i made for you :) Private WithEvents buttons As Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Button1(11) As Button, i As Integer For i = LBound(Button1) To UBound(Button1) Button1(i) = New Button() If i = 0 Then Button1(i).Location = New System.Drawing.Point(8, 32) ElseIf i >= 0 And i <= 5 Then Button1(i).Location = New System.Drawing.Point(Button1(i - 1).Location.X + Button1(i).Width, 32) ElseIf i = 6 Then Button1(i).Location = New System.Drawing.Point(8, 64) Else Button1(i).Location = New System.Drawing.Point(Button1(i - 1).Location.X + Button1(i).Width, 64) End If MyBase.Controls.Add(Button1(i)) Button1(i).Tag = i Button1(i).Text = "Button1(" & i & ")" AddHandler Button1(i).Click, AddressOf buttons_Click Next End Sub Private Sub buttons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttons.Click Dim btn As Button = DirectCast(sender, Button) '/// buttons now work as an array. MessageBox.Show("i was clicked by ... " & btn.Tag) End Sub Quote
Antoine Posted September 9, 2003 Author Posted September 9, 2003 Great thanks, sorry for the late reply, but I was out for a few days ! I will try that !! Regards, Antoine Quote
Antoine Posted September 9, 2003 Author Posted September 9, 2003 Hey, that works nicely ! Grtz, Antoine Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.