7

Hi can I'm very new to windows forms. Here I want to maintain state (like session in web applications) in windows forms.

Actually i want to store user login details in session. But i think there is no concept of session in winforms. So what is the alternative method to handle this type of situation.

Regards, Nagu

8 Answers 8

6

There is no concept of Session variables in windows forms. What you can do is:

  1. Create a internal class that holds the User name and password and any other variables and enumerations needed across the application (Something like Common.cs). These can be accessed through public properties across the application.

  2. Have a parameterized constructor for all the forms and send the user name and the password whenever you are showing the form.

2
  • Still I'm facing the same problem.. i declared two variables in login.cs like public string loginid = "123456"; after that i try to access in home.cs like login lg = new login() MessageBox.Show(lg.loginid); It is showing null value.. What is the problem
    – Nagu
    Jul 27, 2009 at 7:22
  • @Danish - Can you provide any structure ? Dec 21, 2015 at 10:36
4
public class MyForm : Form
{
     private string userName;
     private string password;
}

Since windows forms are statefull (opposed to stateless for web forms), you can just use a field in your Form class.

2
  • can i use this values in all other pages in the application? if so how can i get username and password in home.cs page?
    – Nagu
    Jul 27, 2009 at 4:46
  • Then you would need to make the fields internal or add properties to expose them. Jul 27, 2009 at 4:49
2

You need to think more in terms of scope than session; as long as an object remains in scope you will be able to pull values from its public properties/fields.

In your case it would make sense to store the user details in a static class:

public static class LoginInfo
{
    public static string UserID;
}

Now you can access the UserID simply from anywhere in your code:

MessageBox.Show(LogInfo.UserID);
1

In winforms you can use variables that are exposed to other forms through methods or properties.

You can also use static variables.

4
  • thank you but here how to i set the startup page? I want to set login.aspx as starup page after successfull authentication I want to redirect to home page
    – Nagu
    Jul 27, 2009 at 4:41
  • 1
    woa! @Nagu: I wonder how are you using aspx in winform application?
    – Hemant
    Jul 27, 2009 at 4:48
  • login.aspx? Are you creating a web application?
    – rahul
    Jul 27, 2009 at 4:49
  • sorry sorry just its login.cs only
    – Nagu
    Jul 27, 2009 at 6:44
1

In the following example, you would have a controller for each window or group of windows. The controllers would be passed to one another depending on how they need to collaborate (what knowledge they need to share, etc). The important thing is to keep your application state in the controllers and limit the windows to handling user input and events.

// pseudocode, because I do not know WinForms that much
class MainController
{
    private Guid securityToken;

    public Guid SecurityToken
    {
        get { return securityToken; }

        set { securityToken = value; }
    }
}

class LoginWindowController
{
    MainController mainController;
    LoginWindow    loginWindow;

    public LoginWindowController(MainController mainController)
    {
        this.loginWindow    = new LoginWindow(this);
        this.mainController = mainController;
    }

    public void Show()
    {
        loginWindow.IsVisible = true;
    }

    public void HandleLogin()
    {
        Guid token = 
            myobject.Authenticate(loginWindow.Username, loginWindow.Password);

        if (token != Guid.Empty)
        {
            mainController.SecurityToken = token;
        }   
    }
}
0

In reply to your comment to my first reply:

You are creating the new instance of the Login form. How is that supposed to have values. It is a Login form and hence I believe you will be closing it as the user enters user name and password and clicks OK or whatever.

Then, there is no way you can get the values from the Login form as it is closed. If you need to stick to this approach, this could be a way:

  1. Do not close the Login form, just hide it.
  2. Pass the current instance to the next form. Like this: In Login form:

    NextForm nxt = new NextForm(this);

The constructor of NextForm will look like:

public NextForm(LoginForm frm){
// Code here
}

Now in NextForm, you can access the properties through "frm".

0

from a program i was using with a login form to store global variables and to store the password as a secure string. Within the program I am able to "run as" a specific user when I call processes. You can use it for other things besides process.start.

//to run process as another user

//create these global variables on the first
//form or piece of code in your program
class usernameGlobalVariable
    {
        public static string var = "";
    }
    class passwordGlobalVariable
    {
        public static SecureString var;
    }

// use these as event handlers for text fields
//for your login form
private void usernameTextBox_TextChanged(object sender, EventArgs e)
{
    usernameGlobalVariable.var = usernameTextBox.Text;
}

private void passwordTextBox_TextChanged(object sender, EventArgs e)
    {
    SecureString passWord = new SecureString();
        foreach (char c in passwordTextBox.Text.ToCharArray())
        {
        passWord.AppendChar(c);
        }
    passwordGlobalVariable.var = passWord;
    }



//put this on form that launches program
//this assigns variables for process.start
//change fileName to path and name of program
// use \\ in paths
string fileName = "c:\\hdatools\\Ping2.exe";
string arguments = "";
string domain = "domain";

//start the process
//put this on the page along w the above variables that
//launches the app as another user
//the .var variables are global
{
    Process.Start(
    fileName,
    arguments,
    usernameGlobalVariable.var,
    passwordGlobalVariable.var,
    domain);
}
-4

It's unclear to me whether you are talking about a web application or a stand along application based upon one of your responses. If you are talking about a web application, you can use the Session properties on the Page object.

It would set the variables like this:

Session["username"] = "Username";
Session["fullname"] = "User's full name";

You could then access like:

lblGreetings.Text = "Hi " + Session["fullname"];

Is that what you were after?

2
  • The post is about windows form, and not web apps.
    – Bhaskar
    Jul 30, 2009 at 13:16
  • this is not to used in winforms valid in web application only Dec 21, 2015 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.