Izzy G
Izzy G

Reputation: 77

Using threestate checkbox to determine 3 states of a field

Currently we call a web service to check if user has permission to use the chat room and we receive a 0 or a 1. I want to add to also be able to receive a 2, which will indicate it is enabled and authorized now so I want to use three state check boxes, fully filled will mean he is authorized and also currently enabled (logged in), check will mean he is authorized but not enabled now, unchecked will mean he doesn't have permission for this chat room.

Here is some code I currently use. FYI I took over this project from our developer and I'm kind of new to this, if you can give me an example even just for authtac1 then I'll figure out how to do all others, and also do I need to add a new field ActiveTac1 like I have AuthTac1? Thanks for your help.

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();
    }

last question, to make sure I didnt screw up anything else @competemt_tech this is what I ended up using (thanks so so much for your help, and our developer who was able to assist me)

To know how to set the checkboxes when i get the data from the webservice I used

 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);

and then I used:

 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;
        }
    }

and after I make changes, to send back to the webservice I used:

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),

and then I used:

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;
        }
    }

Upvotes: 1

Views: 14118

Answers (1)

competent_tech
competent_tech

Reputation: 44941

Based on your code, it appears that you are using WinForms, not WPF, correct?

If this is the case, then you will want to use the CheckState property of the checkbox and not Checked, since it supports three values:

Unchecked = 0
Checked = 1
Indeterminate = 2

Update

In order to address your requirements, you would have code similar to the following:

First, create a function that will generate the correct checkstate base on the authcode:

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());
    }
}

Then, use this code to set the state of your checkboxes:

AuthChat.CheckState = GetCheckStateFromAuthCode(imu.AuthChat);
AuthTac1.CheckState = GetCheckStateFromAuthCode(imu.AuthTac1);
etc.

Update 2

In order to retrieve the authcode from the checkstate:

First, create a function that will generate the correct checkstate base on the authcode:

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());
    }
}

Then, use this code to set the auth code from your checkboxes:

imu.AuthChat = GetAuthCodeFromCheckState(AuthChat.CheckState);
imu.AuthTac1 = GetAuthCodeFromCheckState(AuthTac1.CheckState);
etc.

Upvotes: 4

Related Questions