Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update Form1 from Form2

Posted on 2003-11-18
16
Medium Priority
?
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +2
16 Comments
 
LVL 7

Expert Comment

by:Enlade
ID: 9776677

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

Author Comment

by:dnk99
ID: 9776772
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 400 total points
ID: 9776803

Then add a public function to form1 and pass it the "ffff" and have that function add it to your listbox.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:Enlade
ID: 9776826

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
ID: 9781258
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
ID: 9782685
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
ID: 9783219
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
ID: 9783476
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
 
LVL 7

Expert Comment

by:Enlade
ID: 9783533

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
ID: 9787175
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
ID: 9789716
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
ID: 9789731
BTW you might want to ask this question in the .NET TA next time.
0
 

Author Comment

by:dnk99
ID: 9791333
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
ID: 9791381
I can't see your code, so I can't see where you could have gone wrong.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9798293
Try passing ByRef instead of ByVal
0
 
LVL 2

Expert Comment

by:lttia1
ID: 9807958
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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