Solved

Update Form1 from Form2

Posted on 2003-11-18
16
1,276 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:dnk99
  • 4
  • 4
  • 4
  • +2
16 Comments
 
LVL 7

Expert Comment

by:Enlade
Comment Utility

how about just form1.mylistbox.item.add("ffff") ?
0
 

Author Comment

by:dnk99
Comment Utility
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
 
LVL 7

Accepted Solution

by:
Enlade earned 100 total points
Comment Utility

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

Expert Comment

by:Enlade
Comment Utility

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

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

0
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
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
 

Author Comment

by:dnk99
Comment Utility
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
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
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
 

Author Comment

by:dnk99
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Expert Comment

by:Enlade
Comment Utility

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
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
BTW you might want to ask this question in the .NET TA next time.
0
 

Author Comment

by:dnk99
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I can't see your code, so I can't see where you could have gone wrong.
0
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
Try passing ByRef instead of ByVal
0
 
LVL 2

Expert Comment

by:lttia1
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now