Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have windows application based on accdb database in which I have table with usernames and passwords
After logging in on login form with proper username and password another form opens and I want to see in textbox the name of that logged user, or does it have to be textbox I dont' know.

How to do this?
Posted
Comments
V. 25-Feb-11 8:22am    
How does the application go from the login form to the main form?
- it creates the object and calls ShowDialog?
- it starts a new application
- it calls a service
- ...

Depending on the above possibilities you need to find a way to pass the string (username) to the second module. from there it is nothing more then setting the mytxtbox.Text property to the string value passed. My guess is you call the ShowDialog on the object in which case you can pass the username with the constructor.
shonezi 25-Feb-11 12:35pm    
when logged in it opens another form, not showdialog but
form2 frm = new form2();
frm.Show();

that code

Expose the username as a public get-only property on the Login form. Once the login form returns, read out this string and then pass it to your main form (either via the constructor or via a similar public settable property).

Alternatively if your application has a public static class that you use to hold global state data, store it in a property there so you can access it throughout the app.
 
Share this answer
 
v2
Comments
Espen Harlinn 25-Feb-11 9:00am    
That's a logical approach, good answer :)
shonezi 25-Feb-11 12:53pm    
I am new to visual studio, can you if you don't mind be little specific in code please
I would use a Singleton [^]object. To do that, create a class like this:
public class User
{
   private static User instance;
   private User() {}
   public static User Instance
   {
      get
      {
         if (instance == null)
         {
            instance = new User();
         }
         return instance;
      }
   }
}

You can even extend this class to do validation/authentication/authorization... of the user.
In the simple form, I would assign the value of this logged in user and later on retrieve it.
 
Share this answer
 
Create a public static class in your application use that to manage state...

Static Class--
public static class GlobalState
{
  public static string LogedInUse
}

Now change your login button click as
void btnLogin_Click(object sender,EventArg e)
{
 if(ValidateLogin())
{
     GlobalState.LoggedInUser=txtUserName.Text;
}
else
{
// Show some message
}
}


Hope it will usefull.

--Pankaj
 
Share this answer
 
Comments
shonezi 25-Feb-11 14:20pm    
I did what you said, but tell me this txtUserName.Text refers to login form or on another form where I have textbox in which I want to see who is logged on
shonezi 25-Feb-11 14:25pm    
sorry I havent explained better, I have login form with textbox username and I have main form with also textbox username, in this main form I want to see username that is logged in, I have done what you said and the code is ok, just I need how to see on main form in textbox the name of logged user
tthanks
pankajupadhyay29 26-Feb-11 3:38am    
make change on main form and on load event of main page write username.Text= GlobalState.LoggedInUser;
shonezi 26-Feb-11 5:56am    
I did this but it says that GlobalState doesnt exist in current context
pankajupadhyay29 26-Feb-11 6:33am    
if u create GlobleState in same name space and a public class it could not be there. this is general problem with referencing check for that.

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