I am not sure what I am doing wrong, but I am starting to feel very stupid. I am fairly new to C# so be gentle :) .

I have an If Statement that is not working and I am not sure why. Here is my code

if ("Admin" == user)
    btnAdmin.Enabled = true;
else
    btnAdmin.Enabled = false;

For some reason it is always coming back false, no matter what. I need the button to only be active if the user name is Admin.

As a side note, I know this is not very secure, but I just need to get this done and will make it more secure later.

Thanks.

QUESTIONS:
1) Where/when are you executing this code?
2) Where/how are you setting the value of the string "user"?
3) Can you include the whole code text?

commented: Good questions. +10

Yeah, here is the rest of the code block.

public partial class Main : Form
    {
        private string user;
        public string User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }
        public Main()
        {
            InitializeComponent();

            String admin = "Admin";

            if ("Admin" == user)
            
                btnAdmin.Enabled = true;
            
            else
            
                btnAdmin.Enabled = false;
            
        }

The user variable is being pulled from the Login form in the form of a textbox.

I think the other question is answered in the code block.

Thanks.

My guess is you are never actually setting the variable "user" or User property. Where is the code that assigns the TextBox value? e.g.

User = textBox1.Text;

Also, you probably want to rewrite your if statement to be:

if (User == admin)

instead of using the literal "Admin", since you already assigned it to variable 'admin', which is never used in this example.

zip the entire solution and attach it to the post so we can correct and post back

My guess is you are never actually setting the variable "user" or User property. Where is the code that assigns the TextBox value? e.g.

User = textBox1.Text;

Also, you probably want to rewrite your if statement to be:

if (User == admin)

instead of using the literal "Admin", since you already assigned it to variable 'admin', which is never used in this example.

I am setting the variable on another form and passing it over. I know it is passing it because I tested it.

Also, I tried setting

if (user == admin)

and that did not work either. I just have not removed the variable declaration.

zip the entire solution and attach it to the post so we can correct and post back

Here you go. The form that I am have trouble with is Main.cs.

Thanks.

I am setting the variable on another form and passing it over. I know it is passing it because I tested it.

Show the line of code you are using to pass the variable to the form.

GUESS: If you passing in the variable in the constructor, this won't work because the reference will be reset. After your call to ShowDialog(), reset your variable's content before the Form object is destroyed, e.g.:

frm.ShowDialog();
user = frm.user;
bool isValid = this.IsValidatedUser(this.txtUser.Text, this.txtPass.Text );
            
            if ( isValid )
                {
                    String User = this.txtUser.Text;

                    Main main = new Main();

                    main.User = User;

                    main.Show();
                    this.Hide();
                } 

                else

                {

                    MessageBox.Show("Username and Password Incorrect.  Please try again.");

                }

This is the block from the Login form that is collecting the information. If the login is successful it passes the variable the the Main form. The Main form then uses the Get Statement posted above and places the sting into the user variable. I hope I am making sense.

This is a little weird how you are doing this--no big deal though. Can you paste the entire code or zip it up and attach as Serkan suggested?

GUESS: If you passing in the variable in the constructor, this won't work because the reference will be reset. After your call to ShowDialog(), reset your variable's content before the Form object is destroyed, e.g.:

frm.ShowDialog();
user = frm.user;

Does it make a difference if I am just hiding the parent form? I am not actually destroying, am I?

Does it make a difference if I am just hiding the parent form? I am not actually destroying, am I?

What you say is true. Are you resetting the variable to the form's data as suggested?: user = main.user

What I don't understand is what the form is capable of doing since you are hiding it immediately after a modeless call to the Show() method, which prevents any user interaction with the form. e.g.

main.Show();
main.Hide();

This is a little weird how you are doing this--no big deal though. Can you paste the entire code or zip it up and attach as Serkan suggested?

I did. It should be in a post shortly after they requested it.

Thanks.

P.S. If there is a better way to do this, please let me know. It took me over an hour to get the variable to pass from form to form. Like I said, I am very new to c#.

Using the name "Main" for your form: I highly recommend you change this name to be something else that is more meaningful. Only because Main() is a reserved entry point symbol to the execution of an application and it is confusing (though I admit I didn't know you could do what you have done anyway).

What I don't understand is what the form is capable of doing since you are hiding it immediately after a modeless call to the Show() method, which prevents any user interaction with the form. e.g.

The form that this particular code was on is not the main form. It was the login form. So, I am creating the main form, showing the main form, and then hiding the login form.

Using the name "Main" for your form: I highly recommend you change this name to be something else that is more meaningful. Only because Main() is a reserved entry point symbol to the execution of an application and it is confusing (though I admit I didn't know you could do what you have done anyway).

LOL. I named it main because when I started it was the first (read main) form for the program.

Thanks again for your help.

I am going to try and make this a little more clear. I have two form. One is the login form. The other is the main form. I am passing a variable from the login form to the main form with the following code.

Login form code

private void btnLogin_Click(object sender, EventArgs e)
        {
            bool isValid = this.IsValidatedUser(this.txtUser.Text, this.txtPass.Text );
            
            if ( isValid )
                {
                    String User = this.txtUser.Text;

                    Main main = new Main();

                    main.User = User;

                    main.Show();
                    this.Hide();
                } 

                else

                {

                    MessageBox.Show("Username and Password Incorrect.  Please try again.");

                }

If the login is successful the main form is created and the variable user is passed.

The main form picks up this variable and an if statement is executed to enable/disable a button with the following code.

Main form code

public partial class Main : Form
    {
        private string user;
        public string User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }
        public Main()
        {
            InitializeComponent();

            String admin = "Admin";

            if ("Admin" == user)
            
                btnAdmin.Enabled = true;
            
            else
            
                btnAdmin.Enabled = false;
            
        }

For some reason the If Statement is always coming back false, so the button is always disabled.

Thanks.

Here is the problem: you are setting the button.Enabled property in the Main() constructor. However, you are setting the value of user after you instantiate the Main form, which means the button's if statement has already been evaluated before the User variable gets set to "Admin".

SUGGESTION/SOLUTION: Move the "if' block to check for "Admin" to the form's Load event, which will be called after your call to Show(), allowing the variable to have been set in between. Does this make sense to you?

Here is the problem: you are setting the button.Enabled property in the Main() constructor. However, you are setting the value of user after you instantiate the Main form, which means the button's if statement has already been evaluated before the User variable gets set to "Admin".

Ok, I understand. I thought I was setting the variable before the Main() constructor occurred. If I move the code that sets the variable inside the Main() constructor, before the IF statement, will that work?

Thanks.

Here is the modification:

public Main()
        {
            InitializeComponent();

            //String admin = "Admin"; // not being used?
            this.Load += new System.EventHandler(this.Main_Load);
        }
        private void Main_Load(object sender, EventArgs e)
        {
            if ("Admin" == user)

                btnAdmin.Enabled = true;

            else

                btnAdmin.Enabled = false;
        }
commented: Thanks for the help. +1

Ok, I understand. I thought I was setting the variable before the Main() constructor occurred. If I move the code that sets the variable inside the Main() constructor, before the IF statement, will that work?

Thanks.

No, you cannot just move code inside the Main() constructor; unless, you pass in the user var into the Main constructor: e.g. Main(string user), which will allow the "if" block to see the current value. Does that make sense?

Here is the modification:

public Main()
        {
            InitializeComponent();

            //String admin = "Admin"; // not being used?
            this.Load += new System.EventHandler(this.Main_Load);
        }
        private void Main_Load(object sender, EventArgs e)
        {
            if ("Admin" == user)

                btnAdmin.Enabled = true;

            else

                btnAdmin.Enabled = false;
        }

That works.

I want to make sure I understand what is actually happening now.

The Main() constructor is happening, during which the variable is being set. Then the Load event happens and evaluates the IF Statement. Is this accurate?

Thanks for all your help.

No, you cannot just move code inside the Main() constructor; unless, you pass in the user var into the Main constructor: e.g. Main(string user), which will allow the "if" block to see the current value. Does that make sense?

Yes, that makes sense. I would have to make the Main() constructor aware of the variable in order for the if statement to evaluate properly.

Yes, correct. Another way you could do it is to:

Main main = new Main();
//set value here
if (main.User = "Admin")
    main.btnAdmin.Enabled = true;
else
    main.btnAdmin.Enabled = false;
main.Show();

You would of course need to make the button public to do it this way, and I don't suggest it, just pointing out yet another fix.

Try This Code and see

if(user.Equals("Admin"))
                btnAdmin.Enabled = true;
 else
                btnAdmin.Enabled = false;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.