2017-08-10 2 views
0

J'ai donc deux formulaires, (Form1 et Form2). Form2 demande combien de lignes d'un panneau particluar (TxtBoxPanel) doivent être générées dans Form1. Le panneau contiendra trois boîtes, donc si l'utilisateur dit de générer 5 lignes dans Form1, il y aura 5 panneaux chacun avec 3 zones de texte.Accéder aux contrôles effectués à l'exécution sous deux formes différentes

Form2 apparaît comme indiqué dans l'image: Form2

Ci-dessous le code Form2:

Public Class Form2 
Public Rows As Integer 

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Ok.Click 


    Rows = RowNum.Text 'Row num being box to enter no. of rows 



    For index = 1 To Rows 

     Dim TxtBoxPanel(index) 'Control Array 

     Dim LeftBox(index) 'Control Array 
     Dim CenterBox(index) 'Control Array 
     Dim RightBox(index) 'Control Array 
     Dim YAxis As Integer ' for adding TxtBoxPanel in new row 

     'adding the textbox panel 

     TxtBoxPanel(index) = New Panel 

     Form1.MajorPanel.Controls.Add(TxtBoxPanel(index)) 'referring to form1 as panel needed in form1 
     TxtBoxPanel(index).Name = ("txtBoxPanel" & index) 
     TxtBoxPanel(index).Size = New Size(610, 32) 
     YAxis += 32 
     TxtBoxPanel(index).Location = New Point(3, YAxis) 

     'adding left box 
     LeftBox(index) = New TextBox 

     TxtBoxPanel(index).Controls.Add(LeftBox(index)) 
     LeftBox(index).Name = ("LeftBox" & index) 



     LeftBox(index).Text = (index) 
     LeftBox(index).Size = New Size(100, 20) 
     LeftBox(index).Location = New Point(3, 3) 

     'adding center box 
     CenterBox(index) = New TextBox 

     TxtBoxPanel(index).Controls.Add(CenterBox(index)) 
     CenterBox(index).Name = ("CenterBox" & index) 
     CenterBox(index).Text = (index) 
     CenterBox(index).Size = New Size(100, 20) 
     CenterBox(index).Location = New Point(258, 3) 

     'adding right box 
     RightBox(index) = New TextBox 

     TxtBoxPanel(index).Controls.Add(RightBox(index)) 
     RightBox(index).Name = ("RightBox" & index) 
     RightBox(index).Size = New Size(100, 20) 
     RightBox(index).Text = (index) 
     RightBox(index).Location = New Point(495, 3) 



    Next index 

    Close() 'After generation of controls, Form2 closes 

End Sub 



End Sub 
End Class 

Les boîtes génèrent bien dans Form1 comme indiqué dans l'image ci-dessous: Controls in Form1

Maintenant, je voudrais parcourir chaque instance de "LeftBox" afin que je puisse imprimer la propriété "Name" de chaque instance lorsque je clique sur le bouton "Message" sur Form1. Cependant, le code pour générer LeftBox est situé dans Form2, et je trouve qu'il est difficile de le référencer dans Form1.

S'il vous plaît noter que je générer les commandes à l'aide des tableaux comme je l'ai lu quelque part, ils le rendent plus facile à référencer les contrôles par rapport à l'utilisation Controls.Find

Ainsi, la question est, comment puis-je faire référence aux contrôles dans Form1 après sont générés lors de l'exécution?

Voilà comment je wtried référence au LeftBox, par exemple, plus tôt:

' FINDING CONTROLS PROGRAMATICALLY 


    For Index = 1 To Form2.Rows 

     Dim LBox As TextBox 

     For Each LBox In Me.Controls.Find("LeftBox" & Index, True) 
      If (LBox.Name.Contains("LeftBox") = True) Then 

       MsgBox(LBox.Text, MsgBoxStyle.Information, "Testing") 

      Else 
       MsgBox("There's a problem", MsgBoxStyle.Information, "Testing") 


      End If 

     Next 

    Next 

Merci.

+0

'Form1' possède une collection' Controls' qui contiendra tous vos contrôles. Avez-vous vérifié cela? Affiche le code que vous avez utilisé pour accéder aux contrôles. –

+0

Veuillez ne pas mettre de code dans un commentaire. Au lieu de cela, _edit_ votre question et ajoutez le code ici. Je suis un peu confus. Je pensais que tous vos contrôles 'TxtBoxPanel' étaient sur Form1'. Le code que vous avez ajouté semble les chercher dans 'Form2'? –

+0

Désolé pour ça @Chris, je viens d'éditer la question pour inclure le code. Le TxtBoxPanel génère sur Form1, mais le code pour générer le panneau sur form1 est sur form2. Voilà pourquoi je pensais que je devais faire référence à form2 puisque le code est effectivement là – Amin84

Répondre

0

Donc finalement après avoir essayé pendant des heures, j'ai utilisé List pour stocker chaque instance des zones de texte, ce qui m'a permis de les référencer plus tard. Voici le code ci-dessous:

Public Class Form2 
Public Rows As Integer 

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles OK.Click 


    Rows = RowNum.Text 
    Vars.Indx = Rows 'for storing the value of rows globally 


    For index = 1 To Rows 

     Dim TxtBoxPanel 
     Dim LeftBox 
     Dim CenterBox 
     Dim RightBox 
     Dim YAxis As Integer ' for adding TxtBoxPanel in new row 

     'adding the textbox panel 

     TxtBoxPanel = New Panel 

     Form1.Controls.Add(TxtBoxPanel) 
     TxtBoxPanel.Name = ("txtBoxPanel" & index) 
     TxtBoxPanel.Size = New Size(610, 32) 
     YAxis += 30 
     TxtBoxPanel.Location = New Point(24, YAxis) 

     'adding left box 
     LeftBox = New TextBox 

     TxtBoxPanel.Controls.Add(LeftBox) 
     LeftBox.Name = ("LeftBox" & index) 



     LeftBox.Text = (index) 
     LeftBox.Size = New Size(100, 20) 
     LeftBox.Location = New Point(3, 3) 

     LeftList.Add(LeftBox) 'inserts LeftBox to list 
     'adding center box 
     CenterBox = New TextBox 

     TxtBoxPanel.Controls.Add(CenterBox) 
     CenterBox.Name = ("CenterBox" & index) 
     CenterBox.Text = index 
     CenterBox.Size = New Size(100, 20) 
     CenterBox.Location = New Point(258, 3) 
     CenterList.Add(CenterBox) 'inserts centerbox to list 
     'adding right box 
     RightBox = New TextBox 

     TxtBoxPanel.Controls.Add(RightBox) 
     RightBox.Name = ("RightBox" & index) 
     RightBox.Size = New Size(100, 20) 
     RightBox.Text = index 
     RightBox.Location = New Point(495, 3) 

     RightList.Add(RightBox) 'adds rightbox to list 

    Next index 



    Close() 'After generation of controls, Form2 closes 

End Sub 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles RowNum.TextChanged 

End Sub 
End Class 

Inorder pour référencer les zones de texte dans Form1, je l'avais déclaré la liste dans un module appelé Vars comme indiqué ci-dessous:

Module Vars 
'for storing variables needed globally 
'whatever needs to be "connected" to different forms, store it here 

Public Indx As Integer 
Public CenterList As New List(Of TextBox) 
Public RightList As New List(Of TextBox) 
Public LeftList As New List(Of TextBox) 

End Module 

Puis dans Form1, c'est ce que je faisais :

Public Class Form1 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Form2.Show() 
End Sub 



Private Sub Message_Click(sender As Object, e As EventArgs) Handles Message.Click 


    For index = 0 To (Vars.Indx - 1) 'loop through leftbox 

     MsgBox(LeftList(index).Text, MsgBoxStyle.Information, "INFO") 


    Next 



End Sub 
End Class 

Son fonctionnement mais je me demande toujours si ce code est vraiment efficace?