c# - 在用户登录后将按钮更改为可见
问题描述
我创建了一个带有连接的 SQL 数据库和一个带有用户名和密码的表。我设法按照指南创建了一个登录名。代码可以正常登录,但我想做的是在用户登录后在主窗体上显示所有其他按钮。
我在主窗体中使用面板来显示第二个窗体。单击按钮在中间面板中显示一个用户控件。
我只想在不打开新的主窗体的情况下将Button.Visible
值更新为false
to 。true
我可以在表单加载时使按钮可见,但在我尝试设置条件时不可见。我不知道如何设置一个条件,即当用户成功登录时,其他按钮设置为可见并且登录按钮被隐藏。
MainLogin
UserControl中的部分代码。我添加的是在登录成功时将 TextBox 中的方法传递
给Form中的方法。MainMenu.UserAutherised
userID
UserAutherised()
MainMenu
if (mread.Read() == true)
{
MessageBox.Show("Login successfull");
this.Hide();
MainMenu.UserAutherised(text_userID.Text);
}
中的部分代码MainMenu
:我这样做是为了解决问题。
我认为true
当用户成功登录时设置一个布尔值,然后我可以调用该ShowButtons()
方法将按钮设置为可见:
public static bool UserAutherised(string user)
{
bool returnValue;
string _user ="true";
string _noUser ="false";
bool _userID = bool.Parse(_user);
bool _noUserID = bool.Parse(_noUser);
}
if (user == "")
{
returnValue = _noUserID;
return returnValue;
}
else
{
returnValue = _userID;
return returnValue;
}
我从调试中可以理解的布尔值,但是当我尝试使用方法if
中的语句时ShowButtons()
,我无法弄清楚如何从 to 获取 bool 值UserAutherised()
以if(UserAutherised())
在值为true
.
我希望我对问题的描述足够好。
编辑1:
我尝试使用相同的event Action
语法来获取登录用户的位置和访问权限。在MainLogin
表单中我添加了以下内容:
public event Action<string> ACCESS;
public event Action<string> POSITION;
string userAccess, userPosition;
然后我在mread.Read
if 语句中添加了以下内容:
Sqlcommand cmd_get_position_Access = new SqlCommand (
"SELECT, Access FROM Users WHERE position = @position AND Access = @Access", dataconnection);
SqlParameter _position = new SqlParameter("@position",
SqlDbType.NVarchar);
SqlParameter _access = new SqlParameter("@Access", SqlDbType.NChar);
cmd.Parameters.Add(_position);
cmd.Parameters.Add(_access);
userPosition = mread["position"].ToString();
userAccess = mread["Access"].ToString();
我NullReferenceException
在尝试调用新事件时得到一个,所以我添加了以下 if-else 语句来修复它:
if (ACCESS == null || POSITION == null)
{
return;
}
else
{
ACCESS.Invoke(userAccess);
POSITION.Invoke(userPosition);
}
在MainMenu
按钮单击事件中:
var userLogin = new MainLogin();
panel_Main.controls.Add(userLogin);
userLogin.userLogged += UserLogged;
userLogin.ACCESS += UserAccess;
userlogin.POSITION += Position;
userLogin.Show();
调试时,我可以看到我从数据库表中获取了 Access 值。但是,当我尝试使用带有 if 语句的方法时,即使条件为真,也会跳过条件。我也尝试了 switch 语句,但同样的事情也发生在案例中。他们被跳过。例如,如果我获得了管理员登录名access yellow
,但会跳过大小写。if 语句条件也会发生同样的事情。Access is yellow
并且条件为真,但 if 语句被跳过。当条件为真时,为什么会跳过它们?
private void UserAccess(string access)
{
switch (access)
{
case: "yellow":
userAdmin();
break;
case: "green":
userGreen();
break;
default:
break;
}
}
编辑2:
我发现了问题。原来是在 User 表中。访问权限设置为nchar
数据类型,该数据类型会在值中添加空格。Access
正在获取string
"yellow "
. 我将数据类型更改为varchar
,它解决了这个问题。
解决方案
为什么要使用静态方法访问 MainMenu?
我认为最好以 MainLogin 形式声明事件,例如:
public event Action<string> userLogged;
并在 this.hide 之后调用它:
userLogged.Invoke(text_userID.Text);
并在 MainMenu 表单中为此事件创建一个处理程序:
void UserLogged(string user)
{
if (user != "")ShowButtons();
}
并在显示 MainLogin 表单之前订阅 userLigged 事件,例如:
MainLogin loginForm = new MainLogin();
loginForm.userLigged += UserLogged;
loginForm.Show();
推荐阅读
- botframework - 自适应卡:付款请求
- python - 如何将 Pi Camera 给出的图像输入直接存储到变量而不是文件中?
- spring - Spring Boot 中的 CORS
- python - 第二次加载 Tkinter 窗口时出现问题
- android - android如何更新活动?
- micronaut - 如何初始化 ApplicationContext 和/或 Environment
- javascript - 如何在 Typescript 中承诺读取 2 个文件并连接输出,然后打印总结果?
- c# - C# 将数据导入/导出到 Mailclient
- python - Mojave 升级后,遇到 Readline 库错误。Python 3.5 需要 Readline 7,但 Brew 只安装 Readline 8
- sockets - 使用 Socket.io 构建 Flutter 应用程序:无法收听或向套接字通道发送消息