The name '' does not exist in the current context

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    The name '' does not exist in the current context

    I am having a problem with this code

    Code:
    private void loginButton_Click(object sender, System.EventArgs e)
            {
                if (userTB.Text == "")
                {
                    MessageBox.Show("You have not entered a username");
                    return;
                }
                else if (userPass.Text == "")
                {
                    MessageBox.Show("You have not entered a password");
                    return;
                }
                MessageBox.Show("You have been logged in!");
            }
    The controls themselves are declared and added to the tab, but how come the compiler says that they do not exist?

    Code:
    private void FirstLogin()
            {
                TabPage loginTab = new TabPage("Login");
                
                loginTab.BackColor = SystemColors.Control;
    
                tabControl1.TabPages.Add(loginTab); // add the tab
    
                Label loginLabel = new Label();
                loginLabel.Text = "You are required to log in before you can access the system.";
                loginLabel.AutoSize = true;
    
                Label userName = new Label();
                userName.Text = "Username: ";
                userName.AutoSize = true;
                userName.Location = new System.Drawing.Point(0, 30);
    
                TextBox userTB = new TextBox();
                userTB.Location = new System.Drawing.Point(61, 28);
    
                Label password = new Label();
                password.Text = "Password: ";
                password.AutoSize = true;
                password.Location = new System.Drawing.Point(0, 60);
                
                TextBox userPass = new TextBox();
                userPass.Location = new System.Drawing.Point(61, 58);
                userPass.PasswordChar = '*';
    
                Button loginButton = new Button();
                loginButton.Location = new System.Drawing.Point(0, 90);
                loginButton.Text = "Login";
                loginButton.Click += new System.EventHandler(loginButton_Click);
    
                loginTab.Controls.Add(loginLabel);
                loginTab.Controls.Add(userName);
                loginTab.Controls.Add(userTB);
                loginTab.Controls.Add(userPass);
                loginTab.Controls.Add(password);
                loginTab.Controls.Add(loginButton);
            }
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    #2
    Can anybody answer this? It's for a school project

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      You are dynamically creating your controls in FirstLogin. Those variables in that function are scoped only for that function. You can not refer to them outside of that function. I don't know C# so I don't know what attributes the controls have but it must have some sort of name or ID attribute that you can assign.

      Comment

      Working...