Very Basic Form Opening Question

G

garyusenet

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.

Thanks,

Gary.
 
C

Chris Dunaway

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(false);

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;
}
}
 
G

garyusenet

Thankyou very much. The first bit of code for use in the main part of
the programme. is that for the program.cs file ? At the moment that
consists of the following: -

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Do i just replace that with the code you supplied or add the code you
supplied to it?

I was wondering because if i replace it, I don't see how my first form
will load.

Thankyou very much,
Gary.
 
G

garyusenet

Dear Chris having re read and experimented with what you said, it now
makes perfect sense. I will certainly be using this in the future so
have made a note of it in my note application!

Many Thanks,

Gary.
 
R

rowe_newsgroups

As well as Chris's answer you could just do the following in the click
handler for the OK button:

<aircode>

private void OK_Click(Object sender, EventArgs e)
{
if (UsernameIsValid(userNameTextBox.Text))
{
this.Close();
MainForm f = new MainForm();
f.Show(); //Or use f.ShowDialog() if you need it to be modal
}
else
{
//Handle the incorrect username or password
MessageBox.Show("Wrong UserName or Password!");
}
}

private Boolean UserNameIsValid(String userName)
{
//Do your validation test here and return the necessary Boolean
value.
if (UserNamePassedTheValidationTest)
return true;
else
return false;
}

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?

How are you storing the usernames now? It's a bit hard to tell you how
to validate them without knowing how they are stored. Let us know and
we'll be glad to help you out!

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top