Question

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

Was it helpful?

Solution

You can wire up all boxes to same logic

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

And the handler can handle what to do with the text box, its data, etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top