Hi
I am new to C#.
I have to enable my text box whn the check box is checked.
It would be very helpful if i can get the code for this.

Recommended Answers

All 16 Replies

Hi,

This is pretty simple.

1) Use the CheckedChanged event in Checkbox
2) Use the enabled property of Textbox.

Good luck.

Hi
I was trying to set the check state value of the chechbox to checked.
I dont know Why i am getting an error.
Please find the code below.

public void Checked_Changed(Object sender, EventArgs s)
        {
            MessageBox.Show("Checked");
            CheckBox chkbox=System.Windows.Forms.CheckBox(sender);
            chkbox.Checked = true;
            
        }

Hi,

Here is the simple code;

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                textBox1.Enabled = false;
            else
                textBox1.Enabled = true;
        }

Good luck.

if (checkBox1.Checked == true)
            {
                textBox1.Enabled = true;
            }
            else
            {
                textBox1.Enabled = false;
            }

let me know if you need any other help

Thanks

You can use a one-liner ;)

textBox1.Enabled = checkBox1.Checked;

DIPY -- Are you getting a callstack overflow error? I'm looking at the code you pasted and in the event you are changing the check status of the checkbox that fired the event.

public void Checked_Changed(Object sender, EventArgs s)
        {
            MessageBox.Show("Checked");
            CheckBox chkbox=System.Windows.Forms.CheckBox(sender);
            chkbox.Checked = true;
        }

When you set the checkbox.checked property like that it will fire the event off again.

This is ok, but I have something else in mind, when I check checkbox I need my textbox to be false right away... This code doesnt exetuce that.

textBox1.Enabled = !checkBox1.Checked;
(using exclamation point)
Try it like that for the effect that you want.

This is ok, but I have something else in mind, when I check checkbox I need my textbox to be false right away... This code doesnt exetuce that.

That's completely opposite from what you said at first. You've gotten completely opposite answers to match your 2 requests. "right away"? The response should take less than 1 millisecond, how fast do you want it? Ten seconds before you decide to click the check box? Your problem has nothing to do with how fast. Your problem is to set coding to do what you want. By the way, is the ReadOnly property set correctly?

It's two different people, that's why. Bokac piggybacked on the original thread (which he probably shouldn't have).

private void check15min_CheckedChanged(object sender, EventArgs e)
        {
            //sat2.Enabled = check15min.Checked;
            if (check15min.Checked)
            {
                sat2.Enabled = false; sat2.Text = "00";
                min2.Enabled = false; min2.Text = "15";
                sec2.Enabled = false; sec2.Text = "00";
            }
            else
            {
                sat2.Enabled = true;
                min2.Enabled = true;
                sec2.Enabled = true;
            }
        }

Here is part of my code, but what happens is that when I click on this checkbox nothing happens, my textboxes stay unchanged, like I didnt do anything. I have no idea what I am doing wrong. ReadOnly property, I am not sure I follow you.

I was able to get it to work with the code you provided (see screenshot). I'm not sure what Read-Only would have to do with it in this case (that would allow your textbox to work as a display and not as a data entry point). Is there anything else in your code that might be blocking that code from running?

It's two different people, that's why. Bokac piggybacked on the original thread (which he probably shouldn't have).

Thanks, you are right

Here is part of my code, but what happens is that when I click on this checkbox nothing happens, my textboxes stay unchanged, like I didnt do anything. I have no idea what I am doing wrong. ReadOnly property, I am not sure I follow you.

Have you created the event handler on your checkbox? I posted a tutorial a while back that might help.

i have 5 checkboxes in form1..based on the checkboxes that are checked in the form1, i want to display those checkbox values in textbox in form 2..can u tell me the code to do this..m using windows forms in visual studio 2010..c#

    if (checkBox1.Checked)
    {
    textBox1.Enabled = true;
    }
    else
    {
    textBox1.Enabled = false;
    }

you can writeyour code into checkedChange Event of all check box and assign values you want to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.