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)
This is the NewSale User Control
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.