0

I'm working on an assignment for a course on C#/.NET development. I've been asked to create a basic database driven application for a book store. The application needs to allow the store to add clients, make sales, purchase stock and report on sales performance. The application must also support multiple users, though not at the same time... just differentiate between an admin user and a standard user.

I feel I've made good progress, however I've become stuck and require some assistance from those more knowledgeable. My application works thus far, though I have not created the database as yet... My application, however, is less than ideal.

The application runs in a single window, with multiple User Controls which I send to the front as required.

Two examples are provided below.

This is the Login User Control (default)

enter image description here

This is the NewSale User Control

enter image description here

As can be seen in the first image, the Login screen, users are required to log in before they can interact with the rest of the application. I've achieved this by defining three "runLevels"; 0 - User Not Logged in; 1 - Standard User Logged In; 2 - Admin User Logged In.

If the runLevel = 0, the application is "lockedDown". If the runLevel = 1, the user is logged in but unable to perform administrative functions. If the runLevel = 2, the user is logged in and can perform all actions.

I've defined an event tied to the button btn_loguserin, as such;

private void btn_loguserin_Click(object sender, EventArgs e)
    {
        string userName;
        string userPassword;

        userName = txtbx_username.Text;
        userPassword = txtbx_password.Text;

        if (userName == "" || userPassword == "")
        {
            txtbx_username.Text = "MISSING INPUT";
        }
        else
        {
            LogIn loginStatus = LogIn.getInstance();
            byte runLevel = loginStatus.LogMeIn(userName, userPassword);
            if (runLevel > 0)
            {
                Form1 form1 = Application.OpenForms["Form1"] as Form1;
                if (form1 == null)
                {
                    Console.WriteLine("Form is null!");
                    // !!! Throw an exception here !!!
                }
                else
                {
                    form1.runLevelChecker();
                }
            }
            else
            {
                txtbx_username.Text = "INVALID LOGIN";
            }

I am not happy with the method by which I've achieved this. More specifically, this line;

Form1 form1 = Application.OpenForms["Form1"] as Form1;

I'd rather have my LogIn class handle this task, thereby reducing the amount of code in my individual User Controls to just required events.

However, I don't know how to call a method inside the default Form1 (or any form for that matter) from a class. The line of code above does not work if inserted into a class.

I apologise if I have not provided enough information here, still very new to programming. I will provide more details on request.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
RHSkinner
  • 37
  • 7
  • Form1 anotherForm = new Form1(); --- anotherForm.Show(); Does that help? – Prateek Shrivastava Apr 17 '18 at 07:29
  • There is no "default" Form1. You could either declare the instance as static, or pass the reference to whatever you want. – babu646 Apr 17 '18 at 07:34
  • you need to design what you will need where and then plan accordingly - maybe you need a static class all the others can access that your form1 can set various things like the access level for all the others to see – BugFinder Apr 17 '18 at 07:41
  • Let me know if it isn't a duplicate, either pass the form/class to the other form so you have access to it or use events – Jeremy Thompson Apr 17 '18 at 08:39

0 Answers0