0

I have a radio buttons and one text box on a panel made dynamically . Now, I want to disable the text box when the second radio button is checked, means they are both connected. how can I make the event for it. I want to have the event working. Thanks a lot in advanced. this is my code which is not working:

Panel pnl = new Panel();
pnl.Name = "pnl_";
pnl.Size = new Size(630, 80);

RadioButton rd = new RadioButton();
rd.Name = "rd_" + dr[i]["Value_Name"].ToString();
rd.Text = dr[i]["Value_Name"].ToString();
rd.Location = new Point(i,i*2);
pnl.Controls.Add(rd);

TextBox txt = new TextBox();
txt.Name = "txt_" + Field_Name+"_"+dr[i]["Value_Name"].ToString();
txt.Size = new Size(171, 20);
txt.Text = Field_Name + "_" + dr[i]["Value_Name"].ToString(); 
txt.Location = new Point(20, 30);
pnl.Controls.Add(txt);

//////  ???? ////////
rd.CheckedChanged += new EventHandler(eventTxt(txt));

void eventTxt(object sender,EventArgs e,TextBox txt)
        { 
           RadioButton rd = (RadioButton)sender;
           txt.Enabled = rd.Checked;

        }

4 Answers 4

1

Use a lambda to close over the relevant variable(s):

rd.CheckedChanged += (s, args) => txt.Enabled = rd.Checked;

If you had more than a one line implementation, you could call out to a method accepting whatever parameters you've closed over, instead of including it all inline.

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest to set the Tag of the radio button and get walk down the dependencies.

rd.Tag = txt;

In the event handler use this:

TextBox txt = (sender as Control).Tag as TextBox;

txt.Enabled = ...

Comments

0

you can use code given

rd.CheckedChanged += (s,argx) => txt.Enabled = rd.Checked;

Comments

0

Here's how you could create an event for it:

//if you are using Microsoft Visual Studio, the following
//line of code will go in a separate file called 'Form1.Design.cs'
//instead of just 'Form1.cs'
myTextBox.CheckChangedEventHandeler += new EventHandeler(checkBox1_CheckChanged);  //set an event handeler  

public void checkBox1_CheckChanged(object sender, EventArgs e)     //what you want to happen every time the check is changed
{
    if(checkBox1.checked == true)    //replace 'checkBox1' with your check-box's name
    {           

        myTextBox.enabled = false;    //replace 'myTextbox' with your text box's name;
                                      //change your text box's enabled property to false
    }
}

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.