Hello,

Please tell me how to add a string item to a text box, returned from another form that has a list box?

I have two forms.

on the first form I have a button which calls another form that has a List Box.
When I select the value from the list box and click on the apply button then the value should be passed to the first form that will also have a text box.

The text box should contain the value of list box.

Please help me in this.

Dani AI

Generated

’s approach (writing the ListBox value straight into Form1’s TextBox and toggling visibility) will work when the Form1 instance is alive, but it tightly couples the forms and can cause problems if Form1 has been closed/disposed. If the intention is to set the text without showing Form1 again, note two important facts: a hidden form keeps control values (setting TextBox.Text works while the form is hidden), and a safer pattern is to return the selected string from Form2 and let Form1 update its own UI.

Recommended pattern — modal dialog with a read-only property on Form2 (Form1 pulls the value after the dialog closes):

' Form2: expose the selection
Public ReadOnly Property SelectedText As String
    Get
        Return If(ListBox1.SelectedItem IsNot Nothing, ListBox1.SelectedItem.ToString(), String.Empty)
    End Get
End Property

Private Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApply.Click
    Me.DialogResult = DialogResult.OK
    Me.Close()
End Sub
' Form1: open Form2 and read the property (Form1 updates its own TextBox)
Using f As New Form2()
    If f.ShowDialog() = DialogResult.OK Then
        TextBox1.Text = f.SelectedText
    End If
End Using

Alternative — event/delegate so Form2 raises the chosen value and Form1 subscribes (keeps forms decoupled and works if Form1 stays loaded):

' Form2
Public Event ItemChosen(ByVal value As String)
' raise ItemChosen(ListBox1.SelectedItem.ToString()) on apply
' Form1: AddHandler f.ItemChosen, AddressOf OnItemChosen
Private Sub OnItemChosen(ByVal val As String)
    TextBox1.Text = val
End Sub

Notes and troubleshooting: always check SelectedItem for Nothing before calling ToString; avoid directly manipulating another form’s controls from a secondary form (prefer properties/events); if forms run on different threads Invoke is required; if Form1 was closed/disposed a live instance must be recreated or the value stored centrally (model/service) before updating UI.

Recommended Answers

All 3 Replies

public class form1
'Put textbox and button1 on form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Visible = True
    End Sub
End class


Public Class Form2
'put listbox1,button1 on form 2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("TEST")
        ListBox1.Items.Add("TEST1")
        ListBox1.Items.Add("TEST2")
    End Sub

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

        Form1.TextBox1.Text = ListBox1.SelectedItem
        Me.Visible = False
        Form1.Visible = True

    End Sub
End Class
public class form1
'Put textbox and button1 on form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Visible = True
    End Sub
End class


Public Class Form2
'put listbox1,button1 on form 2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("TEST")
        ListBox1.Items.Add("TEST1")
        ListBox1.Items.Add("TEST2")
    End Sub

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

        Form1.TextBox1.Text = ListBox1.SelectedItem
        Me.Visible = False
        Form1.Visible = True

    End Sub
End Class

this has helped me a lot....
Thank u so much

but can u tell me that without again opening the form1 how can the value be set

Do

form1.visible = false
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.