943,587 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4361
  • C# RSS
Dec 30th, 2007
0

Navigating Forms

Expand Post »
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.
Similar Threads
Reputation Points: 8
Solved Threads: 1
Junior Poster in Training
ongxizhe is offline Offline
68 posts
since Mar 2007
Dec 30th, 2007
0

Re: Navigating Forms

if you use form1 to call form two, you can do something like this inside form1:

C# Syntax (Toggle Plain Text)
  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.
C# Syntax (Toggle Plain Text)
  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);
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Dec 31st, 2007
0

Re: Navigating Forms

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.
Reputation Points: 8
Solved Threads: 1
Junior Poster in Training
ongxizhe is offline Offline
68 posts
since Mar 2007
Dec 31st, 2007
0

Re: Navigating Forms

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:

C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Dec 31st, 2007
0

Re: Navigating Forms

I'm kind of confused now. Do you mind to write the complete code?
Reputation Points: 8
Solved Threads: 1
Junior Poster in Training
ongxizhe is offline Offline
68 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Basic Sorted Neighborhood
Next Thread in C# Forum Timeline: crc32





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC