Hi all, just would like to ask some help as to how I would go about modifying this code I created to do the following:

1 - need user input (in password field) to be echoed back as *******.
2 - need the class to open a new class (lets call that class class2 for now) upon entry of correct credentials.Many thanks!

/* Phil
*
*/
using System;
using System.Windows.Forms;
using System.Drawing;
     
    public class UserLogin : Form 
    {
    private Button btnOk;    
    private Button btnCancel; 
    	
    private Label lblUser;
    private Label lblPass;
    private Label lblInst;
   
    private TextBox txtUser;
    private TextBox txtPass;
     
/*-----------------------------------------------------------------------------------------*/     
  
  public UserLogin()
    {
       InitializeComponent();
    }
    
/*-----------------------------------------------------------------------------------------*/
    
    private void InitializeComponent()
    {
        this.FormBorderStyle = FormBorderStyle.Fixed3D;
    	this.Size = new System.Drawing.Size(300, 300);
        this.Text = "User Login Screen";
    	this.MinimizeBox = true;
    	this.MaximizeBox = false;
    	this.HelpButton = false;
    	this.ControlBox = true;
        this.StartPosition = FormStartPosition.CenterScreen;
       
        // Instantiate the controls...
        lblInst = new Label();
        lblUser = new Label();
        lblPass = new Label();
           
        txtUser = new TextBox();
        txtPass = new TextBox();
            
        btnOk = new Button();
    	btnCancel = new Button();
    
        // Set properties
     
        lblInst.AutoSize = true;
        lblInst.Text     = "Please supply your credentials:";
        lblInst.Location = new Point(20, 20);
        
        lblUser.AutoSize = true;
        lblUser.Text     = "Username:";
        lblUser.Location = new Point(20, 50);
     
        lblPass.AutoSize = true;
        lblPass.Text     = "Password:";
        lblPass.Location = new Point(20, 80);
          
        txtUser.Width = 100;
        txtUser.Location = new Point(140, 50);
      
        txtPass.Width = 100;
        txtPass.Location = new Point(140, 80);
    	
    	btnOk.Text = "OK";
        btnOk.BackColor = Color.LightGray;
        btnOk.Location = new Point(50, 200);
     
     	btnCancel.Text = "Cancel";
        btnCancel.BackColor = Color.LightGray;
        btnCancel.Location = new Point(150, 200);     
    	
        this.Controls.Add(lblInst);
        this.Controls.Add(lblUser);    // Add label to form
        this.Controls.Add(lblPass);   
        this.Controls.Add(txtUser);	   // add text box to form
        this.Controls.Add(txtPass);   
        this.Controls.Add(btnOk);  // Add button to form
        this.Controls.Add(btnCancel); 
              
        // Event handlers 
        btnOk.Click += new System.EventHandler(this.btnOk_Click);
        btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
    }
    
/*-----------------------------------------------------------------------------------------*/ 
    
    protected void btnOk_Click( object sender, System.EventArgs e)
    {
    	if(txtUser.Text == "") 
    	{
     		MessageBox.Show("You must enter a Username.", "Name Entry Error",
        	MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  		}
  		if(txtUser.Text == "MJ12" && txtPass.Text == "Roswell1947")
  		{
  			//need help here!
  		}
   		if(txtPass.Text == "") 
   		{
     	 	MessageBox.Show("You must enter a Password.", "Password Entry Error",
         	MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
   		}
   		else 
  		{
      // Code to act on the data entered would go here.
   		}

    }

/*-----------------------------------------------------------------------------------------*/   
   
    protected void btnCancel_Click( object sender, System.EventArgs e)
    {
    	Application.Exit();
    }
   
/*-----------------------------------------------------------------------------------------*/   
   
   public static void Main( string[] args )
    {
       Application.Run( new UserLogin() );
    }
/*-----------------------------------------------------------------------------------------*/   
      
}

Recommended Answers

All 11 Replies

if(txtUser.Text == "MJ12" && txtPass.Text == "Roswell1947")
{
//need help here!
}

I am guessing that you want to create another windows forum at this point.

so you may want to do something like this

if(txtUser.Text == "MJ12" && txtPass.Text == "Roswell1947")
{
	// Create instance of the mainForm class
	Form nextForm = new MainForm();
}

You would have to create a new form and in the case name it MainForm, this will then load the next form when they enter the correct sername and password.

Anyhow good luck and I hope this helps you.

Many thanks for your reply, it is helpful but the issue is that I have already have anothr class that displays a form (class2) which I want to be opened when a correct username/password is entered. Any ideas on that would be helpful. Thanks

if(txtUser.Text == "MJ12" && txtPass.Text == "Roswell1947")
{
	// Create instance of the mainForm class
	Form nextForm = new Class2();
	// We may need to make it visable
	nextForm.Visible=true;
}

I think that should work, But don't have Windows.Forms on this computer to play around with, so am abit uncertain

lol thanks for your help again, unfortunately I had tried that befre posting and I get a compilation error "the type or namespace 'class2' could not be found. (are you missing a using directive or assembly reference?)(CS0246).
Any thoughts on that ? thanks again for your help

If your second class has been defined as class2 and is accessible within your current form ( in the same project and within the same namespace ) it should work. That error suggests that it is unable to find the class named class2,

Just make certain that where this one says

public class UserLogin : Form

your second form says

public class Class2 : Form

note it is caps sensitive and I did say to use Class2 so if you have named your class 'class2' insted of 'Class2' you will have to write

if(txtUser.Text == "MJ12" && txtPass.Text == "Roswell1947")
{
	// Create instance of the mainForm class
	Form nextForm = new class2();
	// We may need to make it visable
	nextForm.Visible=true;
}

thanks pal, would you believe it I made a caps error on class declaration! i.e. it was class2 not Class2! THanks a million! that really is an annoying little error I spent ages trying to figure that out! working good now

I'm working on the same thing only i want to make my login thing to work through a data base.... Mind helping me?

How are you creating the database? SQL? XML?

Sql....

check the MSDN website there are some great tutorials with regards to c# & sql there!

with regards to echoing back input parameters in a pssword box as ***** use this:
this.textboxname.UseSystemPasswordChar = true;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.