How to make a textbox invisible when radio button checked - c#

I am working on C# application. I have 10 radio buttons in a group panel, so now if I only checked the radioButton10 then textBox1 will be visible, if I checked someone of the other radio Buttons (radioButton1 .... radioButton9) then textBox1 should be invisible.
I wrote the following code but the textBox1 is still visible. If the code is right where can I wrote it(form load, some function ... etc) if it's NOT, then Please Help.
public TeamInfoForm()
{
InitializeComponent();
showTeam();
if (radioButton10 .Checked)
textBox1 .Visible = true;
else
textBox1 .Visible = false;
}

I think you forgot to implement the event that occur when you check or uncheck the radiobutton. Try to imlpement "OnCheckChanged" event for the radiobutton and you have to set autopostback to true if you want that the event occurs, otherwise the event won't work.

Initially you have to set the Visible property of textBox1 to false in Forms Designer. Otherwise you can set it in FormInitialize() method. Next you have write code like below
public void ToggleTextBox()
{
textBox1.Visible = radioButton3.Checked;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}

Related

C# WinForms: bring back focus to form after button click

my simple WinForms app consists of one Form which has 2 important methods:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = true;
}
keyTimer.Start();
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = false;
}
keyTimer.Stop();
}
In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:
button1.TabStop = false;
this.Focus();
in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance
Run this in the button click function:
this.ActiveControl = null;
When you click the button, you set it as the active control. Removing this property will let you put focus on your form.

Radiobutton must be checked to show checkboxlist

My question is, what code do I have to write so that my radiobutton1 will show me the checkboxlist1 which is not visible -> Visible set on False.
I want to show the checkboxlist only if radiobutton1 is checked.
I am using visual c#2012. Hope you can help.
You use Checked property of the RadioButton to identify wether RadioButton is checked or not.
From MSDN : RadioButton.Checked
Gets or sets a value indicating whether the control is checked.
Try This:
if(radioButton1.Checked)
{
//Enable checkboxlist
CheckBoxList1.Visible=true;
}
EDIT :
You should handle it in CheckedChanged EVent handler of the RadioButton
Try This:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton1.Checked)
{
//Enable checkboxlist
CheckBoxList1.Visible=true;
}
}
EDIT:
if you want to disable another radiobutton try this:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton1.Checked)
{
//Enable checkboxlist
CheckBoxList1.Visible=true;
//Disable RadioButton
RadioButton2.Visible=false;
}
}
you can do
CheckBoxList1.Visible = radioButton1.Checked

using checkbox to enable textbox

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!
You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}
Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.
You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}
I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}

How to make textbox appear only after user clicks button in c#

How would I make a textbox appear only after clicking a button. THis means that It should be hidden, and once user clicks, then it can appear.
private void button7_Click(object sender, EventArgs e)
{
// .. what next?
}
You can use Control.Visible to make any control visible or hidden:
private void button7_Click(object sender, EventArgs e)
{
theTextBox.Visible = true;
}
Just set it's Visible property to false initially (ie: in the designer).
Assuming you have defined a TextBox textBox1 somewhere:
private void button7_Click(object sender, EventArgs e)
{
textBox1.Visible = !textBox1.Visible;
}
This way you can toggle the visibility.
You can set it just to true if you like, but make sure the initial Visible state, you can set it in the designer, is false.

How to prevent radio button change status

I am trying to prevent the change status of textbox and radio button when the button (btactive) is clicked. I can use property IsReadOnly for textbox but I can not find this property in radio button, Is there anyway to prevent the change status of radiobutton ?
Here is my code :
private void btActive_Click(object sender, RoutedEventArgs e)
{
if (check != 0)
{
udhour.IsReadOnly = true;
udmin.IsReadOnly = true;
testvalue.IsReadOnly = true;
//How can I set Isreadonly for radio button
}
}
Disable radio button. use IsEnabled=false
Change the AutoCheck property of the RadioButton to False
Add radiobutton.IsEnabled = false in the code.
Write and Checked changed event for the readonly effect.
void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.Checked = !radioButton1.Checked;
}

Categories

Resources