473,322 Members | 1,719 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,322 software developers and data experts.

Passing data between forms

I have a C# problem I've been struggling with for a week now. I can't figure it out. I've made a sample application to demonstrate my problem: how do I pass data between forms? I've read tons of posts/articles etc. about doing this, but I cant make it work. The application I'm writing has a bunch of forms, so when I'm done with one, I'd like to close it to free resources. On the close button event on these forms, I'd like to update variables I've declared but not displayed in the main form class. (I'd really like to send back an array of variables from these forms I think - wow, that sounds daunting for me). Anyway, I can't seem to make it work. Here's my example:

Form1 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         private string f2TB1;
  4.         private string f2TB2;
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         public string frm2TextBox1
  10.         {
  11.             set
  12.             {
  13.                 f2TB1 = value;
  14.             }
  15.         }
  16.         public string frm2TextBox2
  17.         {
  18.             set
  19.             {
  20.                 f2TB2 = value;
  21.             }
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             Form2 newFormTwo = new Form2();
  26.             newFormTwo.ShowDialog();
  27.         }
  28.         private void button2_Click(object sender, EventArgs e) //display values
  29.         {
  30.             string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
  31.             MessageBox.Show(prompt);
  32.         }
  33.     }
Form2 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form2 : Form
  2.     {
  3.         public Form2()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void button1_Click(object sender, EventArgs e)
  9.         {
  10.             Form1 frm1 = new Form1(); //This works to create a new Form1 
  11.                                                     //and keep the old text I typed into its
  12.                                                     // textBox1 intact after I close Form2
  13.                                                    //?why would it since I created a new Form1
  14.             frm1.frm2TextBox1 = textBox1.Text;
  15.             frm1.frm2TextBox2 = textBox2.Text;
  16.             this.Close();
  17.         }
  18.     }
When I click on the button in Form1 to MessageBox display the values I've typed into the textBoxes on forms 1 and 2, the Form1 textBox1 value is correct, but the Form2 textBox1 and 2 values (f2TB1 and f2TB2) are null. Why won't this work? Arg!!

Is there a better way to pass the values? Maybe make invisible textBox controls on Form1 and update them when I close Form2 - seems cheesy?

Thanks in advance for any help.
May 27 '07 #1
6 1929
Okay, after messing with the code more, I realized that I created a new Form1 that wasn't visible and that instance of Form1 has the correctly updated variables f2TB1 and 2. Well, I really didn't want to create another instance of Form1, I just want to pass the data from textBoxes back to the existing instance of Form1.

I'll keep struggling along. Any thoughts would be appreciated.
May 28 '07 #2
Hmmm . . . .Delegates?
May 28 '07 #3
Frinavale
9,735 Expert Mod 8TB
Hmmm . . . .Delegates?
Check out this .NET article on Sessions about How to pass information between web pages

There are other means to pass information between forums too: use cookies, use the view state, use hidden fields.

I find that sessions are the easiest.

-Frinny
May 28 '07 #4
mwalts
38
I have a C# problem I've been struggling with for a week now. I can't figure it out. I've made a sample application to demonstrate my problem: how do I pass data between forms? I've read tons of posts/articles etc. about doing this, but I cant make it work. The application I'm writing has a bunch of forms, so when I'm done with one, I'd like to close it to free resources. On the close button event on these forms, I'd like to update variables I've declared but not displayed in the main form class. (I'd really like to send back an array of variables from these forms I think - wow, that sounds daunting for me). Anyway, I can't seem to make it work. Here's my example:

Form1 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         private string f2TB1;
  4.         private string f2TB2;
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         public string frm2TextBox1
  10.         {
  11.             set
  12.             {
  13.                 f2TB1 = value;
  14.             }
  15.         }
  16.         public string frm2TextBox2
  17.         {
  18.             set
  19.             {
  20.                 f2TB2 = value;
  21.             }
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             Form2 newFormTwo = new Form2();
  26.             newFormTwo.ShowDialog();
  27.         }
  28.         private void button2_Click(object sender, EventArgs e) //display values
  29.         {
  30.             string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
  31.             MessageBox.Show(prompt);
  32.         }
  33.     }
Form2 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form2 : Form
  2.     {
  3.         public Form2()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void button1_Click(object sender, EventArgs e)
  9.         {
  10.             Form1 frm1 = new Form1(); //This works to create a new Form1 
  11.                                                     //and keep the old text I typed into its
  12.                                                     // textBox1 intact after I close Form2
  13.                                                    //?why would it since I created a new Form1
  14.             frm1.frm2TextBox1 = textBox1.Text;
  15.             frm1.frm2TextBox2 = textBox2.Text;
  16.             this.Close();
  17.         }
  18.     }
When I click on the button in Form1 to MessageBox display the values I've typed into the textBoxes on forms 1 and 2, the Form1 textBox1 value is correct, but the Form2 textBox1 and 2 values (f2TB1 and f2TB2) are null. Why won't this work? Arg!!

Is there a better way to pass the values? Maybe make invisible textBox controls on Form1 and update them when I close Form2 - seems cheesy?

Thanks in advance for any help.
Seems to me like he's using Windows forms not Web forms.

In which case, there is a lot of things you could do.

For example, since your using ShowDialog() you could make the information you want to pass public properties and then set them as the form exits. These values will then be available to the calling form after the ShowDialog call.

Alternatively, you could pass a reference to the main form in the constructor of the other forms, then use that reference as you exit to set the main forms public properties

That is,
Form1 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         private string f2TB1;
  4.         private string f2TB2;
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         public string frm2TextBox1
  10.         {
  11.             set
  12.             {
  13.                 f2TB1 = value;
  14.             }
  15.         }
  16.         public string frm2TextBox2
  17.         {
  18.             set
  19.             {
  20.                 f2TB2 = value;
  21.             }
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             Form2 newFormTwo = new Form2(this);
  26.             newFormTwo.ShowDialog();
  27.         }
  28.         private void button2_Click(object sender, EventArgs e) //display values
  29.         {
  30.             string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
  31.             MessageBox.Show(prompt);
  32.         }
  33.     }
Form2 code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form2 : Form
  2.     {
  3.         Form1 frm1;
  4.         public Form2(Form1 frm1)
  5.         {
  6.             this.frm1 = frm1;
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void button1_Click(object sender, EventArgs e)
  11.         {
  12.             frm1.frm2TextBox1 = textBox1.Text;
  13.             frm1.frm2TextBox2 = textBox2.Text;
  14.             this.Close();
  15.         }
  16.     }
Hope that helps
May 28 '07 #5
Alternatively, you could pass a reference to the main form in the constructor of the other forms, then use that reference as you exit to set the main forms public properties
That's what I thought I was doing until I realized that to pass the references in a constructor, I needed to create a new instance of the Form1 class which meant I had two Form1s open. I couldn't figure out how to reference the "already open" instance of Form1. I still can't really.
May 29 '07 #6
mwalts
38
That's what I thought I was doing until I realized that to pass the references in a constructor, I needed to create a new instance of the Form1 class which meant I had two Form1s open. I couldn't figure out how to reference the "already open" instance of Form1. I still can't really.
If you open form 2 from form one, just do it like I did in my example. Pass the keyword "this" to the constructor of the second form and that will give you a reference to the already open Form1.

If your opening Form2 from some other form somewhere and you want to reference the open instance of Form1, then you could create a static global property of the current istance of Form1, which you update when Form1 is loaded, then use that static property whenever you need to communicate with the open form.

Not sure if that's the best practice, but I know it works.

Good luck,

-mwalts
May 29 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
5
by: Rod | last post by:
I've written 2 ASP.NET applications (I've worked on one with a team and another by myself). In my ASP.NET pages, when saving data to a backend database I've done it by using the click event of a...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
2
by: Carl Heller | last post by:
Working in VS2003, .Net 1.1 I'm working on a project where I compare data between two databases. This is a lengthy process, and very data intensive, so I decided to create a class, and thread...
1
by: SteveDouglas | last post by:
Hi all, I am currently writing an application in VB.NET that has a lot of controls (treeviews/listviews/labels and so forth) that represent "things" that need to be draggable from place to place,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.