简体   繁体   English

c#Groupbox属性更改

[英]c# Groupbox Property Change

I have a Groupbox on a windows form with the enabled property set to false. 我在Windows窗体上有一个Groupbox,其enabled属性设置为false。 I want to enable the Groupbox in code after verifying a username and password. 我想在验证用户名和密码后在代码中启用Groupbox。 Whe I call the Groupbox.Enabled = true method the state of the Groupbox does not change. 当我调用Groupbox.Enabled = true方法时,Groupbox的状态不会更改。 I am calling from another windows form and I have instantiated the form prior to making the call to enable the Groupbox. 我正在从另一个Windows窗体进行调用,并且在进行调用以启用Groupbox之前已经实例化了该窗体。 Am I missing something? 我想念什么吗?

namespace ShadowKey
{
    public partial class SetKey : Form
    {
        ConfigOptions cOptions = new ConfigOptions();

        public SetKey()
        {
            InitializeComponent();
        }

        private bool CompareStrings(string string1, string string2)
        {
            return string.Compare(string1, string2, true,     System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            Thread.Sleep(2000);
            string connection = @"Data Source=|DataDirectory|\EncrypterDB.sdf";
            SqlCeConnection dataCon = new SqlCeConnection(connection);
            dataCon.Open();
            string userText = tbUser.Text;
            string pwrdText = tbPwrd.Text;
            SqlCeCommand cmd = new SqlCeCommand("Select usr_name, usr_password FROM user_accounts WHERE usr_name='" + tbUser.Text + "' and usr_password='" + tbPwrd.Text + "'", dataCon);
            cmd.Parameters.Add(new SqlCeParameter("username", userText));
            cmd.Parameters.Add(new SqlCeParameter("password", pwrdText));

            SqlCeDataReader rdr = cmd.ExecuteReader();

            try
            {
                rdr.Read();
                if (rdr["usr_name"].ToString().Trim() == userText && rdr["usr_password"].ToString().Trim() == pwrdText)
                {
                    cOptions.gbEncryptionKey.Enabled = true;
                }
            }
            catch
            {
                MessageBox.Show("Invalid Username or Password!", "Login Error!");
            }
            rdr.Close();
            dataCon.Close();
        }

        catch
        {
            MessageBox.Show("You've thrown an exception!", "Error!");
        }

    }
}

} }

There is not a lot of code to go by but I will venture a guess. 没有太多代码需要经过,但是我敢猜测。 I suspect your LinkLabel is creating a new instance of ConfigOptions. 我怀疑您的LinkLabel正在创建ConfigOptions的新实例。 Then, when the Enabled property is set to true it is being set on the instance that was created when SetKey was instantiated. 然后,当Enabled属性设置为true时,将在实例化SetKey时创建的实例上对其进行设置。 This means that you are not updating the one that is being shown, but another one that is hidden. 这意味着您不是在更新所显示的内容,而是在更新隐藏的内容。 Put a breakpoint in the ConfigOptions constructor and see if it is being called more than once. 将断点放在ConfigOptions构造函数中,查看是否多次调用该断点。

I don't see when ConfigOptions gets displayed, but lets say it is displayed as the first form to open and you are now viewing the second form. 我看不到何时显示ConfigOptions,但是可以说它显示为要打开的第一个窗体,现在您正在查看第二个窗体。 The way to access a property on ConfigOptions would be: 访问ConfigOptions上的属性的方法是:

(Application.OpenForms[0] as ConfigOptions).ThePropertyToSet = PropertyValueHere;

Basically, you want to access the forms through Application.OpenForms based on the index(order) in which they were opened. 基本上,您想基于打开窗体的索引(顺序)通过Application.OpenForms访问窗体。 Doing the following does nothing, because you are not actually setting the property on the open ConfigOption form. 执行以下操作无济于事,因为您实际上并未在打开的ConfigOption窗体上设置属性。

ConfigOptions cOptions = new ConfigOptions();
cOptions.gbEncryptionKey.Enabled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM