我是c#的新手。我有個疑問。讓我們來看一個實時的例子如何在Winform標籤/文本框中顯示POCO(類)文件的結果
- 假設我有一個登錄winform提交的用戶名和密碼和一個按鈕。
- 我有一個poco C#文件,在點擊按鈕後可以獲取用戶名和密碼的值。這個C#poco文件與表單文件無關。它是單獨的poco文件,它將處理登錄身份驗證
- 現在poco文件將生成結果說「登錄成功」或「登錄失敗」
- 現在,如何在winform上顯示poco文件的這個結果?
請給出正確的代碼,這將清除我的概念。
謝謝
我是c#的新手。我有個疑問。讓我們來看一個實時的例子如何在Winform標籤/文本框中顯示POCO(類)文件的結果
請給出正確的代碼,這將清除我的概念。
謝謝
在按鈕的點擊處理程序,您的驗證類(如你把它叫做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
}
}
假設你有一個名字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";
}
謝謝你很多。這是我正在尋找,但沒有得到propoer流..謝謝你很多。是否有可能通過接口或其他方式實現這一點? – Akshay
是的,你可以。但不需要接口。 –
請通過點擊答案左側的向下箭頭鍵下方的勾號來接受此答案。謝謝。 –
你有沒有試過任何代碼......? –
是的,poco意味着一個類文件。但我不知道該怎麼做。導致每當我點擊按鈕數據去類文件,但不知道如何發送該文件的結果形成 – Akshay
@Akshay你需要做更多的研究,並提供一些你面臨問題的示例代碼。請通過[this](http://stackoverflow.com/help/how-to-ask) –