C# VS - When one radio button is checked, uncheck another - c#

I have a form, on which i placed 2 radio buttons. My issue is I need it to function in a way where if one is clicked, the other will be unclicked. I have the following code however it gets stuck in an inifinite loop once you do the first click and I do understand why. Wanted to see if any of you guys know how to go about making this in c#? I'm fairly new to c#
public partial class Form1 : Form
{
public Form1()
{
radAllCols.CheckedChanged += new EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new EventHandler(this.radSelCols_Checked);
}
private void radAllCols_Checked(object sender, EventArgs e)
{
if (radAllCols.Checked == true)
{
radAllCols.Checked = false;
radSelCols.Checked = true;
}
}
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radSelCols.Checked = false;
radAllCols.Checked = true;
}
}
}

If the radio buttons have different RadioGroup values you have to first, unregister the Checked event, change the Checked property value and re-register the Checked event.
private void radAllCols_Checked(object sender, EventArgs e)
{
if (radAllCols.Checked == true)
{
radAllCols.CheckedChanged -= new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged -= new
EventHandler(this.radSelCols_Checked);
radAllCols.Checked = false;
radSelCols.Checked = true;
radAllCols.CheckedChanged += new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new
EventHandler(this.radSelCols_Checked);
}
}
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radAllCols.CheckedChanged -= new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged -= new
EventHandler(this.radSelCols_Checked);
radSelCols.Checked = false;
radAllCols.Checked = true;
radAllCols.CheckedChanged += new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new
EventHandler(this.radSelCols_Checked);
}
}
The code above is for very custom scenarios and should be avoided as much as possible. The radio boxes should behave the way you want automatically. Make sure you have the same RadioGroup property value on both of them.

RadioButtons placed in the same parent control (like a panel) behave this way by default.
There is no need to use a checked event for this.
Setting values of the radAllCols.Checked = true property fires the radAllCols_Checked event this causes your infinite "loop"
Since you are trying to uncheck the same radioButton to checked
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radSelCols.Checked = false; // reversed
radAllCols.Checked = true; // reversed
}
}

If you use GroupBox Container Element for same Radio Buttons that you want select one of them, you don`t need handle check state of Radio Buttons manually, When you select a Radio Button all the other Radio Buttons in the same group will be unchecked.

Your rest of the code is fine but you need to change your code in Checked methods() as mentioned below to prevent infinite loop and then it will work fine:
private void radAllCols_Checked(object sender, EventArgs e)
{
radAllCols.Checked = !radSelCols.Checked;
}
private void radSelCols_Checked(object sender, EventArgs e)
{
radSelCols.Checked = !radAllCols.Checked;
}

Related

How to enable button after all textboxes are not empty in c# winforms?

How can I make button property set to enabled=true after all my textboxes are not empty?
I'm learning programming and my apps are simple.
I know how to enable this property when one of my textboxes have text but this is not the case.
Use case is that user need to put data in both textboxes and after that will be able to click btn.
How in most simple way can I validate all form and then enable button?
There are just 2 tb:
https://i.imgur.com/JUslNWE.png
You need to create a TextBox_TextChanged event and subscribe to all text boxes.
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
Thanks guys for all feedback.
I have managed to do this this way:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}

How to allocate two different functions to the same button?

I am working on Winforms with C#.
I have a problem with the logic, there are two different methods that I need to call, so that if I click the button, the first action should get applied and if I click the same button again, the second action should get applied.
This is not the exact code but I have an idea something like this:
private void button1_Click(object sender, EventArgs e)
{
if(button1.click==true)
{
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
Here I have two problems regarding whether the button is already clicked:
If it's not clicked the Imgsave() should get activated.
If button is clicked the Imgsave() should get closed.
Can anyone please help me with this? Thanks.
You need to keep state somewhere. You can do this:
private bool buttonClicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!buttonClicked)
{
buttonClicked = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
This assumes you never going to click it a third time. If you are, you would need to handle that in some way.
I'd have either a class level variable track the number of times a button is clicked:
private bool _unclicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!_unclicked)
{
_unclicked = true; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
_unclicked = false; //toggle it off again
ImgSave.exit();
}
}
, or I'd store it in the .Tag of the button:
private void button1_Click(object sender, EventArgs e)
{
if(!button1.Tag.ToString() == "unclicked")
{
button1.Tag = "clicked"; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
button1.Tag = "unclicked"; //toggle it off again
ImgSave.exit();
}
}
You could also remove one event handler and add another:
private void button1_FirstClick(object sender, EventArgs e)
{
button1.Clicked -= button1_FirstClick;
button1.Clicked += button1_SecondClick;
fileNumber = 1;
ImgSave();
}
private void button1_SecondClick(object sender, EventArgs e)
{
button1.Clicked -= button1_SecondClick;
button1.Clicked += button1_FirstClick;
ImgSave.exit();
}
I've always been less of a fan of adding and removing event handlers to achieve things like this but it's quite a clean solution
You should save your state in a variable. Your state will change after first click and you can change the state of Clicking button with calling ConditionChanger() method anytime.
For example you may need change the state of variable when you clicked a second button.
private void ConditionChanger(){
myState = !myState;
}
Your variable :
private bool myState = false;
And your click event :
private void button1_Click(object sender, EventArgs e)
{
if(!myState)
{
myState = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}

(C#)How do I, upon pressing a button, Make a new form window appear? One that i can drag other buttons and text boxes onto

I'm new to C# and I need this function for a program im working on for school. I need to make a new window pop up when i click a button, not a message box though like a forms window, one that i can design with text boxes and buttons. What is on the new pop up window depends on the previous window but i can figure that out.
Also I need a way to close the previous window once the new one appears
Here's my code:`
// This makes sure only one box is checked
private void MulCB_CheckedChanged(object sender, EventArgs e)
{
if( MulCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void DivCB_CheckedChanged(object sender, EventArgs e)
{
if (DivCB.Checked == true)
{
MulCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void AddCB_CheckedChanged(object sender, EventArgs e)
{
if (AddCB.Checked == true)
{
DivCB.Checked = false;
SubCB.Checked = false;
MulCB.Checked = false;
}
}
private void SubCB_CheckedChanged(object sender, EventArgs e)
{
if (SubCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
MulCB.Checked = false;
}
}
private void oneDCB_CheckedChanged(object sender, EventArgs e)
{
if(oneDCB.Checked == true)
{
twoDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void twoDCB_CheckedChanged(object sender, EventArgs e)
{
if ( twoDCB.Checked == true)
{
oneDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void threeDCB_CheckedChanged(object sender, EventArgs e)
{
if (threeDCB.Checked == true)
{
oneDCB.Checked = false;
twoDCB.Checked = false;
}
}
// ends here
// Button operation
private void button8_Click(object sender, EventArgs e)
{
var form = new Form();
}
}
}
`
Thanks a lot!
Sal
The project is im supposed to make a quizzing program for kids. They should be able to choose 1 operation and the amount of digits the numbers will have. It then has to out put 10 random questions according to the selection made by the kid, then once they have completed the quiz, it should display their results and which questions they got wrong.
Assuming that the design of the window doesn't have to be completely dynamic, you can design it in Visual Studio (I'm assuming you did so with the first one). Then you can pass the results to the window. Like:
// Note: Form2 ist the name of your designed From
Form2 myform = new Form2();
this.Hide();
//You could pass the question settings like this
// 1 is for multiplication, 2 for division,3 for addition, 4 for substraction
myform.operation=1;
myform.digits=2
myform.Show();
And in the code of Form2:
namespace Yournamespace {
public partial class Form2: Form {
//Add these two lines about here
public static int operation;
public static int digits;
public Form2() {
InitializeComponent();
}
}
}
Then you can use the variables in Form2 and fill in the textbox or other elements you might design.
Also: You cloud use radio buttons instead of checkboxes as you then won't have you worry about unchecking the other checkboxes.

Changing the properties of a dynamically created control

I have a Form with a LayoutPanel that has dynamically added buttons inside. The buttons are added at runtime, which works, but my problem is I'd like to set the properties to one of the buttons to disabled if a textBox is empty and enable it when it the textBox is not empty.
Here is a code example, with the error I am receiving below:
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button1";
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
CS0103 The name 'button1' does not exist in the current context
Should I be declaring the buttons elsewhere so the entire code can see that they do in fact exist or is my problem elsewhere? Thanks.
You must declare a Button outside Form1_Load if you want to access the button with name directly from other methods, because in your case the buttons are just available in the Form1_Load method :
Button button1;
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
or if you want declare Buttons inside Form1_Load, you can access to Button like this :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var btn = tableLayoutPanel1.Controls.OfType<Button>().Where(x => x.Name == "button1").FirstOrDefault();
(btn as Button).Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
As an alternative, you could dynamically get the control from the Layout Panel and set the enabled property like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Control button = tableLayoutPanel1.Controls["button1"];
button.Enabled = string.IsNullOrEmpty(textBox1.Text) ? false : true;
}
This approach is not as "safe" as declaring the buttons at form level, but I thought it would be useful to mention for times when you need to be genuinely dynamic in referencing controls.
There are a couple ways you can do this, one would be to use the Find method of the TableLayoutPanel's Controls Collection like this.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Button btn =(Button)tableLayoutPanel1.Controls.Find("button1", true)[0];
if (string.IsNullOrEmpty(textBox1.Text))
{
btn.Enabled = false;
}
else
{
btn.Enabled = true;
}
}
The second would be to use the buttons Tag Property to determine which control to use, I have used this in the past for dynamically generated controls.
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
button1.Tag = 1; //note the Tag property being used
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
button2.Tag = 2;
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button3";
button3.Tag = 3;
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in tableLayoutPanel1.Controls) //iterate through controls
{
if ((int)c.Tag == 1) //if Tag is equal then process
{
if (c is Button)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
((Button)c).Enabled = false;
}
else
{
((Button)c).Enabled = true;
}
break; //if you have multiple controls to process remove this
} //and assign the same tag to the controls you want processed
}
}
}

Set off an Event in case any one of a group of radiobutton gets checkchanged

Situation is as follows:
I have a ButtonA, which is currently not enabled.
I have 5 Radiobuttons of which one is always checked.
I want to enable ButtonA when a different Radiobutton gets selected.
I thought about doing something like
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
... and so on.
There is probably a more elegant solution and im missing it.
You can use a single method as an event handler for all radiobuttons:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
this.RadioButton1.CheckedChanged += RadioButtonChanged;
this.RadioButton2.CheckedChanged += RadioButtonChanged;
this.RadioButton3.CheckedChanged += RadioButtonChanged;
}
Or a loop to do the same thing:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += RadioButtonChanged;
}
Or even a lambda event handler set in a loop:
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += (o,e) => ButtonA.Enabled = true;
}
How about just having 1 event handler like this:
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
Then you can use the form designer to assign this method name to the CheckedChanged events of all the radio buttons.
This will save you having to repeat the code for each radio button.
Another trick is to add all your radio buttons to a group box or panel in the designer, then they will all be children of that control.
Then you can add the following code to your Load event handler for the form:
foreach(var radioButton in radioGroupBox.Controls.Cast<Control>()
.Where(i => i is RadioButton)
.Cast<RadioButton>())
{
radioButton.CheckedChanged += RadioButton_CheckedChanged;
}
This way, if you add more RadioButtons to the group, you don't need to change any code.

Categories

Resources