473,799 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use a variable in form1--> form2

Hi,
I can't find a simple example for a simple(?) problem.

I am working on an application with a variable in form1, that variable
is needed in form2 for a calculation but i can't get that variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John
Nov 21 '05 #1
5 14181
You have to work off of instances of the form, therefore is you want FormA
to access any information from FormB, then FormA has to have a reference to
FormB.

For instance (this is just one way you might do it),

MyFormB formB = new FormB();
MyFormA formA = new Form( formA );

Once a reference to the form is made available, then you can access any
public methods or properties.

"John @hotmaill.com>" <JReinders<nosp am> wrote in message
news:uh******** *************** *********@4ax.c om...
Hi,
I can't find a simple example for a simple(?) problem.

I am working on an application with a variable in form1, that variable
is needed in form2 for a calculation but i can't get that variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John

Nov 21 '05 #2
John,

In .NET, forms are object, just like everything else. VB6 would always
create an instance of a form which you could access by the form's type name.

This was never good practice. Instead, make the variable public to form
2 (internal or public if they are in the same assembly), and then pass the
instance of form 1 to form 2 through a method, by setting a property, or
through the constructor. Then, you can store the form as a field in form2
(or just the value you need, you don't have to pass the whole form) and get
the value when you need it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"John @hotmaill.com>" <JReinders<nosp am> wrote in message
news:uh******** *************** *********@4ax.c om...
Hi,
I can't find a simple example for a simple(?) problem.

I am working on an application with a variable in form1, that variable
is needed in form2 for a calculation but i can't get that variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John

Nov 21 '05 #3
Tnx for the fast response,
however, still missing the point.
Based on a topic @
http://msdn.microsoft.com/library/de...ormvisualc.asp
I created a Form1 with a readonly textBox1 and a button to open Form2.
Form2 contains a textBox1 to insert something and a button to close
Form2.

The code for Form1:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace Form1toForm2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
Form2 getForm2 = new Form2();
getForm2.ShowDi alog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}
}

and the Code for Form2:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace Form1toForm2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTex tBox()
{
textBox1.Text = otherForm.TextB ox1.Text;
}
}
}
The code above is not working because an event is missing to copy
Form2 textBox text to Form1 textBox
So, what is needed?

TIA,
Greetings,
John


On Mon, 21 Nov 2005 11:44:56 -0800, "Peter Rilling"
<pe***@nospam.r illing.net> wrote:
You have to work off of instances of the form, therefore is you want FormA
to access any information from FormB, then FormA has to have a reference to
FormB.

For instance (this is just one way you might do it),

MyFormB formB = new FormB();
MyFormA formA = new Form( formA );

Once a reference to the form is made available, then you can access any
public methods or properties.

"John @hotmaill.com>" <JReinders<nosp am> wrote in message
news:uh******* *************** **********@4ax. com...
Hi,
I can't find a simple example for a simple(?) problem.

I am working on an application with a variable in form1, that variable
is needed in form2 for a calculation but i can't get that variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John


Nov 23 '05 #4
In order to access the variables of Form1 from Form2, you must *pass a
reference* of Form1 into Form2:
The code for Form1:

public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
**Note the change in the following line:
Form2 getForm2 = new Form2(this);
getForm2.ShowDi alog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}

and the Code for Form2:

public partial class Form2 : Form
{
**Note the change in the constructor for Form2
public Form2(Form1 form1)
{
InitializeCompo nent(); otherForm = form1; }

private void button1_Click(o bject sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTex tBox()
{
textBox1.Text = otherForm.TextB ox1.Text;
}
}


Form2's constructor was altered to take a reference to Form1. This was
assigned to your otherForm variable. There are other ways to do this,
this is one way.

Nov 23 '05 #5
Okay after probing your added code it didn't work...
until I found something @
http://www.publicjoe.f9.co.uk/csharp/csharp19.html
where to set the textBox1 from privat (default) to public, that did
the job...
anyway, thanks for the support on this (simple) brainteaser...

John.
On 22 Nov 2005 07:08:41 -0800, "Chris Dunaway" <du******@gmail .com>
wrote:
In order to access the variables of Form1 from Form2, you must *pass a
reference* of Form1 into Form2:
The code for Form1:

public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{


**Note the change in the following line:
Form2 getForm2 = new Form2(this);
getForm2.ShowDi alog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}



and the Code for Form2:

public partial class Form2 : Form
{


**Note the change in the constructor for Form2
public Form2(Form1 form1)
{
InitializeCompo nent();

otherForm = form1;
}

private void button1_Click(o bject sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTex tBox()
{
textBox1.Text = otherForm.TextB ox1.Text;
}
}


Form2's constructor was altered to take a reference to Form1. This was
assigned to your otherForm variable. There are other ways to do this,
this is one way.


Nov 23 '05 #6

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

Similar topics

2
2304
by: AbdSol | last post by:
How to code…….. Form1 DataGridView Form2 DataGridView the selected rows data to be sent to Form1 and Form2 to exit. // Form1 DataGridViewRowCollection rows = this.mGrid.Rows; rows.Add(mUser, mPass, mFullName, mSection, mProfile);
5
33080
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say Form1 (main form) and Form2, there is parameter in form2 I have to called from form1. So please would you tell me how? Any way thanks
3
1343
by: Vijay | last post by:
I have 2 forms. in form1 i declared a public variable in which a value is stored. then i goto form2 . (for this is create an object of form2 in form1.) when i go to form2 there is call form1. when i come to form1 the value stored in the variable decalred in form1 gets lost. how can i retain those values?
3
2482
by: R. Harris | last post by:
Hi. I have 2 forms: form1 form2 On Form2 I have a listbox and a button. When I click the button it calls a function from form1 and within that function it updates the listbox on form2. My problem is I don't see the items added to the listbox unless I use
2
1335
by: khokimfang | last post by:
Hi All, I want to ask how to call form with only one variable in VB.Net. I have 3 form (Form1, Form2 and Form3) and 1 module. in the module i want to declare public variable to call form for ex: Public frm as ..... In the Form1 i have two button (btnForm2 and btn Form3) and one Function. Public Function callForm(FormName as String)
11
1797
by: Miro | last post by:
I am banging my head around something that I think I just dont understand what I am reading in the helps. Its gotta be so simple but I just cant figure out what im reading. ( vb 2003 ) Lets say i have Form1 and Form2. Each form is blank. ( no text boxes or anything ) Very Simple Example: ( i just need a start and then i can modify it and learn from it to do what i need to do )
5
2533
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a pointer to object of class Form1. I don't known how to use method add(), where can I find it. From Form1 I can add value like this this->listBox1- I cant find it. I have textBox1 on Form1 and I can change text in this control like this ...
2
2900
by: peachdot | last post by:
hi, The MainForm will have 2 buttons: 1.) Button A : User click button A, hide Mainform then go to form1. User enter data in the textbox.Click finish button,form1 close then go back to MainForm. 2.) Button B : User Click Button B,hide Mainform then go to form2. Click a button & some mathematical operations will be done using all parameters that have been entered in form1. My method of passing variable is:
13
5896
by: dougancil | last post by:
I have a public variable that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query. If I declare Public Class Form1 Public payPeriodStartDate, payPeriodEndDate As Date How then to I declare that variable in form2 and how to I pass it to my sql query
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.