I created a form frm in c#. This form contains two text boxes for accesing username and password. If these two are valid ones as in database, then display 2nd form frm2. This form display username and password. For this i need the entered values in frm1. How can use this values in frm2?
Loading
SeejaPosted Jul 18, 2008, 3:26 AM
Satish KathiPosted Jul 17, 2008, 10:43 AM
Create Statis variable in Program.cs
public
static string UserNameWhen the log in success assign UserName to this static variable
Program
.UserName = "UserName";And when you are showing any form in the application access this static variable and disply the user name
lblUSerName.Text =
Program.UserName;Here is the Complete code
1. Program.cs
static class Program{
public static string UserName; ///[
STAThread] static void Main(){
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login());}
}
2. Log in Page
public
partial class Login : Form{
public Login(){
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
if ("Your code to check the Login"){
Program.UserName = "UserName"; Form1 f = new Form1();f.Show();
}
}
}
3. Form
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
lblUSerName.Text =
Program.UserName;}
}
Hope this Helps