Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted
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

Posted

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

  • *Experts*
Posted

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

Posted
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.
Gamer extraordinaire. Programmer wannabe.
Posted
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

  • *Gurus*
Posted
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.

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
Posted

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

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...