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: - public partial class Form1 : Form
-
{
-
private string f2TB1;
-
private string f2TB2;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
public string frm2TextBox1
-
{
-
set
-
{
-
f2TB1 = value;
-
}
-
}
-
public string frm2TextBox2
-
{
-
set
-
{
-
f2TB2 = value;
-
}
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form2 newFormTwo = new Form2();
-
newFormTwo.ShowDialog();
-
}
-
private void button2_Click(object sender, EventArgs e) //display values
-
{
-
string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
-
MessageBox.Show(prompt);
-
}
-
}
Form2 code: - public partial class Form2 : Form
-
{
-
public Form2()
-
{
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form1 frm1 = new Form1(); //This works to create a new Form1
-
//and keep the old text I typed into its
-
// textBox1 intact after I close Form2
-
//?why would it since I created a new Form1
-
frm1.frm2TextBox1 = textBox1.Text;
-
frm1.frm2TextBox2 = textBox2.Text;
-
this.Close();
-
}
-
}
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.
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.
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
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: - public partial class Form1 : Form
-
{
-
private string f2TB1;
-
private string f2TB2;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
public string frm2TextBox1
-
{
-
set
-
{
-
f2TB1 = value;
-
}
-
}
-
public string frm2TextBox2
-
{
-
set
-
{
-
f2TB2 = value;
-
}
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form2 newFormTwo = new Form2();
-
newFormTwo.ShowDialog();
-
}
-
private void button2_Click(object sender, EventArgs e) //display values
-
{
-
string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
-
MessageBox.Show(prompt);
-
}
-
}
Form2 code: - public partial class Form2 : Form
-
{
-
public Form2()
-
{
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form1 frm1 = new Form1(); //This works to create a new Form1
-
//and keep the old text I typed into its
-
// textBox1 intact after I close Form2
-
//?why would it since I created a new Form1
-
frm1.frm2TextBox1 = textBox1.Text;
-
frm1.frm2TextBox2 = textBox2.Text;
-
this.Close();
-
}
-
}
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: - public partial class Form1 : Form
-
{
-
private string f2TB1;
-
private string f2TB2;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
public string frm2TextBox1
-
{
-
set
-
{
-
f2TB1 = value;
-
}
-
}
-
public string frm2TextBox2
-
{
-
set
-
{
-
f2TB2 = value;
-
}
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form2 newFormTwo = new Form2(this);
-
newFormTwo.ShowDialog();
-
}
-
private void button2_Click(object sender, EventArgs e) //display values
-
{
-
string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
-
MessageBox.Show(prompt);
-
}
-
}
Form2 code: - public partial class Form2 : Form
-
{
-
Form1 frm1;
-
public Form2(Form1 frm1)
-
{
-
this.frm1 = frm1;
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
frm1.frm2TextBox1 = textBox1.Text;
-
frm1.frm2TextBox2 = textBox2.Text;
-
this.Close();
-
}
-
}
Hope that helps
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alex Vinokur |
last post by:
Various forms of argument passing
=================================
C/C++ Performance Tests
=======================
Using C/C++ Program Perfometer...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |