ga********@myway.com wrote:
I have a login form, which i have made by simply modifying the standard
wizard created form from Visual C# express - when I chose a windows
application.
I have two buttons on my login form.
The first is OK, The second is cancel.
For cancel i have the event set to application.exit
I'm now looking at the OK button. How do I open my main programme form
when OK is clicked?
I have tried Application.Run(new Form1()); But get an error complaining
something about threads.
Also as well as displaying my main form when the button is pressed I
will need to parse it the validated user name, how do i do this also?
I am sure this is basic, but couldn't find a direct answer.
In the Main method for your program use something like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
LoginForm frm = new LoginForm();
if (frm.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm(frm.UserName));
}
}
Add a UserName property to the LoginForm class and change the
constructor of your main form to take a string. On the LoginForm, set
the OK button's DialogResult property to OK and the Cancel button's
property to Cancel. The Main method will check the result of the
LoginForm when it is closed.
public partial class LoginForm: Form
{
public LoginForm()
{
InitializeComponent();
}
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private void button2_Click(object sender, EventArgs e)
{
_userName = textBox1.Text;
}
}
public partial class MainForm : Form
{
public MainForm(string username)
{
InitializeComponent();
this.label1.Text = username;
}
}