none
How to create a login form in c#

    Question

  • After learning delphi and visual basic.net to a intermediate standard i have decided to learn c#.
    Basically i`m trying to create a login form that accepts a username and password and compares the users entrys to pre-defined variables. My current code is as follows...

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string username = "Richard";
                string password = "Peugeot";
                if (this.username.Text == username) &(this.password.Text == password);
                return;
            }
        }
    }

    I have labelled the username and password entry box`s username and password.

    This code throws up 3 errors during compile time which are :

    error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    error CS0214: Pointers and fixed size buffers may only be used in an unsafe context

    error CS0211: Cannot take the address of the given expression

    Any help on how to fix these errors or an example of a working login form that accepts user and pass input from a user would be greatly appreciated.

    I have managed to create working login forms in vb.net and delphi with several types of encryption without any problems.
    C# and nevermind C++ are proving a nightmare in comparison.
    Please help me guys and gurus!

    Many thanks,
    Richard.

    Monday, January 07, 2008 5:52 PM

Answers

  •  Rich_H_30 wrote:
    string username1 = "Richard";
                string password1 = "Peugeot";

                if (this.username.Text == username1 && this.password.Text == password1)

    The above example works very nicely, thanks for all your help!


    if ((this.username.Text == username1) && (this.password.Text == password1))

    Also works well.
    Tuesday, January 08, 2008 2:11 PM

All replies

  • Code Block

      private void button1_Click(object sender, EventArgs e)
            {
                string username = "Richard";
                string password = "Peugeot";
                if ((this.username.Text == username) && (this.password.Text == password)) ;
                return;
            }

    Monday, January 07, 2008 6:12 PM
  • Hi,

    Looking at your code, a number of things catch my eye:
    1. The names of the variables is the same as that of the textboxes. That's not real clear
    2. The if-statement lacks a couple of brackets. Put the entire check between brackets
    3. The if-statement has a ; at the end. That makes that the if-statement doesn't do anything!

    I changed your code a bit:

    Code Block
        private void button1_Click(object sender, EventArgs e)
        {
            string usernm  = "Richard";
            string passwrd = "Peugeot";
            if ((username.Text == usernm) && (password.Text == passwrd))
            {
                //What to do when they match? Redirect?
            }
        }


    Hope this helps

    Monday, January 07, 2008 6:14 PM
  • Hello Richard,

    The problem is with your if statement. The condtitions must always be surrounded by () in c#(also in C++):

              if ( this.username.Text() == username && this.password.Text() == password )
                   return;

    Also, object properties, such as Text, must be called as functions in C#(unlike VB): Text() vs Text(incorrect). Another thing: the AND logical operator. In C#(and C++) the logical AND is &&.
    What you used: "&" is called a bitwise AND - it computes the binary AND operation. It applies only to integer numbers.
    You also added a ";" after the if. A single ";" with no statement means "void instruction". It is legal, but it has no effect.

    Regards
    Monday, January 07, 2008 6:21 PM
  •  marcel nita wrote:
    Also, object properties, such as Text, must be called as functions in C#(unlike VB): Text() vs Text(incorrect).

    this is actualy wrong. properties in c# are called without brackets, unlike methods, which must be called using brackets.
    Monday, January 07, 2008 6:39 PM
  • string username1 = "Richard";
                string password1 = "Peugeot";

                if (this.username.Text == username1 && this.password.Text == password1)

    The above example works very nicely, thanks for all your help!
    Tuesday, January 08, 2008 10:51 AM
  • Heres my complete code for those interested..

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string username1 = "Richard";
                string password1 = "Peugeot";

                if (this.username.Text == username1 && this.password.Text == password1)
                    MessageBox.Show("Welcome Richard!", "Welcome");
                else
                    MessageBox.Show("Incorrect username or password","Bad credentials");
            }
        }
    }

    Obviously this is just startup code, will add some encryption and other things as time goes on.
    Tuesday, January 08, 2008 11:58 AM
  •  Rich_H_30 wrote:
    string username1 = "Richard";
                string password1 = "Peugeot";

                if (this.username.Text == username1 && this.password.Text == password1)

    The above example works very nicely, thanks for all your help!


    if ((this.username.Text == username1) && (this.password.Text == password1))

    Also works well.
    Tuesday, January 08, 2008 2:11 PM
  • better rate our post as answers instead of writign what works and what doesnt. we already know our answers are right.
    Tuesday, January 08, 2008 2:15 PM
  • Erm none of the answers i was given were correct. Compare what i typed in to what others typed in as there answers, there is a difference. What the others wrote just pointed me in the right direction to create code that i could compile without any errors.
    Wednesday, January 09, 2008 1:42 PM
  • Hey Rich,

    Have a look at the answers Naicul, Marcel Nita and I gave you, those are exactly the same as the solution you typed. Sure, the variables names are different, but the functionality is exactly the same. The posts address the problem of your initial code, and give you a solution.

    It's not so nice of you to say nobody helped you, while three of us actually did..., right?

    Kind regards

     

    Wednesday, January 09, 2008 1:47 PM
  •  Rick van den Bosch wrote:


    Code Block
        private void button1_Click(object sender, EventArgs e)
        {
            string usernm  = "Richard";
            string passwrd = "Peugeot";
            if ((username.Text == usernm) && (password.Text == passwrd)
            {
                //What to do when they match? Redirect?
            }
        }





    As you can see your example is missing a bracket and will throw up an error when compiling, i tried all of the examples given before i wrote the correct one.

    Correct example:-
    if (this.username.Text == username1 && this.password.Text == password1)

    or

    if ((this.username.Text == username1) && (this.password.Text == password1))

    As for thanking everyone that has already been done in a previous posting by myself !
    I`m sure you guys are 100x better than i am as i`m a beginner but i`m afraid to say on this occasion what you wrote
    was not entirely correct. I`m just pointing that out so other viewers know exactly what to write.
    Once again thanks to all who pointed me in the right direction Smile
    Wednesday, January 09, 2008 2:01 PM
  • I'd like to be credited for my reply. If a moderator is reading this, please mark my and Rick's answers as correct.

    @ Rich_H_30: be sure I'll not answer any of your questions from now on. Good luck!
    Wednesday, January 09, 2008 2:09 PM
  • Why would a moderator mark a non working example as correct? I tried your example and it gave a compile error.

    Wednesday, January 09, 2008 2:14 PM
  •  naicul wrote:
    I'd like to be credited for my reply. If a moderator is reading this, please mark my and Rick's answers as correct.

    @ Rich_H_30: be sure I'll not answer any of your questions from now on. Good luck!

    Naicul, as a side note, your answer is not completely correct. You added a semicolon after the if statement, which was not correct.

    Since my asnwer was not entirely correct, I don't require my post being marked as answered.
    My opinion is that we shouldn't beg for answers. Moderators mark posts as answered all the time, so they would have done it by now if our posts were correct.

    Regards
    Wednesday, January 09, 2008 2:14 PM
  • @NaicuI: I already marked your post as helpful when you post it(the one about the mistake in my post)...
    So, indeed, helpful posts should be marked, but ours aren't exactly that.
    Marked posts have a higher relevance in search results. We wouldn't want our incorrect posts to apper first in a search, would we?
    Wednesday, January 09, 2008 2:17 PM
  • Guys,

    I stepped out of the discussion because, like Marcel said, 'My opinion is that we shouldn't beg for answers.' By the way, my example did have one bracket missing (bad typo), but the general message was (see point 2 of my first post): The if-statement lacks a couple of brackets. Put the entire check between brackets. Not that it matters in the context of this specific post Big Smile

    Anyhow: Richard, good luck with the further development of your login form (and the rest of the application). And the rest: let's stop abusing this thread to ventilate any irritation you might have about users who only ask and do not acredit others. That's what the moderators are here for Wink

    Kind regards.

    Wednesday, January 09, 2008 2:26 PM
  •  naicul wrote:
    I'd like to be credited for my reply. If a moderator is reading this, please mark my and Rick's answers as correct.

    @ Rich_H_30: be sure I'll not answer any of your questions from now on. Good luck!


    I have now marked yours and ricks post as helpful. Please remember i am a newbie on this forum and didn`t even realise i had to click that until just now.

    Naicul, i suggest you grow up.. Crying because i didn`t click a button immediately just makes me think your a jealous geek who can`t get a girlfriend, theres a lot of people like you in IT unfortunately...  such a shame.
    If you choose not to answer future posts  i really couldn`t care less as i work in a team of 40+ developers who are far more talented than you are.


    Wednesday, January 09, 2008 2:30 PM
  •  marcel nita wrote:
     naicul wrote:
    I'd like to be credited for my reply. If a moderator is reading this, please mark my and Rick's answers as correct.

    @ Rich_H_30: be sure I'll not answer any of your questions from now on. Good luck!

    Naicul, as a side note, your answer is not completely correct. You added a semicolon after the if statement, which was not correct.

    Since my asnwer was not entirely correct, I don't require my post being marked as answered.
    My opinion is that we shouldn't beg for answers. Moderators mark posts as answered all the time, so they would have done it by now if our posts were correct.

    Regards


    Marcel,

    RichH originaly posted the sample with that semicolon. The code is ok as it is, with that semicolon. It compiles and it does nothing, as RichH originaly intended (I think).

    @RichH: if you want your if to do something, don't put a semicolon a semicolon after it.

    I'm not begging for my posts to be marked as answers but, in this case, I entierly deserve it.

    @RichH: what compilation error do you get when you run my code. I'm really, really curious.
    and, for your concern, i do have a girlfriend. good luck, i'm stepping out of this discution.
    Wednesday, January 09, 2008 4:18 PM
  • Poor poor girl.
    Wednesday, January 09, 2008 4:33 PM
  • AM is not poor poor Girl. It is Princess. It is NUMBER 1, It will be raised upto Number 1 again
    Friday, April 27, 2012 11:32 AM