Passing data between forms

Please support our C# advertiser: Intel Parallel Studio Home
dickersonka dickersonka is offline Offline Nov 24th, 2008, 11:40 am |
0
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.
Quick reply to this message  
C# Syntax
  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. }
0
bords bords is offline Offline | Oct 19th, 2009
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....
 
0
bords bords is offline Offline | Oct 19th, 2009
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......
 
 

Tags
c#, forms

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC