无法调用对象,不知道为什么

时间:2018-02-04 12:53:56

标签: c# object

所以我有一个包含3个按钮的表单,"添加用户","删除用户","更改密码"。当您点击其中一个按钮时,我不想要显示新的表单,而是希望当前的更改并显示新的文本框&纽扣。问题是,当我对这些文本框和按钮进行编码时,我无法将它们作为private void BtnAddUser_Click(object sender, Routed EventArgs e)中的对象进行调用。

有人可以解释一下我做错了吗?

我得到的错误是:The name 'txtUsername', 'pbxPassword' and 'cboRangen' do not exist in the current context.

    public admin()
    {
        InitializeComponent();
    }

    private void btnAddUsers_Click(object sender, RoutedEventArgs e)
    {
        // Buttons verbergen
        btnAddUsers.Visibility = Visibility.Hidden;
        btnDeleteUsers.Visibility = Visibility.Hidden;
        btnChangePasswords.Visibility = Visibility.Hidden;

        // Back button toevoegen
        Button btnBack = new Button();
        grdAdmin.Children.Add(btnBack);
        btnBack.Content = "Back";
        btnBack.Visibility = Visibility.Visible;
        btnBack.Height = 30;
        btnBack.Width = 60;
        btnBack.Margin = new Thickness(180, 65, 0, 0);
        btnBack.VerticalAlignment = VerticalAlignment.Top;
        btnBack.HorizontalAlignment = HorizontalAlignment.Left;

        //Textboxes & labels toevoegen

        //lblUsername
        Label lblUsername = new Label();
        grdAdmin.Children.Add(lblUsername);
        lblUsername.Content = "USERNAME";
        lblUsername.Margin = new Thickness(20, 100, 0, 0);
        lblUsername.Padding = new Thickness(0, 5, 5, 5);
        lblUsername.Height = 30;
        lblUsername.Width = 100;
        lblUsername.FontSize = 10;
        lblUsername.SetValue(Label.FontWeightProperty, FontWeights.Bold);
        lblUsername.Opacity = 60;
        lblUsername.VerticalAlignment = VerticalAlignment.Top;
        lblUsername.HorizontalAlignment = HorizontalAlignment.Left;

        //txtUsername
        TextBox txtUsername = new TextBox();
        grdAdmin.Children.Add(txtUsername);
        txtUsername.Text = "";
        txtUsername.Margin = new Thickness(20, 130, 0, 0);
        txtUsername.Padding = new Thickness(10, 0, 0, 0);
        txtUsername.Height = 30;
        txtUsername.Width = 220;
        txtUsername.VerticalAlignment = VerticalAlignment.Top;
        txtUsername.HorizontalAlignment = HorizontalAlignment.Left;
        txtUsername.VerticalContentAlignment = VerticalAlignment.Center;

        //lblPassword
        Label lblPassword = new Label();
        grdAdmin.Children.Add(lblPassword);
        lblPassword.Content = "PASSWORD";
        lblPassword.Margin = new Thickness(20, 160, 0, 0);
        lblPassword.Padding = new Thickness(0, 5, 5, 5);
        lblPassword.Height = 30;
        lblPassword.Width = 100;
        lblPassword.FontSize = 10;
        lblPassword.SetValue(Label.FontWeightProperty, FontWeights.Bold);
        lblPassword.Opacity = 60;
        lblPassword.VerticalAlignment = VerticalAlignment.Top;
        lblPassword.HorizontalAlignment = HorizontalAlignment.Left;

        //pbxPassword
        PasswordBox pbxPassword = new PasswordBox();
        grdAdmin.Children.Add(pbxPassword);
        pbxPassword.Password = "";
        pbxPassword.Margin = new Thickness(20, 190, 0, 0);
        pbxPassword.Padding = new Thickness(10, 0, 0, 0);
        pbxPassword.Height = 30;
        pbxPassword.Width = 220;
        pbxPassword.VerticalAlignment = VerticalAlignment.Top;
        pbxPassword.HorizontalAlignment = HorizontalAlignment.Left;
        pbxPassword.VerticalContentAlignment = VerticalAlignment.Center;
        pbxPassword.PasswordChar = '*';

        //lblRang
        Label lblRang = new Label();
        grdAdmin.Children.Add(lblRang);
        lblRang.Content = "RANG";
        lblRang.Margin = new Thickness(20, 220, 0, 0);
        lblRang.Padding = new Thickness(0, 5, 5, 5);
        lblRang.Height = 30;
        lblRang.Width = 100;
        lblRang.FontSize = 10;
        lblRang.SetValue(Label.FontWeightProperty, FontWeights.Bold);
        lblRang.Opacity = 60;
        lblRang.VerticalAlignment = VerticalAlignment.Top;
        lblRang.HorizontalAlignment = HorizontalAlignment.Left;

        //cboRangen
        ComboBox cboRangen = new ComboBox();
        grdAdmin.Children.Add(cboRangen);
        cboRangen.Margin = new Thickness(20, 250, 0, 0);
        cboRangen.Height = 30;
        cboRangen.Width = 220;
        cboRangen.VerticalAlignment = VerticalAlignment.Top;
        cboRangen.HorizontalAlignment = HorizontalAlignment.Left;
        cboRangen.Items.Add("Directeur");
        cboRangen.Items.Add("Software Manager");

        //btnRegister
        Button btnAddUser = new Button();
        grdAdmin.Children.Add(btnAddUser);
        btnAddUser.Content = "Add User";
        btnAddUser.Visibility = Visibility.Visible;
        btnAddUser.Height = 30;
        btnAddUser.Width = 220;
        btnAddUser.Margin = new Thickness(20, 300, 0, 0);
        btnAddUser.VerticalAlignment = VerticalAlignment.Top;
        btnAddUser.HorizontalAlignment = HorizontalAlignment.Left;
        btnAddUser.Click += BtnAddUser_Click;
    }


    private void BtnAddUser_Click(object sender, RoutedEventArgs e)
    {


        MySqlConnection MySqlCon = new MySqlConnection(connectionStr.ConnectionString);

        try
        {
            if (MySqlCon.State == System.Data.ConnectionState.Closed) MySqlCon.Open();
            String query2 = "INSERT INTO egh.accounts (Username,Password,Rang) VALUES(@Username, @Password, @Rang)";
            MySqlCommand sqlCmd = new MySqlCommand(query2, MySqlCon);
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
            sqlCmd.Parameters.AddWithValue("@Password", pbxPassword.Text);
            sqlCmd.Parameters.AddWithValue("@Rang", cboRangen.SelectedValue);
            sqlCmd.ExecuteScalar();

            MessageBox.Show("User added!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            MySqlCon.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

提到的变量是btnAddUsers_Click()函数的本地变量。它们只存在于此函数中。

要访问它们,您有多种选择(包括但不限于):

  • 将局部变量转换为类成员
  • 找到另一种访问组件的方法
  • 在设计时添加组件,只需切换可见性。