Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:53:17+00:00 2026-05-27T10:53:17+00:00

I have a OOP question. I have 3 classes. Program (main class), login form

  • 0

I have a OOP question. I have 3 classes. Program (main class), login form and form1.
program runs login that checks for authentication, if this is successful form1 is then run while login closes.
Now the problem I have is, what if I need to pass a variable value to my form1?
The reason is it I wish to use the username login as a variable, pass it to my form1 that then runs the appropiate code to execute specific things.
Basically admin would have full access to controls, but a normal user would have limited access.

here’s my code excluding my form1(not needed unless someone wish’s to see it).

namespace RepSalesNetAnalysis
{
public partial class LoginForm : Form
{
    public bool letsGO = false;
    public LoginForm()
    {
        InitializeComponent();
    }

    private static DataTable LookupUser(string Username)
    {
        const string connStr = "Server=server;" +
                            "Database=dbname;" +
                            "uid=user;" +
                            "pwd=*****;" +
                            "Connect Timeout=120;";


        const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
        DataTable result = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    result.Load(dr);
                }
            }
        }
        return result;
    }

    private void buttonLogin_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(textUser.Text))
        {
            //Focus box before showing a message
            textUser.Focus();
            MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
            textUser.Focus();
            textPass.Clear();
            return;
        }
        else if (string.IsNullOrEmpty(textPass.Text))
        {
            textPass.Focus();
            MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            textPass.Focus();
            return;

        }
        //OK they enter a user and pass, lets see if they can authenticate
        using (DataTable dt = LookupUser(textUser.Text))
        {
            if (dt.Rows.Count == 0)
            {
                textUser.Focus();
                MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textUser.Focus();
                textUser.Clear();
                textPass.Clear();
                return;
            }
            else
            {
                string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
                string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB

                Console.WriteLine(string.Compare(dbPassword, appPassword));

                if (string.Compare(dbPassword, appPassword) == 0)
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    //You may want to use the same error message so they can't tell which field they got wrong
                    textPass.Focus();
                    MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textPass.Focus();
                    textPass.Clear();
                    return;
                }
            }
        }
    }

    private void emailSteve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start("mailto:stevesmith@shaftec.co.uk");
    }
}

and heres my main class

namespace RepSalesNetAnalysis
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new LoginForm());

        LoginForm fLogin = new LoginForm();
        if (fLogin.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Form1());
        }
        else
        {
            Application.Exit();
        }
    }
}
}

So to summarise, I need to pass a value from my login class to my form1 class so i can use a condition inside form1 to limit the users interaction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:53:18+00:00Added an answer on May 27, 2026 at 10:53 am

    You need to expose a property in the LoginForm, and the pass it to the Form1 as a parameter in the constructor. Here are the steps to do it:

    1) Add the following code into your LoginForm class:

    public string UserName
    {
        get
        {
            return textUser.Text;
        }
    }
    

    2) Then, change your Form1’s constructor to receive the username as a parameter:

    private _userName;
    public class Form1(string userName)
    {
        _userName = userName;
    }
    

    3) Finally you only need to construct Form1 with the desired parameter:

    namespace RepSalesNetAnalysis
    {
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new LoginForm());
    
            LoginForm fLogin = new LoginForm();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form1(fLogin.UserName));
            }
            else
            {
                Application.Exit();
            }
        }
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a noob question. If I have a ViewController and inside that class
I have a general OOP question. If I have the following classes in C#
Consider this example (typical in OOP books): I have an Animal class, where each
I have a question regarding as3 listeners, and instances of a class. The main
I have a solid understanding of most OOP theory but the one thing that
It seemed like this question should have been asked before, but searching found nothing.
The problem is mostly OOP design problem. I have a class which handles the
Quick question. I have been Googling this all morning, but it's either not there,
Foreword tl;wr: This is a discussion. I am aware that this question is more
First of all, I apologize for yet another interface question. I thought that this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.