2013-07-24 87 views
0

我在頁面上有幾個文本框。文本框的值從創建的註冊表項中提取。我有一個更新按鈕,用戶可以按來更新註冊表中的值。我需要添加功能,以便如果他們更改一個或多個文本框中的值,錯誤提供程序會顯示在他們更改的文本框旁邊。我知道我可以爲每個文本框編寫代碼,但那會很麻煩而且很長。有沒有一種方法可以使用foreach循環?C#驗證多個文本框

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

+0

在.NET TextBox控件上編寫一個包裝類。定義一些屬性或成員,在其中存儲任何環境變量的默認值,在構造函數中設置該屬性..等等..什麼說? –

+0

你想要發生什麼,什麼時候發生?這是否應該發生在驗證?或者當按鈕被點擊時一次全部?你怎麼知道價值已經改變...與變量相比? – DonBoitnott

+0

,因爲用戶會鍵入它會檢查textbox.text是否等於註冊表項。如果沒有,那麼errorprovider會彈出。 – sstrickland

回答

0

您可以連接了所有箱子相同的邏輯

而且處理器可以處理如何處理文本做盒子,其數據等