Navigating Forms

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 68
Reputation: ongxizhe is an unknown quantity at this point 
Solved Threads: 1
ongxizhe's Avatar
ongxizhe ongxizhe is offline Offline
Junior Poster in Training

Navigating Forms

 
0
  #1
Dec 30th, 2007
I'm making a Windows Application in C#.
How to navigate from one form to another and not losing data(s) from the previous form?


For an example,
I created Form1.
In Form1, there's textBox1 and button1.
By clicking button1 links you to Form2,
and after Form2 is shown,
text from textBox1 in Form1 will be printed on label1 in Form2.


If anyone have any idea how to write the codes for the following request, please kindly reply. Help will be appreciated. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Navigating Forms

 
0
  #2
Dec 30th, 2007
if you use form1 to call form two, you can do something like this inside form1:

  1. Form2 frmSec = new Form2();
  2. frmSec.label1.Text = textbox1.Text;
This method requires that the label1 modifier on Form2 be set to public.

Or, you can do it like this, some might feel this is a more correct way.
  1. //This is the Form2 constructor, which we have modified
  2. public Form2 (string labText)
  3. {
  4. InitializeComponent(); //it is important that you put this before the next line of code, else an exception is thrown.
  5. this.label1 = labText;
  6. }
  7.  
  8. //Now you put this where you call Form2 inside Form1
  9. Form2 frmSec = new Form2(textbox1.Text);
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 68
Reputation: ongxizhe is an unknown quantity at this point 
Solved Threads: 1
ongxizhe's Avatar
ongxizhe ongxizhe is offline Offline
Junior Poster in Training

Re: Navigating Forms

 
0
  #3
Dec 31st, 2007
What if I was to link Form1 and Form2 together without losing data(s) but when Form2 was already exist? What codes are to type in Form1 and Form2 to get them in connection?

Example:
By clicking button1 in Form1 will transfer you from Form1 to Form2 and along with the text in textBox1 in Form1 to be displayed in Label1 in Form2.

Please write the codes for both Form1 and Form2 along with the description and explanation.
Thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Navigating Forms

 
0
  #4
Dec 31st, 2007
If an Form2 already exists, then it may be wise to have a member variable in Form1 that refers to the instance of Form2. So when you press the button in the Form1, you can use that variable to manipulate the label in the Form2 instance. Consider:

  1. //FORM1 CODE:
  2. //assuming that the Form2 instance inside Form1 is named frmSec
  3.  
  4. //insert into button event handler:
  5. frmSec.ChangeLabel(this.textbox1.Text);
  6.  
  7. //FORM2 code:
  8. public void ChangeLabel(string newText) //make sure this modifier is public
  9. {
  10. label1.Text = newText;
  11. }
How the frmSec variable gets initialized inside Form1 depends on when Form1 and Form2 are created. It shouldn't be hard for you to figure in that part.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 68
Reputation: ongxizhe is an unknown quantity at this point 
Solved Threads: 1
ongxizhe's Avatar
ongxizhe ongxizhe is offline Offline
Junior Poster in Training

Re: Navigating Forms

 
0
  #5
Dec 31st, 2007
I'm kind of confused now. Do you mind to write the complete code?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC