This must be easy... i just can't do it!!

J

johnb41

I can't believe i'm stumbling on such a seemingly minor task. I have a
major mental block here.

I have boiled down the problem to it's most basic form:

I have form1 and form2. Form1 has a blank label. Form2 has a button.
When clicked, i want to add hard coded text to Form1.

Code for form1: (simply opens up form2)

Private Sub Button1_Click(...) Handles Button1.Click
Dim form2 As New Form2
form2.Show()
End Sub


Code for form2: (writes text to form1)

Dim form1 As New Form1
Private Sub Button1_Click(...) Handles Button1.Click
form1.Label1.Text = "Sample text"
End Sub

Nothing happens! In my real life app, i want to display an image in a
picturebox on a different form from the currently active form.

Help anyone?

Thanks very much!
John
 
C

Crouchie1998

Here's one way you could tackle it:

Start a new Windows application
Add a button & label to form 1
Add a new form
Add a button to that form (form 2)
Add a textbox to form 2

Double-click the btton on form 2 & type:

Me.Close()

Double-click the button on form 1 & type:

Dim frm As New Form2
frm.ShowDialog()
Label1.Text = frm.TextBox1.Text
frm.Dispose()

That's it. Run the project, click the button on form 1, type something into
the textbox & click the button on form2. The text you just typed will appear
in the label.

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
G

Gerald Hernandez

Take note of the line:
Dim form1 As New Form1
Your button is setting the text on the "New" Form1, not the one that
launched it.

The way I would deal with this scenario is to add a method on Form1 for
setting the value of the label. Lets call it SetLabelCaption.
On Form2, I would set the ParentForm to be a reference to the form that
spawned it, or add a strongly typed property for this, such as
MyParentForm1.

Then make some changes, like:
Code for form1: (simply opens up form2)

Private Sub Button1_Click(...) Handles Button1.Click
Dim form2 As New Form2
form2.MyParentForm = Me
form2.Show()
End Sub

Code for form2: (writes text to form1)
Private m_ParentForm as Form1

Property MyParentForm() As Form1
Get
Return m_MyParentForm
End Get
Set(ByVal Value As Form1)
m_MyParentForm = Value
End Set
End Property

Private Sub Button1_Click(...) Handles Button1.Click
m_MyParentForm.SetLabelCaption ("Sample text")
End Sub

Or something like that. Of course, there are a number of other ways to deal
with this as well. But the "proper" one depends a whole lot on your overall
design and needs.

Gerald
 
R

Richard Myers

Hi John

Yes. Think of it in terms of memory space. Your form1 that opens form 2 is
different from the "instance" of form1 you create inside of form2.i.e you
actually have 3 memory spaces in play 2 * Form1's and 1* Form 2.

You actually have 2 instance of form 1 running and the label would be
changed as anticipated but because you haven't shown the second instance
(the form1 instance in form 2) you dont see the change. You could use
shared properties to achieve the result or pass in a reference to form1
from the starting form1 to form2 or cast from me.owner or me.parent
depending on the relationship between form1 and form2 instances.

On button click

Dim frm as new form2(Me)


....Form 2 constructor
Sub New(frm1 as from1)
Me.form1 = frm1
End Sub

A more advanced implementation would be to use events to signal form1 to
change it picture box. This has the advantage of form2 not having to keep
track of a form 1 instead just raising an event in response to some action
that listeners (form1) can the handle.

Richard
 
J

johnb41

Wow, so I guess it's not an obvious thing to do. I was embarrased to
ask the question!

Thanks everyone for your help! Gerald, your example made the most
sense to me. I was able to take your code and make it work (w/ a
little tweaking)! I just now have to adapt it for displaying an image.
Thanks again!

John
 
P

Phill. W

.. . .
Dim form1 As New Form1
Private Sub Button1_Click(...) Handles Button1.Click
form1.Label1.Text = "Sample text"
End Sub

Nothing happens!

Oh Yes, it does!
Dim form1 As New Form1

Creates a *new* instance of your Form1 class, but it's not visible.
form1.Label1.Text = "Sample text"
Sets the text of Label1 on the *new* instance of your form.

Since you never /show/ this instance, nothing "appears" to happen.


In order for Form2 to be able to manipulate Form1, it *must*
have an object reference to it, as in :

[Form1.vb]
Private Sub Button1_Click(...) Handles Button1.Click
Dim form2 As New Form2
form2.Caller = Me
form2.Show()
End Sub

[Form2.vb]
Public WriteOnly Property Caller() as Form1
Set( Value as Form1 )
m_oCaller = Value
End Set
End Property

Private m_oCaller as Form1 = Nothing

Private Sub Button1_Click(...) Handles Button1.Click
m_oCaller.Label1.Text = "Sample text"
End Sub

HTH,
Phill W.
 
J

johnb41

Thanks Phill,

Your reply was very similar to Geralds. I learned a WHOLE lot from
posting this question. I find I use this technique all the time now!
:)

John
 
D

DaveG

I can't believe i'm stumbling on such a seemingly minor task. I have
a major mental block here.

I have boiled down the problem to it's most basic form:

I have form1 and form2. Form1 has a blank label. Form2 has a button.
When clicked, i want to add hard coded text to Form1.

Code for form1: (simply opens up form2)

Private Sub Button1_Click(...) Handles Button1.Click
Dim form2 As New Form2
form2.Show()
End Sub


Code for form2: (writes text to form1)

Dim form1 As New Form1
Private Sub Button1_Click(...) Handles Button1.Click
form1.Label1.Text = "Sample text"
End Sub

Nothing happens! In my real life app, i want to display an image in a
picturebox on a different form from the currently active form.

Help anyone?

Thanks very much!
John

I asked the same question some time ago and Cor posted this answer for
me, works great for all I'v done til now

Dave,

There are many roads that goes to Rome

Your mainform
\\\
Dim frm As New Form2
frm.Owner = Me
frm.Show()
///
You form where you want to use it.
\\\
MessageBox.Show(DirectCast(Me.Owner, Form1).TextBox1.Text)
///
Assuming that you have a textbox1 on form1

I hope this helps,

Cor
 
J

johnb41

Thanks Dave. That is a good technique. Alot less code that how i'm
doing it now!

I found another way to do it, but it's probably not the most efficient
of computer resources:

In a module, create a public variable:
Public MainForm as Form1

Then in Form1, do this:
MainForm = me

Now you can access Form1 anywhere.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top