473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Nov 21 '05 #1
8 1260
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

"johnb41" <or****@informatik.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 21 '05 #2
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
Nov 21 '05 #3
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

"johnb41" <or****@informatik.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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

Nov 21 '05 #4
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

Nov 21 '05 #5
"johnb41" <or****@informatik.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
.. . .
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.
Nov 21 '05 #6
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

Nov 21 '05 #7
On 26/05/2005 johnb41 wrote:
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

--
DaveG - Learning VB.Net slowly.
Skøyen - Oslo - Norway
Nov 21 '05 #8
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

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: mohd hisham | last post by:
Write a C program to construct a queue of integers and to perform the following operations on it: a. insert b. delete c. display The program should print appropriate messages for stack overflow...
1
by: Bob Rock | last post by:
Hello, always having to validate an XML stream against a XSD may add up an important overhead. My XMLs are usually the result of serializing a class instance and often in my applications what I...
5
by: MrNobody | last post by:
I am using the no-arg ShowDialog() method hoping that the window would not be modal to any other window like the other ShowDialog(IWin32Window) method does, but when this opens it somehow becomes...
7
by: Mike Fellows | last post by:
Below is my code that is carried out on my dataset, datagrid etc... Im trying to get column0 "Date & Time" to show date and time, not just date ive read some stuff posted by Dmitriy Lapshin on...
1
by: monomaniac21 | last post by:
hi all! how can i enter html and php into a db and display this within a TEXTAREA without it being processed as code? do i need to replace characters like <? with something else like say <..? or...
7
by: Frederick Williams | last post by:
Does anybody know where I can download Borland C++ 4.52 from? Some years ago it was available "free" on a magazine cover. So I'm hoping that it's available from somewhere without upsetting the...
4
by: Frugoo Scape | last post by:
Hi, I know the basic about java and i have been trying to edit a program that is mad in java. If you help me ill make you admin in the game. --- The problem with this app is that it does not...
5
by: mohammaditraders | last post by:
Question # 1 Write a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions...
18
by: David Moss | last post by:
Hi, I want to manage and control access to several important attributes in a class and override the behaviour of some of them in various subclasses. Below is a stripped version of how I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.