fillibar Posted June 4, 2003 Posted June 4, 2003 I checked the forum, and ran across someone that had 500 textboxes, for which an array can work. My problem is that I have between 8 and 22 elements on different forms which require the WithEvents setting. These are primarily checkboxes that enable or disable other, conflicting checkboxes, and buttons that open option windows. In VB6 I simply made them all indexed, so could create them as part of loops as needed. VB.Net states that a WithEvents object cannot be part of an array. I would REALLY want to avoid making each button and checkbox separate, because the code would be highly redundant, and VERY annoying then. Is there any way to make the equivalent of indexes(arrays) while retaining WithEvents? I tried making a custom class, but it still got the same error. And yes, I did check the FAQ, and I am not sure that will entirely suit my needs. Quote
sjn78 Posted June 5, 2003 Posted June 5, 2003 Can you create a sub that handles all or some of the events that are required? Im not fully sure of what you are trying to achieve Quote
fillibar Posted June 5, 2003 Author Posted June 5, 2003 'In VB6 I would have had sets like: Begin VB.CommandButton btnPlayerInfo Caption = "Player2 Info" BeginProperty Font Name = "MS Sans Serif" Size = 12 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 495 Index = 1 Left = 120 TabIndex = 10 Top = 840 Width = 3255 End 'With that I could call a "for" loop like so: (overly simplified) For X = 0 to 7 frmMain.btnPlayerInfo(X).Text = Player(X).Name & "'s Info" Next X I am looking for an equivalent in VB.Net rather then having to say: ...btnPlayerInfo1.Text = Player(0).Name & "'s Info" ...btnPlayerInfo2.Text = Player(1).Name & "'s Info" ...btnPlayerInfo3.Text = Player(3).Name & "'s Info" and so on for every single value I need to set in the forms... Quote
sjn78 Posted June 6, 2003 Posted June 6, 2003 You would have to create an array of textboxes. Sub CreateArray() Dim btnPlayerInfo() As Button Dim i as Integer = 0 'Needs to start at 0 Dim r as Integer = 7 ' Number of Buttons Dim y as Integer = 25 ' y position of your button...depending on your forms layout Dim x as Integer = 10 ' x position of your button...depending on your forms layout Do while i < r ReDim Preserve btnPlayerInfo(i) btnPlayerInfo(i) = New Button btnPlayerInfo(i).Left = x btnPlayerInfo(i).Top = y btnPlayerInfo(i).Test = "Player " & i & "'s Info" '... '... Any other options to set '... Me.Controls.Add(btnPlayerInfo(i)) i += 1 y += 25 Loop You could do something like that to populate your form with buttons with it loads. Quote
fillibar Posted June 6, 2003 Author Posted June 6, 2003 That will not work. While it will generate the buttons, and give them the correct text, they cannot have WithEvents. Thus they cannot run any code if someone clicks on them, because you cannot make an event handler for them without the WithEvents clause. Quote
sjn78 Posted June 6, 2003 Posted June 6, 2003 Put this line in below the me.controls add part AddHandler btnPlayerInfro(i).Click, AddressOf btnPlayerHandler then create a sub called Sub btnPlayerHandler(ByVal sender As Object, ByVal e As System.EventArgs) Quote
fillibar Posted June 8, 2003 Author Posted June 8, 2003 Thanks! Have events now. You would think there could be an easier way to do this, but this will fit the bill. Quote
Leaders dynamic_sysop Posted June 9, 2003 Leaders Posted June 9, 2003 just a thought but couldnt you try something like this code i bunged together : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Ctrls(0) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Ctrls(1) End Sub Public Sub Ctrls(ByVal i As Integer) Dim btns() As Button = {Button1, Button2} Select Case i Case 0 MessageBox.Show("you clicked button index " & i.ToString()) Case 1 MessageBox.Show("you clicked button index " & i.ToString()) End Select End Sub 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.