首页 > 解决方案 > 在用户登录后将按钮更改为可见

问题描述

我创建了一个带有连接的 SQL 数据库和一个带有用户名和密码的表。我设法按照指南创建了一个登录名。代码可以正常登录,但我想做的是在用户登录后在主窗体上显示所有其他按钮。
我在主窗体中使用面板来显示第二个窗体。单击按钮在中间面板中显示一个用户控件。
我只想在不打开新的主窗体的情况下将Button.Visible值更新为falseto 。true

我可以在表单加载时使按钮可见,但在我尝试设置条件时不可见。我不知道如何设置一个条件,即当用户成功登录时,其他按钮设置为可见并且登录按钮被隐藏。

MainLoginUserControl中的部分代码。我添加的是在登录成功时将 TextBox 中的方法传递
给Form中的方法。MainMenu.UserAutheriseduserIDUserAutherised()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.Readif 语句中添加了以下内容:

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,它解决了这个问题。

标签: c#winformsuser-controlsaccess-modifiers

解决方案


为什么要使用静态方法访问 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();

推荐阅读