C#验证多个TextBox

时间:2013-07-24 15:37:26

标签: c# textbox registrykey

我在页面上有几个TextBox。文本框的值是从创建的注册表项中提取的。我有一个更新按钮,用户可以按此更新注册表中的值。我需要添加功能,以便如果他们更改一个或多个文本框中的值,错误提取器将显示在他们更改的文本框旁边。我知道我可以为每个文本框编写代码,但这会很麻烦而且很长。有没有办法可以使用foreach循环?

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();
}

感谢您的时间,

-S

1 个答案:

答案 0 :(得分:0)

您可以将所有方框连接到相同的逻辑

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

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