19 Replies - 3754 Views - Last Post: 21 November 2009 - 08:49 AM
#1
How to set a password in Winform application
Posted 31 December 2008 - 01:52 PM
Replies To: How to set a password in Winform application
#2
Re: How to set a password in Winform application
Posted 31 December 2008 - 02:08 PM
#3
Re: How to set a password in Winform application
Posted 31 December 2008 - 02:12 PM
#4
Re: How to set a password in Winform application
Posted 31 December 2008 - 02:40 PM
#5
Re: How to set a password in Winform application
Posted 31 December 2008 - 11:12 PM
#6
Re: How to set a password in Winform application
Posted 31 December 2008 - 11:21 PM

Then, for the OK button add the verification code (basically, you compare the strings):
if (textBox1.Text == "fDHvnE34")
{
// Actions if password is correct
}
else
{
// Actions if password is incorrect
}
Also, set the PasswordChar property to something like a @ symbol, so the characters entered in the TextBox will be replaced with that symbol (in this case nobody will see what characters are being entered).
#7
Re: How to set a password in Winform application
Posted 01 January 2009 - 02:05 AM
nomeealy, on 31 Dec, 2008 - 12:52 PM, said:
Hi
Usually , we use a login form asking for user login name and password before letting him to use the application.
If that's the case ,It would be a good idea to use an application context to accomplish this task.An ApplicationContext is an object that tells the Application object the order of showing forms to user.It has a MainForm property that you can set to let Application knows which form to show next.
Here there's an example that shows how to use an application context.
#8
Re: How to set a password in Winform application
Posted 01 January 2009 - 11:48 AM
Basically the code ur tryin 2 tell is seen in the main form but I want a textbox appearing before the application and user can only allow to access the application if he enters the password correctly ...!!!!
#9
Re: How to set a password in Winform application
Posted 01 January 2009 - 11:52 AM
Application.Run(new Form1());
You just have to replace Form1 with the name of your log in form (so it will become thew startup form). Then, you just use the code I showed - if the password is correct, just close the form and open another one. If the password is incorrect, then show a message (or do another action, like exiting the application).
#10
Re: How to set a password in Winform application
Posted 01 January 2009 - 12:02 PM
Please correct me as am gettin pissed off and my head is gettin blown off.. I have created a Windowsapplication(named example) and make a textbox and button and ur code behind that button then I have gone to the Program.cs of the example and write Application.Run(new binary()); (as i have to run binary program when the user enters the passwd correctly) but the error is showing although i have added binary reference in in ... Correct me please
#11
Re: How to set a password in Winform application
Posted 01 January 2009 - 12:08 PM
#12
Re: How to set a password in Winform application
Posted 01 January 2009 - 12:52 PM
In my example, I have a main form that I want to open AFTER the user logs in correctly. This form is called "frmMain".
I also have a login form called "frmLogin" that will allow a user to login BEFORE the main from is shown.
my Program.cs file:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// the form that you put in the following statement
// is the form that you want to open AFTER the user
// has logged in successfully.
// I am opening the main form of my application, because it is the form
// that I want to open AFTER the user logs in successfully.
Application.Run(new frmMain());
}
}
the "frmMain" form's code:
public partial class frmMain : Form
{
//Constructor of the main form
public frmMain()
{
InitializeComponent();
// this code runs BEFORE the main form is drawn to the screen
// I am opening the login screen to allow the user to login BEFORE
// the main application opens.
frmLogin loginForm = new frmLogin();
loginForm.ShowDialog();
loginForm.Dispose();
}
}
basically, when the application is started, it will start the "frmMain" which calls the constructor for the form. In the constructor, I open the "frmLogin" form.
the "frmLogin" form's code:
public partial class frmLogin : Form
{
bool authenticated = false;
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (ValidateLogin(txtUserName.Text, txtPassword.Text))
{
authenticated = true;
this.Close();
}
else
{
MessageBox.Show("Invalid login. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool ValidateLogin(string userName, string password)
{
// do your validation of the login.
// return true or false depending on whether the login was successful
}
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
if (!authenticated)
Application.Exit();
else
db.Dispose();
}
}
I use a username and password to validate the login. You do NOT have to use both if you don't want to. You can use just a password if you like.
This post has been edited by eclipsed4utoo: 01 January 2009 - 01:43 PM
#13
Re: How to set a password in Winform application
Posted 01 January 2009 - 12:57 PM
#14
Re: How to set a password in Winform application
Posted 01 January 2009 - 12:58 PM
nomeealy, on 1 Jan, 2009 - 02:57 PM, said:
is the application you are opening a .Net project? If so, do you have the source code?
This post has been edited by eclipsed4utoo: 01 January 2009 - 12:59 PM
#15
Re: How to set a password in Winform application
Posted 01 January 2009 - 01:13 PM
|
|

New Topic/Question
This topic is locked




MultiQuote




|