473,396 Members | 2,068 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,396 software developers and data experts.

Passing Variable Value

Hi All,
I am new to C#, and am having trouble passing the value of a variable
from one windows form to another.

Can anyone point me in the right direction.

Any help here would be greatly appreciated.

Thanks
David
Nov 15 '05 #1
3 1857
More detail would be good... But I'll use my intuition;

Maybe you have a TextBox on FormA called textBox1 and you want to set the
textof a label on FormB (called label1) to the text of textBox1.

Because FormA.textBox1 is private, FormB can't access it by simply
referencing FormA.textBox1.Text;

If this where the case, you'd might want to create a public property for
textBox1.Text that looks like this;

// class FormA
public string TextOnFormA
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = (string)value;
}
}
//

Then FormB could reference (get and set) the text of FormA.textBox1 like
this;

// class FormB
this.label1.Text = FormA.TextOnFormA;
//

Of course you could also just make textBox1 public, but that's not really
the "right way".

Sound close to what you want?
Josh
Microsoft.com Tools
"David Bacon" <dm*******@hotmail.com> wrote in message
news:ub**************@tk2msftngp13.phx.gbl...
Hi All,
I am new to C#, and am having trouble passing the value of a variable
from one windows form to another.

Can anyone point me in the right direction.

Any help here would be greatly appreciated.

Thanks
David

Nov 15 '05 #2
Thats Close,
Lets say you have a string "sItemNumber" on form 1 and you want to pass
that same string "sItemNumber" in form 2 to use as part of a SQL command.
"Josh [MS.Com]" <so*****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
More detail would be good... But I'll use my intuition;

Maybe you have a TextBox on FormA called textBox1 and you want to set the
textof a label on FormB (called label1) to the text of textBox1.

Because FormA.textBox1 is private, FormB can't access it by simply
referencing FormA.textBox1.Text;

If this where the case, you'd might want to create a public property for
textBox1.Text that looks like this;

// class FormA
public string TextOnFormA
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = (string)value;
}
}
//

Then FormB could reference (get and set) the text of FormA.textBox1 like
this;

// class FormB
this.label1.Text = FormA.TextOnFormA;
//

Of course you could also just make textBox1 public, but that's not really
the "right way".

Sound close to what you want?
Josh
Microsoft.com Tools
"David Bacon" <dm*******@hotmail.com> wrote in message
news:ub**************@tk2msftngp13.phx.gbl...
Hi All,
I am new to C#, and am having trouble passing the value of a variable from one windows form to another.

Can anyone point me in the right direction.

Any help here would be greatly appreciated.

Thanks
David


Nov 15 '05 #3
On Tue, 20 Jan 2004 18:33:12 -0600, David Bacon <dm*******@hotmail.com>
wrote:
Thats Close,
Lets say you have a string "sItemNumber" on form 1 and you want to
pass
that same string "sItemNumber" in form 2 to use as part of a SQL command.


Either use a property or a public method in Form 2 that accepts the string
as a parameter.
In both cases you need a reference to Form 2 to be able to access it.
In case of a common "parent" it might be better to call upon the parent to
pass on the string.
If Form 1 is the "parent" of Form 2 (you create Form 2 from within Form 1)
keep the reference to Form 2 for later use

Form2 f2 = new Form2();
f2.TextOnForm2 = "somestring";
f2.CreateSqlString("somestring");

if Form 2 is the parent form, pass 'this' when creating Form 1 and store
the "parent" in Form 1 for later use

Form1 f1 = new Form1(this);

public class Form1
{
Form2 parent = null;

public Form1(Form2 parent)
{
this.parent = parent;
}
}

and then use

parent.TextOnForm2 = "something";
parent.CreateSqlString("somestring");

This assumes Form2 has a property TextOnForm2 or a method CreateSqlString

public string TextOnForm2
{
get{}
set{}
}

public void CreateSqlString(string sourceString)
{
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #4

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

Similar topics

1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.