Emil Dumbazu
Emil Dumbazu

Reputation: 662

Multiple users in Windows Forms

I'm working on a project and i need some implementention ideas. So far i used windows forms. The application will be used by different users on the same pc. I'm not a good relater so i'm gonna give you a scenario:

1. The application starts by showing a login form.

2. The user "John" enters the application and makes some modifications in the database through a form.

3. "John" quits the form. At this point I want that login form to be shown again where user "Brad" should be able to log in.

4. "Brad" should be able to see the modification made by "John" and it should be able to make some modifications too.

Please help me with some examples.

Later edit: I've tried this:

form_login login = new form_login();
DialogResult result = login.ShowDialog();

if (result == DialogResult.OK)
{
  Application.Run(new main_page());
}
else if (result == DialogResult.Yes)
{
  Application.Run(new admin_page());
}
else if (result == DialogResult.No) // Back button 
{
  Application.Run(new form_login());
}
else
{
  Application.Exit();
}

Later edit:

The problem I have is when the user logs off . The application stops but what I want is to point me to login form.

Upvotes: 2

Views: 4381

Answers (2)

LarsTech
LarsTech

Reputation: 81610

You can add this to your Main starting point. Basically, you need to be able to loop through the process again once the user gets passed the login form:

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);

  DialogResult running = DialogResult.OK;
  while (running == DialogResult.OK) {
    form_login login = new form_login();
    Application.Run(login);
    running = login.DialogResult;
    if (login.DialogResult == DialogResult.OK)
      Application.Run(new Form1());
      // or your other forms...
  }
}

This is assuming your login form has an OK and a Cancel button that sets those dialog results.

If the login works, then it launches the main form, Form1. When the user closes Form1, it starts the Login form again. If the user cancels the login, the application is exited.

Upvotes: 2

3Dave
3Dave

Reputation: 29041

Assuming that all changes are stored in the database, and the form is initialized with what's in the database, then you shouldn't have to do anything special.

This is like saying,

  1. John puts something in a box on a shelf in a closet, and leaves.
  2. Later, Brad comes along and looks in the box.
  3. How do I make sure that Brad sees what John put in the box?

The only thing to keep in mind is that your form needs to be initialized with what's in the database before any user sees it.

Also, it's unusual these days to have a multi-user system that is only accessed sequentially, with no more than 1 user at a time. If you're not sure if that will always be the case, it may make more sense to implement this as a web application.

However, if you're building something like a point-of-sale system, though, where you need hardware like bar code scanners or credit card readers, you should be fine with a winforms app - and, in fact, it makes more sense than a web app in that case - keeping all of the data in a central, shared database like MS SQL Server will take care of most of your synchronization issues.

Update

If you're really asking about how to keep the app running when a user logs out: Application.Run() should really only be called once, when the app starts. You'll launch your main form:

  using(var mainForm = new MainForm())
    Application.Run(mainForm);

The class MainForm might represent, for instance, your main data entry screen.

MainForm would then be responsible for determining if a user is logged and, if necessary, showing a modal login form. There are a few other posts that address this:

Using Multiple Forms in c#

Upvotes: 0

Related Questions