Update Form1 from Form2

I am new to VB, and I'm using MS Visual Studio .NET. I have a project with 2 forms, form1 and form2. I am opening form2 from form1, so both forms are open. I am trying to update a list box on form1 by pressing a button on form2. I tried creating a variable of type form1, then updating the list box, but this doesn't seem to work:
tempvar As Form1
tempvar.mylistbox.item.add("fffff")

I also tried directly accessing the list box, just like this:
mylistbox.item.add("fffff")

I thought about creating a new Form1 variable, but I need to keep the same Form1 I already have open, since I have already updated some things on there. Any help would be appreciated.

-Dave
dnk99Asked:
Who is Participating?
 
EnladeConnect With a Mentor Commented:

Then add a public function to form1 and pass it the "ffff" and have that function add it to your listbox.
0
 
EnladeCommented:

how about just form1.mylistbox.item.add("ffff") ?
0
 
dnk99Author Commented:
Yeah, I forgot to mention that I have tried that already. (form1.mylistbox.item.add("ffff") )
It doesn't seem to like the .mylistbox after form1.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
EnladeCommented:

Hmmmmm....it works for me.  Here tell me if this is what you are doing:

http://www.enlade.com/Samples/CrossFormTest.zip

0
 
SRigneyCommented:
The only thing that I can think of is that it's not actually called form1.

In VB objects on a form are public, which means on Form2, it has the ability to modify any control that's on Form1 with Form1.Controlname.

In your case that would be Form1.mylistbox.item.add("ffff")

You can have the code check the name of all of your forms.  In the button on Form2. Run this code

Dim CurrentForm As Form
For Each CurrentForm In Forms
   Debug.Print CurrentForm.Name
Next

Then check your debug window to make sure it says Form1.  If it has another name, then you need to have your code reference the other name.
0
 
dnk99Author Commented:
Enlade, that is exactly what I am trying to do. My first form is called Form1. When I type form1 in form2, the only choices I get for after the . are ActiveForm, ControlCollection, DefaultBackColor, DefaultFont, DefaultForeColor, DockPaddingEdges, DockPaddingEdgesConverter, IsMnemonic, Main, ModifierKeys, MouseButtons, MousePosition, and a bunch of Scroll stuff. Anything else I put, Visual Studio doesn't like.
0
 
SRigneyCommented:
This is a .Net question.

I don't know how I overlooked that.  

Cancel what I just said.  In .Net controls are hidden by default.
The easiest way is to add a public function to your Form1 that Form2 can call

In Form1 add
    Public Sub AddItemToList(ByVal listitem As String)
      mylistbox.item.add(listitem)
    End Sub


Then in Form2 when you click the button you can do Form1.AddItemToList("ffff")
0
 
dnk99Author Commented:
Where should I put that function in Form1? I am putting it right after Public Sub New()...End Sub
and it doesn't seem to be working.
I get an error that points to the line Form1.AddItemToList("ffff") in Form2. The error says:
Reference to a non-shared member requires an object reference

I also tried using the line: Call Form1.AddItemToList("ffff"), but that didn't work either.
0
 
EnladeCommented:

Yea....that was what I said earlier.  Create a public function in Form1 and pass that function the string you want to add to the listbox.  Then code that function to actually add it to the listbox control.
0
 
SRigneyCommented:
When you open form2 to you asssign it an owner or parent?

Enlade, you did say that earlier.  I'm a C# guy more than a VB.Net guy.  Any thoughts on how he can get the reference to Form1?
0
 
Bob LearnedCommented:
Pass a reference to Form1 to Form2:

Form2:
   Private m_CallingForm As Form1    'Module-level reference to main calling form.

   Public WriteOnly Property CallingForm() As Form1
      Set(ByVal Value As Form1)
         m_CallingForm = Value
      End Set
   End Property

Within form2:

   m_CallingForm.AddItemToList("Item")
0
 
Bob LearnedCommented:
BTW you might want to ask this question in the .NET TA next time.
0
 
dnk99Author Commented:
SRigney, I am not assigning an owner or a parent to form2. Not sure how to do this.

TheLearnedOne, I tried adding all that, but I still get the error: "Object reference not set to an instance of an object" when I click on the button in Form2 to add "fffff" to the listbox in Form1.
0
 
Bob LearnedCommented:
I can't see your code, so I can't see where you could have gone wrong.
0
 
SRigneyCommented:
Try passing ByRef instead of ByVal
0
 
lttia1Commented:
How abt this:
In this case, user cannot perform any operation on form 1 until form 2 is closed and the result will only be shown on form 1 after form 2 is closed.

in form 1:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm2 As New Form2
    Dim Index As Integer

    If frm2.ShowDialog() = DialogResult.OK Then
     'Add the code below if public variable is used in form 2
     ListBox1.Items.Add(frm2.variable)
     
     'Add the code below if listbox is used in form2
      If frm2.ListBox1.Items.Count > 0 Then
        For Index = 0 To frm2.ListBox1.Items.Count - 1
          ListBox1.Items.Add(frm2.ListBox1.Items.Item(Index))
        Next
      End If

    End If

    frm2.Dispose()
End Sub

In form 2:

1) Create a listbox to add more than 1 items at a time to form 1
OR
create a public variable if there is only 1 item.

2) Create a add button if listbox is used.

3) Create a ok button to confirm all items in form2 listbox(if used) to be added to form1 listbox . Otherwise, u can use it to assign the item to be added to the public variable.
Set the DialogResult to OK

4) Create a cancel button to cancel all the items in form2 listbox(if used).
Set the DialogResult to CANCEL

Please note that upon clicking buttons mentioned in pt 3 and 4, form2 will close.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    Dim frm2 As New Form2

    'If Listbox is not used, assign the item to be added to form1 to the public variable here

    frm2.AcceptButton = btnOk
  End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
   'Add the code for items to add to form2 listbox here
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Dim frm2 As New Form2

    frm2.CancelButton = btnOk
End Sub
0
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.