Form2-Textbox, to, Form1 -Listbox ( 2003)

Hi Experts

I have a Two Forms. Form1 has a Listbox & a 'users' Button. When button clicked, it opens Form2.

Form2 has a Listview at the top of the page followed by button1... when I select the row in the list view, I click button1 and it imports the selected item into Textbox1 & 2 below.

*Form2 diagram*
|                     column A  |  column B                      |
|                          data A    |  data B                        |
|                                                                            |

 ------------    -------------
 |  Textbox1  |    |   Textbox2    |
 ------------    -------------

So Far so good. (I've got this far)

What I want to do is click Button2 to transfer data from Textbox 1 & 2 and populate the Listbox in Form1 whilst closing form 2. I'be tried a few things, but get errors.

Any ideas?

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
In my opinion, the simplest way would be to open Form2 via ShowDialog()...

In Form1:

    Dim f2 As New Form2
    ' setup "f2" as needed before display...
    f2.ShowDialog() ' code STOPS here until "f2" is hidden/closed
    ' now get some info DIRECTLY from "f2"...

In Button2 on Form2:

    Me.DialogResult = DialogResult.Ok ' this causes the form to HIDE (not Dispose) and code in Form1 continues

It is possible to solve this in many ways, but I suggest that this one as the simpler:

Step 1 : Go to the constructor of Form2 and declare a parameter of type Form1 e.g.
            You should also declare a private Form1 field in your Form2
             Private myParent as Form1
             Public Sub New(ByVal parentForm As Form1)

Step 2 : Declare a public Sub in Form1 with two arguments. This sub should add to the
            listbox the two arguments as an item. e.g.
            Public Sub AddAnItem(first as String, second as String)
                 ... add your code here to add the new item ...

Step 3 : In the Button2 click event write:
            myParent.AddAnItem(TextBox1.Text, TextBox2.Text)

Step 4 : Finally change the code in Form1 that opens the Form2 from something like new Form2() that you have now
            to new Form2(Me)

This should do the job.

Alternatively you can go to the graphical editor of Form1 and change the Modifiers property from Private to Public for the
listbox in Form1, so you can access it from within Form2, BUT that would only merge steps 2 & 3 from the above procedure
which is not much, and is a bad programming practice.

If you have any question or need more (specific) help, please do not hesitate to come back with more...


A simple way would be, for example if you were using C# instead, is to save the contents from Form2 into a text file.  And then have code in Form1 to read that text file and put the contents into the listbox.  

Not the most pretty way of doing it, but it's a solution if stuck.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Regarding angus_young_acdc solution my objection is
that in this case you would have to terminate the
Form2 in order to execute the code in Form1. In addition
this method has many problems regarding the syncronization
of the access to the file in case a problem with the program
comes up (e.g. a program crash).

I did say it wasn't a pretty way of doing it and that I was merely offering a solution if Roberto was stuck.   It does however offer a little backup should the program crash for another reason.  The data would be saved, could be opened again whereas if it was just stored in variables etc then it would be gone.
I don't mean to be a pain but..

Again the same problem arises as in case that angus_young_acdc proposes.
Each time you open Form2 you can add only one entry in the listview of Form1.
However using the Button2 you can add many entries with one "session" of

This is the only difference. If this is not a requirement, then Idle_Mind's method
is equivalent (with less code) to the one I suggested. However I think that Roberto
had such a requirement in mind when he added the Button2 in Form2.

Mike TomlinsonMiddle School Assistant TeacherCommented:
The author said:

    "What I want to do is click Button2 to transfer data from Textbox 1 & 2 and populate the Listbox in Form1 whilst closing form 2."

He says "whilst closing form 2"...thus I interpreted this as Form2 being used only ONCE per "session".

But you never know if what was said is actually what was intended.  =)

RobertoFreemanoAuthor Commented:
Thanks All,

Cheers Idle Mind.... 1 more question that'll complete my project.

Q) if I have another button on Form 1, if clicked opens form 5 and displays the contents of Listbox1 on Form 1 with inserted text... is that possible?


Form 5
Textbox1.text = "The current user is? " & Form1(Listbox1.selecteditem(1) & "text continued..." & VbCtrl & "Location? " &  Form1(Listbox1.selecteditem(2) & "text continued..."...

That's the idea?

Mike TomlinsonMiddle School Assistant TeacherCommented:
With regard to your last question, you don't need any special coding to do that as this can all be accomplished from within Form1 when you create your instance of Form5:

    Dim f5 As New Form5

    ' setup "f5" as necessary using info from THIS form...
    f5.TextBox1.Text = "The current user is? " & Me.ListBox1.SelectedItem(1) & "text continued..." & VbCrLf & ...

    f5.Show() ' or f5.ShowDialog()...
RobertoFreemanoAuthor Commented:
Hi Idle Mind

I get an error when I run the above script...

Error Message: 'An unhandled exception of type 'System.IndexOutofRangeException' occurred in' Addition information: Index was outside the bounds of the array.

Any ideas?
Mike TomlinsonMiddle School Assistant TeacherCommented:
That was not meant to be exact code silly goose...

I thought you were asking how to pass data from Form1 to Form5 for display.  That is the intent of the code.

The gist of it is:

    Dim f5 As New Form5
    f5.TextBox1.Text = Me.something.value

Do you need specific help getting something in particular off of Form1?  If so, then what?
RobertoFreemanoAuthor Commented:
Hi Idle_Mind,

Silly me...

In a nutshell... I have a Listbox on form1. If I click Button 1=Form2 & I select options that add items back to Listbox=Form1 and continuous with other Buttons & Forms.

I want to output the final collection of items from Listbox1 on Form1 to... Final Form with a Textbox on it so that I can Copy all the data and Paste it somewhere else.

RobertoFreemanoAuthor Commented:
I tried adding -
'textBox1.Text = ListBox1.GetItemText(ListBox1.SelectedItem' & it does display text in the final textbox on the Final Form... but it looks like code rather than the contents of Listbox1 from Form1.

Mike TomlinsonMiddle School Assistant TeacherCommented:
So basically you need ALL the items from your ListBox in a String...with each item seperated by vbCrLf?
RobertoFreemanoAuthor Commented:
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can do something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As String = GetListBoxItems(ListBox1)
    End Sub

    Private Function GetListBoxItems(ByVal lb As ListBox) As String
        Dim sb As New System.Text.StringBuilder
        For Each item As String In lb.Items
            sb.Append(item & vbCrLf)
        Return sb.ToString
    End Function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.