Avatar of VooDooBamboo
VooDooBamboo

asked on 

How to read a checkbox state in C#

I have some code that was once assigned to a toolstripmenu item.  However I wanted this to work from a regular button instead on my form.  All I did was copy the code from the toolstripmeny item and place it in the button code but I am getting an error.

I get this error....
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

For this part....
enableAutoSit.Checked = false;

I am really new to C#.  How to you tell via logic if a checkbox is checked or not?  I think thats what I am doing wrong here.  I guess....  I really need some help here.  The way it should work is if somebody does try to check the enableAutoSit check box it should then check to make sure that SeatID is not null.  If it is it should not check and report as so as you can see below.  If there is a value, it should then check the box and move on.  I think you can see all this from the code below.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (enableAutoSit.Checked)
            {
                enableAutoSit.Checked = false;
                LogErrors("Auto-sit disabled.", Color.Blue);
            }
            else
            {
                enableAutoSit.Checked = SeatID != null;
                if (enableAutoSit.Checked)
                {
                    LogErrors("Auto-sit enabled (" + SeatID.ToString() + ")", Color.Blue);
                    Client.Self.RequestSit(SeatID, LLVector3.Zero);
                    Client.Self.Sit();
 
                }
                else
                {
                    LogErrors("Auto-sit object ID invalid or not defined.", Color.Blue);
                }
            }
        }

Open in new window

Editors IDEsC#

Avatar of undefined
Last Comment
Imperdonato

8/22/2022 - Mon