Pass value from one form to another in C#

Download
Download is available until [expire_date]
  • Version
  • Download 54
  • File Size 60.22 KB
  • File Count 1
  • Create Date April 25, 2016
  • Last Updated April 25, 2016

Pass value from one form to another in C#

Pass value from one form to another in C#

This is a sample program in c# that passes or transfer value entered in a textbox from source form to the label control of another form.

The program will ask you to enter your name, address and age, any value you entered on those fields will appear on the label in the second form.

Open the project solution (.sln) in visual studio 2010/2012 or open directly the project file (.csproj)

Source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
            
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            string myname=textBox1.Text;
            int myage = int.Parse(textBox2.Text);
            string myaddress = textBox3.Text;
            Form2 form2 = new Form2(myname, myage, myaddress);

            if (textBox1.Text == "")
            {
                MessageBox.Show("Please enter your name");
                textBox1.Focus();
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("Please enter your age");
                textBox2.Focus();
            }
            else if (textBox3.Text == "")
            {
                MessageBox.Show("Please enter your address");
                textBox3.Focus();
            }
            else
            {
                form2.Show();
            }
            
        }
    }
}
, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.