I would like to know how can I transfter a string value (a name or something) from Login class over static class to Form1?

This code I have:

1. In program class:

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]       
        static void Main()
        {            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            using (LoginForm login = new LoginForm())
            {                
                if (login.ShowDialog() == DialogResult.OK)
                {                    
                    Application.Run(new Form1());                    
                }
            }
        }
    }

2. In Login class, which checkes if the user has entered the correct user name and password (but here is no login code)

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

        private void buttonLogin_Click(object sender, EventArgs e)
        { 
            string strUserName = textBox1.Text;
            string strPassword = textBox2.Text;
            bool LoginSucceeded = CheckingLogin(strUserName, strPassword);
            if (LoginSucceeded)
            {
                //Here I would need to pass "strUserName" to Form1
                this.DialogResult = DialogResult.OK; 
            }      
        }
    }

I would like to know how to pass the "strUserName from LoginForm to Form1, where I would like to show, who is logged in.

>I would like to know how to pass the "strUserName from LoginForm to Form1

There are number of ways to do that but here is simpler one.

public static string strUserName;
  private void buttonLogin_Click(object sender, EventArgs e)
        { 
            strUserName = textBox1.Text;
            string strPassword = textBox2.Text;
            bool LoginSucceeded = CheckingLogin(strUserName, strPassword);
            if (LoginSucceeded)
            {
                //Here I would need to pass "strUserName" to Form1
                this.DialogResult = DialogResult.OK; 
            }      
        }

Now you can you LoginForm.strUserName static field anywhere within the application.

...
MessageBox.Show(LoginForm.strUserName);
 ...

Suppose, you want to have a list of logged-user then add static collection (List) into your login form.

public static List<string> strUserName=new List<string>();
  private void buttonLogin_Click(object sender, EventArgs e)
        { 
            strUserName.Add(textBox1.Text);
            string strPassword = textBox2.Text;
            bool LoginSucceeded = CheckingLogin(strUserName, strPassword);
            if (LoginSucceeded)
            {
                //Here I would need to pass "strUserName" to Form1
                this.DialogResult = DialogResult.OK; 
            }      
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.