在登录表单上登录后-如何在另一个表单上显示该登录用户 [英] After logging in on login form -how to show that logged user on another form
问题描述
我有基于accdb数据库的Windows应用程序,其中有带有用户名和密码的表
使用正确的用户名和密码登录登录表单后,将打开另一个表单,我想在文本框中查看该登录用户的名称,还是必须是我不知道的文本框.
I have windows application based on accdb database in which I have table with usernames and passwords
After logging in on login form with proper username and password another form opens and I want to see in textbox the name of that logged user, or does it have to be textbox I dont'' know.
How to do this?
推荐答案
将用户名公开为登录"表单上的公共get-only属性.登录表单返回后,读出此字符串,然后将其传递给您的主表单(通过构造函数或通过类似的公共settable属性).
另外,如果您的应用程序具有用于保存全局状态数据的公共静态类,请将其存储在该属性中,以便您可以在整个应用程序中对其进行访问.
Expose the username as a public get-only property on the Login form. Once the login form returns, read out this string and then pass it to your main form (either via the constructor or via a similar public settable property).
Alternatively if your application has a public static class that you use to hold global state data, store it in a property there so you can access it throughout the app.
我会使用单例 [
I would use a Singleton [^]object. To do that, create a class like this:
public class User
{
private static User instance;
private User() {}
public static User Instance
{
get
{
if (instance == null)
{
instance = new User();
}
return instance;
}
}
}
您甚至可以扩展此类,以对用户进行验证/认证/授权....
以简单的形式,我将分配此登录用户的值,以后再取回它.
You can even extend this class to do validation/authentication/authorization... of the user.
In the simple form, I would assign the value of this logged in user and later on retrieve it.
在应用程序中创建一个公共静态类,用于管理状态...
静态类-
Create a public static class in your application use that to manage state...
Static Class--
public static class GlobalState
{
public static string LogedInUse
}
现在将您的登录按钮更改为
Now change your login button click as
void btnLogin_Click(object sender,EventArg e)
{
if(ValidateLogin())
{
GlobalState.LoggedInUser=txtUserName.Text;
}
else
{
// Show some message
}
}
希望对您有用.
--Pankaj
Hope it will usefull.
--Pankaj
这篇关于在登录表单上登录后-如何在另一个表单上显示该登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!