Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hi,

In my class I defined some textboxes, see C# code below. The user must fillin his username/password, I want to reuse those values in another class.

So I tried this in my initial class:

public string username {
    get {
        return this.textBoxUsername.Text;
    }
    set {
        this.textBoxUsername.Text = value;
    }
}
public UserControlStart(string test)
{
    this.textBoxUsername.Text = xc;
}



In my other class where I want to reuse the username/password value I tried this:

UserControlStart p1 = new UserControlStart();
MessageBox.Show(p1.username);



Complete Code
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace Form {

    public partial class UserControlStart : UserControl {
        public string xc, yc;
        public UserControlStart() {
            InitializeComponent();
        }
        public void buttonSignIn_Click(object sender, EventArgs e) {
            if (textBoxUsername.Text.Length == 0) {
                errorProvider.SetError(textBoxUsername, "Please enter your username");
                return;
            } else if (textBoxUsername.Text.Length > 0) {
                errorProvider.Clear();
            }
            if (textBoxPassword.Text.Length == 0) {
                errorProvider.SetError(textBoxPassword, "Please enter your password");
                return;
            } else if (textBoxPassword.Text.Length > 0) {
                errorProvider.Clear();
            }
            this.Controls.Clear();
            Start.ActiveForm.Size = new System.Drawing.Size(500, 560);
            this.Controls.Add(new UserControlForm());
        }
        public string username {
            get {
                return this.textBoxUsername.Text;
            }
            set {
                this.textBoxUsername.Text = value;
            }
        }
        public UserControlStart(string test)
        {
            this.textBoxUsername.Text = xc;
        }
    }
}


My issue is that I don't get the username and password value in my other class

Thanks in advance!
Posted
Updated 12-Apr-11 23:41pm
v3
Comments
Sandeep Mewara 13-Apr-11 5:33am    
Not sure on what are you trying to ask here putting all these code. You getting some issue? error? Stuck?
Member 7762422 13-Apr-11 5:36am    
My issue is that I don't get the username and password value in my other class

The problem is that you haven't displayed the class:
UserControlStart p1 = new UserControlStart();
MessageBox.Show(p1.username);
All that does is constructs an instance of the control, and try to display a property from it. Since the control is at no point actually displayed to the user, he can't fill in any details.

Instead, you have probably put an instance of your control onto your form in the designer. Use that instance by name, and it should work. The code is probably:
MessageBox.Show(userControlStart1.username);
if you have used the standard Visual Studio names without making then more relevant to you program.
 
Share this answer
 
Hi Member 7762422,

You can create a public variable and an overloaded constructor in the second class like this :

public string Uname;

Other_Class (string UserName)
{
    Uname = UserName
}


and when calling it from the initial class you can use :

Other_Class other = new Oter_Class(textBoxUsername.Text)


I hope this help,
:)
 
Share this answer
 
Comments
Member 7762422 13-Apr-11 6:20am    
Thanks i tried this solution: I've got this error: The name 'textBoxUsername' does not exist in the current context
Michael Waguih 13-Apr-11 6:24am    
That was the name you have given to your textBox ... replace it with the textBox name you gived to it.
Member 7762422 13-Apr-11 6:28am    
Probabaly I don't know what you mean, could you give a example?
Two things.
1. In this code:
C#
public UserControlStart(string test)
{
  this.textBoxUsername.Text = xc;
}

you pass in a string test but never use it, so why pass it? If your code is full of redundant code, it makes it much harder to debug.

2. You have:
C#
public string xc, yc;

they should be private NOT public. I suspect that you changed the access level at some time whilst trying to get this to work but it is a very, very bad idea to have public members. Use public properties instead, if you want access from outside your class.

If I have correctly interpreted what you are trying to do you want the user to Login and then display another Form for your application and I think that this is what you are trying to do with this code:
C#
this.Controls.Clear();
Start.ActiveForm.Size = new System.Drawing.Size(500, 560);
this.Controls.Add(new UserControlForm());

What is Start as in <big>Start</big>.ActiveForm.Size? What is UserControlForm, and why on earth are you trying to insert it into the Controls collection of your UserControlStart?

You have references to these in your code without explaining what they are and what they are supposed to do and still expect people to be able to diagnose your problem and help you.

From what I am able to discern from your code you are approaching this in entirely the wrong way. The normal way would be to have a Login Form/Dialog, show that and get the users credentials, close it and then open the main Form for the application. The Login Dialog and the Main Form should be entirely separate not entangled as you seem to be doing. The Login Dialog should handle everything to do with authenticating the user, and the username and password should be discarded as soon as they are verified. They should NEVER, EVER be passed around in your application (particularly the password) as this leaves the system wide open to hacking.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900