Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

Join 244,299 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 843 people online right now. Registration is fast and FREE... Join Now!




How to set a password in Winform application

 
Reply to this topicStart new topic

How to set a password in Winform application

nomeealy
31 Dec, 2008 - 12:52 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

Can anyone tell me how to set a password in the C# application (Windows application) , like whenever I run the program it ask me for the password???

User is offlineProfile CardPM
+Quote Post


Core
RE: How To Set A Password In Winform Application
31 Dec, 2008 - 01:08 PM
Post #2

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
You can just add a TextBox to the startup form and ask the user to enter the password. Then, for a Click event handler of a Button control you can add a piece of verification code, to check whether the text entered in the TextBox control is the correct password or not (just compare strings). You can either set the password directly in the code or use application settings.
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
31 Dec, 2008 - 01:12 PM
Post #3

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

can i display a textbox to the user like a MessageBox?? like when a user click CTRL+F5 den the textbox appears first asking for the passwd , if the psswd is correct then only the application will run.. Or there any VS Studio.net support for suchthing?
User is offlineProfile CardPM
+Quote Post

Core
RE: How To Set A Password In Winform Application
31 Dec, 2008 - 01:40 PM
Post #4

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
You can create a Windows Form like I described in my previous post. I don't know why do you want to use Ctrl+F5 to launch your application. Note, that the user won't use F5 or Ctrl+F5 to launch the application (like it is in Visual Studio). They will use the executable file.
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
31 Dec, 2008 - 10:12 PM
Post #5

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

ya xactly , thats wat i mean ! Whether a user runs a .exe file or build the application the MessageBox textfield occurs and asks for a password , if user enters the psswd correctly then only he is allowed to use the application otherwise not ... How will I do it?
User is offlineProfile CardPM
+Quote Post

Core
RE: How To Set A Password In Winform Application
31 Dec, 2008 - 10:21 PM
Post #6

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
Just create a Windows Form similar to the one on the picture:

IPB Image

Then, for the OK button add the verification code (basically, you compare the strings):

CODE

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).
User is offlineProfile CardPM
+Quote Post

beatles1692
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 01:05 AM
Post #7

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 62



Thanked: 12 times
My Contributions
QUOTE(nomeealy @ 31 Dec, 2008 - 12:52 PM) *

Can anyone tell me how to set a password in the C# application (Windows application) , like whenever I run the program it ask me for the password???

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.
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 10:48 AM
Post #8

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

@ CORE

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 ...!!!!
User is offlineProfile CardPM
+Quote Post

Core
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 10:52 AM
Post #9

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
You just have to set up the startup object for your application to your log in form. In the Program.cs file (you can locate it in the Solution Explorer) you can find the line:

CODE

            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).
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 11:02 AM
Post #10

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

@CORE

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
User is offlineProfile CardPM
+Quote Post

Core
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 11:08 AM
Post #11

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
In the piece of code I have shown (from the Program.cs file) you have to set the name of the Windows Form to be shown at startup, not a name like binary (unless binary is the name of the startup form). And in my first piece of code (verification) you should replace the comments with specific code you want to be executed in each case.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 11:52 AM
Post #12

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
here's how you would do it.

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:
CODE

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:
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:
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: 1 Jan, 2009 - 12:43 PM
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 11:57 AM
Post #13

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

I think using System.Diagnostics to Start a process is much more efficient and easier..!!!! thanx anyways:-)
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 11:58 AM
Post #14

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
QUOTE(nomeealy @ 1 Jan, 2009 - 02:57 PM) *

I think using System.Diagnostics to Start a process is much more efficient and easier..!!!! thanx anyways:-)


is the application you are opening a .Net project? If so, do you have the source code?

This post has been edited by eclipsed4utoo: 1 Jan, 2009 - 11:59 AM
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 12:13 PM
Post #15

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

yes its a .net project
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 12:17 PM
Post #16

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
why don't you just add the login form to the binary applications project?
User is offlineProfile CardPM
+Quote Post

nomeealy
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 12:22 PM
Post #17

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 34

The login form name is form1 , i just add this to the binary's program.cs like this

[code]

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace binary
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // this is the name of the binary form itself
Application.Run(new Form1()); // this will be the login form name??? not gettin ur point seriously
}
}
}

The login form name is form1 , i just add this to the binary's program.cs like this

CODE


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace binary
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());   // this is the name of the binary form itself
            Application.Run(new Form1()); // this will be the login form name??? not gettin ur point seriously
        }
    }
}


User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: How To Set A Password In Winform Application
1 Jan, 2009 - 12:35 PM
Post #18

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
I gave you all the code that you need to add a login form to an application. What don't you understand?

Look at my code, did I have two "Application.Run()" statements? no.
Look at my code, which form do I call in the "Application.Run()" statement?
Look at my code, what is the name of the Login form?

This post has been edited by eclipsed4utoo: 1 Jan, 2009 - 12:36 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:06PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month