使用threestate复选框确定字段的3种状态
本文关键字:3种 状态 字段 threestate 复选框 使用 | 更新日期: 2023-09-27 18:15:51
目前我们调用一个web服务来检查用户是否有使用聊天室的权限,我们收到一个0或1。我想添加也能收到一个2,这将表明它是启用和授权现在所以我想使用三个状态复选框,完全填写将意味着他已被授权并且当前已启用(登录),选中将意味着他已被授权但现在未启用,未选中将意味着他没有此聊天室的权限。
下面是我目前使用的一些代码。仅供参考,我从我们的开发人员那里接管了这个项目,我对此有点陌生,如果你能给我一个例子,即使只是authtac1
,那么我就会弄清楚如何做所有其他的,我也需要添加一个新的领域ActiveTac1
,就像我有AuthTac1
吗?谢谢你的帮助。
public partial class IMeditor : Form
{
private IMuser imu;
private IMui IMui;
private IMdata IMdata;
public IMeditor(IMui IMui, IMuser U, string Member)
{
InitializeComponent();
this.IMui= IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.MemberNick= Member;
}
else
Add.Text = "Update";
IMdata = new IMdata();
MemberHandle.Text = imu.MemberHandle;
IM.Text = imu.IM;
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
private void Add_Click(object sender, EventArgs e)
{
IMdata.AddIM(IMui.Username, IMui.Password, imu.UID, MemberHandle.Text, Transport.Text, IM.Text,
AuthChat.Checked ? 1 : 0, 0,
AuthTac1.Checked ? 1 : 0, AuthTac2.Checked ? 1 : 0, AuthTac3.Checked ? 1 : 0,
AuthTac4.Checked ? 1 : 0);
Close();
}
最后一个问题,以确保我没有搞砸任何其他@competemt_tech这是我最终使用的(非常感谢您的帮助,我们的开发人员能够帮助我)
知道如何设置复选框时,我从webservice我使用
AuthChat.CheckState = GetCheckStateFromAuthCode(imu.AuthChat, imu.Enabled);
AuthTac1.CheckState = GetCheckStateFromAuthCode(imu.AuthTac1, imu.Tac1Enabled);
AuthTac2.CheckState = GetCheckStateFromAuthCode(imu.AuthTac2, imu.Tac2Enabled);
AuthTac3.CheckState = GetCheckStateFromAuthCode(imu.AuthTac3, imu.Tac3Enabled);
,然后我用:
private CheckState GetCheckStateFromAuthCode(int AuthCode, int Enabled)
{
switch (AuthCode + Enabled)
{
case 0:
return CheckState.Unchecked; // Unauthorized
case 1:
return CheckState.Checked; // Authorized, not enabled
case 2:
return CheckState.Indeterminate; // Authorized and enabled
default:
return CheckState.Unchecked;
}
}
,在我做了更改之后,发送回我使用的webservice:
imu.AuthChat, imu.Enabled = GetAuthCodeFromCheckState(AuthChat.CheckState),
imu.AuthTac1, imu.Tac1Enabled = GetAuthCodeFromCheckState(AuthTac1.CheckState),
imu.AuthTac2, imu.Tac2Enabled = GetAuthCodeFromCheckState(AuthTac2.CheckState),
imu.AuthTac3, imu.Tac3Enabled = GetAuthCodeFromCheckState(AuthTac3.CheckState),
,然后我用:
private int GetAuthCodeFromCheckState(CheckState checkState)
{
switch (checkState)
{
case CheckState.Unchecked: // Unauthorized
return 0;
case CheckState.Indeterminate: // Authorized, not enabled
return 1;
case CheckState.Checked: // Authorized and enabled
return 2;
default:
return 0;
}
}
根据您的代码,似乎您正在使用WinForms,而不是WPF,正确吗?
如果是这种情况,那么您将希望使用复选框的CheckState属性而不是Checked,因为它支持三个值:
Unchecked = 0
Checked = 1
Indeterminate = 2
为了满足您的需求,您应该编写如下代码:
首先,创建一个函数,该函数将根据authcode生成正确的checkstate:
private CheckState GetCheckStateFromAuthCode(int AuthCode)
{
switch (AuthCode)
{
case 0:
// Unauthorized
return CheckState.Unchecked;
case 1:
// Authorized, not enabled
return CheckState.Indeterminate;
case 2:
// Authorized and enabled
return CheckState.Checked;
default:
throw new ArgumentException("Unrecognized AuthCode value " + AuthCode.ToString());
}
}
然后,使用以下代码设置复选框的状态:
AuthChat.CheckState = GetCheckStateFromAuthCode(imu.AuthChat);
AuthTac1.CheckState = GetCheckStateFromAuthCode(imu.AuthTac1);
etc.
更新2
为了从checkstate中检索authcode:
首先,创建一个函数,该函数将根据authcode生成正确的checkstate:
private int GetAuthCodeFromCheckState(CheckState checkState)
{
switch (checkState)
{
case CheckState.Unchecked:
// Unauthorized
return 0;
case CheckState.Indeterminate:
// Authorized, not enabled
return 1;
case CheckState.Checked:
// Authorized and enabled
return 2;
default:
throw new ArgumentException("Unrecognized checkState value " + checkState.ToString());
}
}
然后,使用以下代码设置复选框中的验证码:
imu.AuthChat = GetAuthCodeFromCheckState(AuthChat.CheckState);
imu.AuthTac1 = GetAuthCodeFromCheckState(AuthTac1.CheckState);
etc.