can't add items to a listbox

sgtmoody

Member
Joined
Jul 10, 2007
Messages
5
Programming Experience
1-3
ok so I'm working on a multi form program. form1 is the main form, from there I call form2 which acquires a name and phone number from the user and adds it to a file. then it adds the name of the person to a listbox on form1.
VB.NET:
//calls form2 to get user input
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New form2
        frm.Show()
    End Sub
VB.NET:
//code for form2 when user inputs data and clicks add button
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim Name As String = txtName.Text
        Dim Number As String = txtNumber.Text
        form1.lstNames.Items.Add(txtName.Text)//doesn't seem to work
        Dim oWrite As System.IO.StreamWriter
        Dim fileName As String = String.Concat(My.Application.Info.DirectoryPath, "/data.txt")

        oWrite = IO.File.AppendText(fileName)
        oWrite.WriteLine(Name)
        oWrite.WriteLine(Number)
        oWrite.Close()

        Me.Close()
    End Sub
problem is when adding to the listbox from form2 the name doesn't show up. When I start up the application the data is read in from the file and the names are added. so I know the write to file works because if I restart the application the newly added name shows up in the listbox.

I tried just getting the data from the user and sending it to a function on form1 and adding to the listbox and writing to the file there and I get the same problem.

the only thing I got to work was just getting the data from 2 input boxes in a sub on form1 and then writing to the file and adding to the listbox with out ever using form2.
VB.NET:
public sub getinput()
name = inputbox
number = inputbox

//write stuff to file
lstNames.items.add(name)
the name got added to the listbox and the data got written to file. this works just fine but I would prefer to use form2 because you can do allot more with the form then you can with inputboxes also it looks better.

if anyone sees anything wrong any help would be much appreciated I have been stuck on this problem for a while and its boggling my mind. sometimes another set or eyes helps.
 
Back
Top