471,594 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Very Basic Form Opening Question

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.

Jan 3 '07 #1
4 1241
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;
}
}

Jan 3 '07 #2
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(fals e);
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.
Chris Dunaway wrote:
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;
}
}
Jan 3 '07 #3
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.

ga********@myway.com wrote:
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(fals e);
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.
Chris Dunaway wrote:
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;
}
}
Jan 3 '07 #4
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;
}

</aircode>
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
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.

Thanks,

Gary.
Jan 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Jason Curl | last post: by
5 posts views Thread by Sarah | last post: by
50 posts views Thread by diffuser78 | last post: by
12 posts views Thread by Tom | last post: by
1 post views Thread by pitjpz | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.