When we create a windows application on that case we always
face the situation where we needed to pass the value from one form to the
another form.
So in this article I will show you how you can pass value
from one form to another form in windows application using c#.net and vb.net.
Some of my previous article are as follows: How
to Read a Text File Line by Line in C# in Console Application, How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application,
Bind
DataGridView In Windows Application Using C#, How
to Minimize an Application to the Taskbar Tray in C#.Net | System Tray
Application C#, Email
Validation in Windows Application C#.Net and VB.Net | Validating Email ID in
TextBox in C# .Net, How
to get the selected date of a MonthCalendar control in C#.
For this article first we will create a new windows
application and add two form. Form1 and
from2 .
Form1 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 DemoWindowsApplication
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void button1_Click(object
sender, EventArgs e)
{
Form2
objform2 = new Form2();
objform2.form1value =
textBox1.Text;
objform2.Show();
}
}
}
|
In above code we have declared a public variable in Form2
which is form1value. so we have
passed the value of textbox to form1value.
Form2 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 DemoWindowsApplication
{
public partial class Form2 : Form
{
public
Form2()
{
InitializeComponent();
}
public
string form1value = "";
private
void Form2_Load(object
sender, EventArgs e)
{
label2.Text = form1value;
}
}
}
|
Now here is the output
0 comments:
Please let me know your view