I use the code

Dim Selvk As New Selvkost2
        Selvk.PassedText = TextBox29.Text
                Selvk.Show()

in mainform and the code

Private _passedText As String
    
    Public Property [PassedText]() As String
        Get
            Return _passedText
        End Get
        Set(ByVal Value As String)
            _passedText = Value
        End Set
    End Property

in my second form and the text passes. but I need to pass values from 20 textboxes.
do I have to copy the code in my second form 20 times an declare passedtext2 and so on or is there an easier way to do this?

Recommended Answers

All 4 Replies

is there an easier way to do this?

Yes there is. Use a special naming with textboxes, store strings to an array and pass the array as form's property

Here's the code to find those 20 textboxes (named TextBox0, TextBox1,..., TextBox19):

Dim TextToPass(20) As String ' Array of 20 strings 0..19
Dim TempStr As String
Dim TempInt As Integer

For Each c As Control In Me.Controls
    ' Is it textbox?
    If TypeOf (c) Is TextBox Then
        ' Control is a textbox. Check it's name (TextBox0, TextBox1,..., TextBox19)
        If CType(c, TextBox).Name.StartsWith("TextBox") Then
            ' Naming seems to be ok. Is there a number 0..19?
            If CType(c, TextBox).Name.Length > "TextBox".Length Then
                ' Take ending string
                TempStr = CType(c, TextBox).Name.Substring("TextBox".Length, CType(c, TextBox).Name.Length - "TextBox".Length)
                '  and try to convert it to integer
                If Integer.TryParse(TempStr, TempInt) Then
                    ' It is integer, is it between 0-19?
                    If TempInt >= 0 AndAlso TempInt <= 19 Then
                        ' Now assign the textboxes value to array with index TempInt
                        TextToPass(TempInt) = CType(c, TextBox).Text
                    End If
                End If
            End If
        End If
    End If
Next

' At this point TextToPass contains text's from the textboxes. Pass the array to form's property

and this is the property for the second form. Now it passes the array of string

Private _passedText(20) As String

Public Property [PassedText]() As String()
    Get
        Return _passedText
    End Get
    Set(ByVal Value As String())
        _passedText = Value
    End Set
End Property

HTH

But how do I get the text into the textboxes in my second form?

textbox1.text= passedtext(0)
textbox2.text= passedtext(1)

or????

But how do I get the text into the textboxes in my second form?

textbox1.text= passedtext(0)
textbox2.text= passedtext(1)

or????

I tried it like that but it did not work,,why?

I tried it like that but it did not work,,why?

That's totally impossible to say because you didn't post any error messages or explained what does "it did not work" mean in plain English!!!

Anyway. The code does work. Only small mistake I did was that Dim TextToPass(20) As String ' Array of 20 strings 0..19 actually allocates 21 strings (indices 0..20), but that shouldn't make any difference.

I'll repost the code and also show, how you can re-use the same code to fill the textboxes in the second form.

Create a new project, add two forms (Form1 as a main form and Form2). Add one button and a few textboxes to both forms. You don't need all 20 textboxes for testing. When you add the form's first textbox, rename it to TextBox0. Remember to do the same thing in the second form too. Then paste the codes below to the forms.
Code for Form1:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TextToPass(20) As String ' Array of 21 strings 0..20
        Dim TempStr As String
        Dim TempInt As Integer

        For Each c As Control In Me.Controls
            ' Is it textbox?
            If TypeOf (c) Is TextBox Then
                ' Control is a textbox. Check it's name (TextBox0, TextBox1,..., TextBox19)
                If CType(c, TextBox).Name.StartsWith("TextBox") Then
                    ' Naming seems to be ok. Is there a number 0..19?
                    If CType(c, TextBox).Name.Length > "TextBox".Length Then
                        ' Take ending string
                        TempStr = CType(c, TextBox).Name.Substring("TextBox".Length, CType(c, TextBox).Name.Length - "TextBox".Length)
                        '  and try to convert it to integer
                        If Integer.TryParse(TempStr, TempInt) Then
                            ' It is integer, is it between 0-19?
                            If TempInt >= 0 AndAlso TempInt <= 19 Then
                                ' Now assign the textboxes value to array with index TempInt
                                TextToPass(TempInt) = CType(c, TextBox).Text
                            End If
                        End If
                    End If
                End If
            End If
        Next

        ' At this point TextToPass contains text's from the textboxes. Pass the array to form's property
        Dim oForm As Form2

        oForm = New Form2
        oForm.PassedText = TextToPass
        oForm.ShowDialog()

    End Sub

End Class

Code for Form2:

Option Explicit On
Option Strict On

Public Class Form2

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TempStr As String
        Dim TempInt As Integer

        For Each c As Control In Me.Controls
            ' Is it textbox?
            If TypeOf (c) Is TextBox Then
                ' Control is a textbox. Check it's name (TextBox0, TextBox1,..., TextBox19)
                If CType(c, TextBox).Name.StartsWith("TextBox") Then
                    ' Naming seems to be ok. Is there a number 0..19?
                    If CType(c, TextBox).Name.Length > "TextBox".Length Then
                        ' Take ending string
                        TempStr = CType(c, TextBox).Name.Substring("TextBox".Length, CType(c, TextBox).Name.Length - "TextBox".Length)
                        '  and try to convert it to integer
                        If Integer.TryParse(TempStr, TempInt) Then
                            ' It is integer, is it between 0-19?
                            If TempInt >= 0 AndAlso TempInt <= 19 Then
                                ' Now assign the value from the array with index TempInt to textbox
                                CType(c, TextBox).Text = _passedText(TempInt) ' This assignment is "swapped" here in the second form compared to code in Form1
                            End If
                        End If
                    End If
                End If
            End If
        Next
    End Sub

    Private _passedText(20) As String  ' Array of 21 strings 0..20

    Public Property [PassedText]() As String()
        Get
            Return _passedText
        End Get
        Set(ByVal Value As String())
            _passedText = Value
        End Set
    End Property

End Class

What you might have missed from my first post was that correct naming of the textboxes.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.