Tuesday, 12 March 2013

PASSING VALUE FROM ONE FORM TO ANOTHER USING C#


This an example of transferring a value from Form1 to Form2..

IN THE FORM1 TYPE THE FOLLOWING CODE


   public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
         }

        public static string Sno;  //Declaring a Global variable ID

    private void button1_Click(object sender, EventArgs e)
        {

         Sno = textBox1.Text;    //Type any value in the textbox and click the button
         Form2 f2 = new Form2();
         f2.Show();
        }
      }

IN THE FORM2 TYPE THE FOLLOWING CODE

  public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
         }

  private void Form2_Load(object sender, EventArgs e)
        {

          int v1 =  int.Parse(Form1.Sno);  //Getting the Sno value from Form1
          Label1.Text  = Sno.ToString();  //Displaing the Sno value got from Form1
        }
     }

No comments:

Post a Comment

back to top