Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1936

Making a radio button selected by default and making a textbox unable to take input in C#

I am coding in C# using Visual Studio 2010 and have a form whereby I have two radio buttons and a textbox, that is:

o Radio button 1 "Yes"

o Radio button 2 "No"   Textbox here

I want the form to load with the "Yes" radio button already selected, that is:

x Radio button 1 "Yes"

o Radio button 2 "No"   Textbox here (grayed out "cannot input text")

By default, the textbox should not accept input. I want it such that if a user clicks and selects radio button 2 "No", the text box is now able to accept input.

How do I achieve this?

private void rb_taxable_yes_CheckedChanged(object sender, EventArgs e)
{

}

private void rb_taxable_no_CheckedChanged(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

Upvotes: 3

Views: 18910

Answers (3)

user3527736
user3527736

Reputation: 11

just choose the properties of your radio button 1

and then select the "checked" item as true it will set as defult. no need to code

Upvotes: 1

soandos
soandos

Reputation: 5146

Start out with textbox1.enabled = false, and use the onchecked event for the second radio button to change that. also set the first radio button to have radiobutton1.checked = true.

Upvotes: 1

Alex Aza
Alex Aza

Reputation: 78457

On your Form in design mode set Enabled to False for the TextBox, and Checked to True for the first RadioButton.

Attach both radiobuttons to one event handler:

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    textBox.Enabled = radioButton2.Checked;
}

Upvotes: 2

Related Questions