2015-10-26 33 views
-1

我是c#的新手。我有個疑問。讓我們來看一個實時的例子如何在Winform標籤/文本框中顯示POCO(類)文件的結果

  1. 假設我有一個登錄winform提交的用戶名和密碼和一個按鈕。
  2. 我有一個poco C#文件,在點擊按鈕後可以獲取用戶名和密碼的值。這個C#poco文件與表單文件無關。它是單獨的poco文件,它將處理登錄身份驗證
  3. 現在poco文件將生成結果說「登錄成功」或「登錄失敗」
  4. 現在,如何在winform上顯示poco文件的這個結果?

請給出正確的代碼,這將清除我的概念。

謝謝

+1

你有沒有試過任何代碼......? –

+0

是的,poco意味着一個類文件。但我不知道該怎麼做。導致每當我點擊按鈕數據去類文件,但不知道如何發送該文件的結果形成 – Akshay

+0

@Akshay你需要做更多的研究,並提供一些你面臨問題的示例代碼。請通過[this](http://stackoverflow.com/help/how-to-ask) –

回答

1

在按鈕的點擊處理程序,您的驗證類(如你把它叫做POCO文件)的認證方法可以被調用,結果可以顯示在任何方式,例如用消息框:

var loginResult = Authenticator.Authenticate(txtUserName.Text, txtPassword.Text, out userInfo); 

if (loginResult == LoginResult.Success) 
{ 
    // hide the authentication form, or unlock menu, toolbars, ... 
} 
else 
{ 
    MessageBox.Show("Invalid user name or password"); 
    // the user stays on the login form and needs to press the login button again for another attempt 
} 

的認證類可能看起來llike:

public enum LoginResult 
{ 
    Success, 
    InvalidUserNameOrPasword, 
} 

public class UserInfo 
{ 
    public int Id { get; set; } 
    publi string Name { get; set; } 
} 

public class Authenticator 
{ 
    public LoginResult Authenticate(string userName, string password, out userInfo) 
    { 
    // the logic to load the user from DB 
    } 
} 
+0

ok你的意思是successAuthentication在類文件中是靜態的嗎?如果它被設置,那麼它會進入if子句?將嘗試 – Akshay

+0

以及謝謝馬丁..它幫助我 – Akshay

1

假設你有一個名字Authenticate的方法我n你的POCO類需要使用userid和pasword並驗證它。指定該方法的返回類型爲bool。當認證成功時,則返回true,否則返回false。現在在按鈕單擊事件中按如下方式編寫代碼。我假設userid文本框的名稱爲TxtUserId,密碼文本框爲TxtPwd,並且在按鈕下方有一個名稱爲LblError的標籤,用於在身份驗證失敗時顯示錯誤消息。

bool success=POCOClassName/object.Authenticate(TxtUserId.Text,TxtPwd.Text); 
if(success) 
{ 
    //write the code to display a form that is accessible to authenticated users 
} 
else 
{ 
    LblError.Text="UserId and or Password Is Wrong"; 
} 
+0

謝謝你很多。這是我正在尋找,但沒有得到propoer流..謝謝你很多。是否有可能通過接口或其他方式實現這一點? – Akshay

+1

是的,你可以。但不需要接口。 –

+0

請通過點擊答案左側的向下箭頭鍵下方的勾號來接受此答案。謝謝。 –

相關問題