0

I have several TextBoxes on a page. Values for the textboxes are pulled from registry keys that are created. I have an update button that the user can press to update the values in the registry. I need to add functionality so that if they change a value in one or multiple textboxes an errorprovider would show up next to the textbox they changed. I know I can write code for each and every textbox, but that would be messy and long. Is there a way that I could use a foreach loop?

private void tabPage1_Enter(object sender, EventArgs e)
    {

        //            Root: HKLM64; Subkey: "Software\Microsoft\Windows NT\CurrentVersion\Winlogon";ValueType: string; ValueName: "AutoAdminLogon"; ValueData: "1";Check: IsWin64
        //          Root: HKLM64; Subkey: "Software\Microsoft\Windows NT\CurrentVersion\Winlogon";ValueType: string; ValueName: "DefaultUserName"; ValueData: "mydealerlot";Check: IsWin64
        //        Root: HKLM64; Subkey: "Software\Microsoft\Windows NT\CurrentVersion\Winlogon";ValueType: string; ValueName: "DefaultPassword"; ValueData: "sdc1234";Check: IsWin64
        //      Root: HKLM64; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";ValueType: dword; ValueName: "EnableLUA"; ValueData: "0";Check: IsWin64

        AutoAdminLogin = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\SystemRestart", "AutoAdminLogon", null);
        DefaultUserName = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\SystemRestart", "DefaultUserName", null);
        DefaultDomain = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\SystemRestart", "DefaultDomain", null);
        DefaultPassword = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\SystemRestart", "DefaultPassword", null);
        SymbolicName = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\RemoteAgent", "SymbolicName", null);
        ServerAPIKey = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\RemoteAgent", "ServerAPIKey", null);


        if (String.IsNullOrEmpty(AutoAdminLogin))
        {
            AutoAdminLogin = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "AutoAdminLogon", null);
        }

        if (String.IsNullOrEmpty(DefaultUserName))
        {
            DefaultUserName = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName", null);
        }

        if (String.IsNullOrEmpty(DefaultDomain))
        {
            DefaultDomain = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultDomain", null);
        }
        if (String.IsNullOrEmpty(DefaultPassword))
        {
            DefaultPassword = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultPassword", null);
        }
        if (String.IsNullOrEmpty(SymbolicName))
        {
            SymbolicName = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\RemoteAgent", "SymbolicName", null);
        }
        if (String.IsNullOrEmpty(ServerAPIKey))
        {
            ServerAPIKey = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MDL\RemoteAgent", "ServerAPIKey", null);
        }

        if (String.IsNullOrEmpty(DefaultUserName))
        {
            DefaultUserName = Environment.GetEnvironmentVariable("USERNAME");
        }

        if (String.IsNullOrEmpty(DefaultDomain))
        {
            DefaultDomain = Environment.GetEnvironmentVariable("USERDOMAIN");
        }

        if (AutoAdminLogin == null || AutoAdminLogin != "1")
            AutoAdminLogin = "0";

        tbUserName.Text = DefaultUserName;
        tbDomain.Text = DefaultDomain;
        tbPassword.Text = DefaultPassword;
        tbSymbolicName.Text = SymbolicName;
        tbServerAPIKey.Text = ServerAPIKey;

        cbAutoLogin.Checked = (AutoAdminLogin == "1") ? true : false;

        compareLoginUsername();

        compareLoginPassword();
}

Thank you for your time,

-S

4

1 回答 1

0

您可以将所有盒子连接到相同的逻辑

box1.TextChanged += TextchangedHandler();
box2.TextChanged += TextchangedHandler();
box3.TextChanged += TextchangedHandler();

处理程序可以处理如何处理文本框、其数据等

于 2013-07-24T19:00:41.190 回答