943,340 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 20189
  • C# RSS
1

Passing data between forms

by on Nov 24th, 2008
A common question that routinely comes up is how to share data between forms.

I have outlined two common ways to pass data between forms, among many others. I have used a public property on the output form to accept a value and update the display. I also have a method accepting parameters from the input form, to update the display.

On the first form there are three textboxes tbFirstName, tbLastName, and tbSample with a Submit button to redirect to the output form. I have the same layout on the output form to display the passed values.
C# Code Snippet (Toggle Plain Text)
  1. public partial class frmInput : Form
  2. {
  3. public frmInput()
  4. {
  5. InitializeComponent();
  6. }
  7.  
  8. private void btnSubmit_Click(object sender, EventArgs e)
  9. {
  10. //Set our variables from our input form
  11. string firstName = this.tbFirstName.Text;
  12. string lastName = this.tbLastName.Text;
  13. string sample = this.tbSample.Text;
  14.  
  15. //Create an instance of the output form
  16. frmOutput frmOut = new frmOutput();
  17.  
  18. //We set values through a property
  19. frmOut.Sample = sample;
  20.  
  21. //We set values through a public method
  22. frmOut.SetDisplayValues(firstName, lastName);
  23.  
  24.  
  25. //We show the output form
  26. frmOut.ShowDialog();
  27. }
  28. }
  29.  
  30.  
  31. public partial class frmOutput : Form
  32. {
  33. private string sample;
  34. //Getter / Setter for Sample
  35. public string Sample
  36. {
  37. get
  38. {
  39. return sample;
  40. }
  41. set
  42. {
  43. sample = value;
  44.  
  45. //Set from property
  46. this.tbSample.Text = this.sample;
  47. }
  48. }
  49.  
  50. public frmOutput()
  51. {
  52. InitializeComponent();
  53. }
  54.  
  55. public void SetDisplayValues(string firstName, string lastName)
  56. {
  57. //Set from method
  58. this.tbFirstName.Text = firstName;
  59. this.tbLastName.Text = lastName;
  60. }
  61. }
Comments on this Code Snippet
Oct 19th, 2009
0

Re: Passing data between forms

can you please help me.... what is the code in windows form...i want to open a new form and closing or hiding my current form....
Light Poster
bords is offline Offline
35 posts
since Oct 2009
Oct 19th, 2009
0

Re: Passing data between forms

can you please help me.... what is the code in windows form...i want to open a new form and closing or hiding my current form......
Light Poster
bords is offline Offline
35 posts
since Oct 2009
Jan 15th, 2010
0

Re: Passing data between forms

friend try this show() and hide() function will help u....
eg:
to show form2 and hide form1 see below
C# Syntax (Toggle Plain Text)
  1. form2.show()
and
C# Syntax (Toggle Plain Text)
  1. form1.hide()

note: here form1 and form2 are the names of forms.
Light Poster
ravikiran032 is offline Offline
39 posts
since Jun 2008
Apr 29th, 2010
0

Re: Passing data between forms

hii,i have tried the above code but it is not working for me.
i am having a textbox on form 1 and i am having another form which is form2.
now i want to pass this textbox value of form1 to form 2's label.so can you help me out?
Newbie Poster
mrunmayee is offline Offline
1 posts
since Apr 2010
Apr 29th, 2010
0

Re: Passing data between forms

@mrunmayee:

See this post:

http://www.daniweb.com/forums/post12...ml#post1201961

Thanks
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Feb 26th, 2011
0

Re: Passing data between forms

Hi can anyone help me...On main form (for example Form1) I have a query which creates dynamic table in specific database...

for example
C# Syntax (Toggle Plain Text)
  1. string Query = "create table"+Tablica+"(.........);";
Tablica is defined like
C# Syntax (Toggle Plain Text)
  1. string Tablica = "Mujo_"+value;
where value is value from textBox1

now i want to use variable Tablica in 2nd form (for example Form2) so i can make query with insert to Tablica table

how can i do it
Newbie Poster
Tonchi is offline Offline
1 posts
since Feb 2011
Message:
Previous Thread in C# Forum Timeline: Creating dll for an application and running this dll from other app
Next Thread in C# Forum Timeline: Utilize a webcam





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


Follow us on Twitter


© 2011 DaniWeb® LLC