Toggling Radio Buttons In WinForms - c#

This is what I'm doing and it results in a stack overflow because it just switches back and forth forever.
private void radioButtonA_CheckedChanged(object sender, EventArgs e)
{
radioButtonB.Checked = !radioButtonB.Checked;
}
private void radioButtonB_CheckedChanged(object sender, EventArgs e)
{
radioButtonA.Checked = !radioButtonA.Checked;
}
There has to be a better way to do this...

try commenting out all of your code and see if it works the way you want.
you don't have to uncheck the other radio buttons in code

Is it toggling of "One" radio button what you were trying to do? If so, I tried this, and it works for me:
Set the Radio Button AutoCheck property to FALSE.
Create a "Click" event for the Radio Button.
In the Click Event Handler for the Radio Button paste the code:
radioButton.Checked = !radioButton.Checked;
I hope this helps.

You can disable the Event in the code before changing the Checked value, then add it again immediately afterwards.

Related

Disable click button event c#

I program in Visual Studio 2013 C#.
I want to have a functionality to disable for short time a button.
I tried button.Enabled=false, but I see that when I click it during it is disabled then the click action starts right after I get that button enabled in other place of the program.
How to clear that event or block them when button is disabled?
Best regards,
Krzysztof
for disable button click.
button1.Click -= button1_Click; // here button1_Click is your event name
for enable button click.
button1.Click += button1_Click; // here button1_Click is your event name
This is the extremely simple way that I found on the internet and it works.
Disable the button(s)
Run whatever is needed to be performed on the procedure
Run this command Application.DoEvent();. This command enqueues all the click events (as many as the user clicked) and ignore/dispose them
Enable the button(s) at the end
Good Luck
Can't you just check for button's state before executing commands in the method?
For example:
void Click(object sender, EventArgs e)
{
if(!(sender as Button).Enabled)
return;
//other statements
}
Try
YourButton.Attributes.Add("onClick", "");
To remove its onClick altogether.
and then
YourButton.Attributes.Add("onClick", "YourButton_Click");
To add it again.
But your program shouldn't execute the Click when you say it does. It's likely there's something wrong in your code's logic.
Look at this code below, after click button2, button 1 is disabled. while button 1 is disabled, click button 1. After button 1 enable automatically after 5 seconds textBox1 update to "Update".
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Update";
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread.Sleep(5000);
button1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
btn.Visibility = Visibility.Collapsed;
You can try this.
ALL WRONG!
"button1_Click" is not the standard handler behind a button, so it cannot be removed. That is your own function.
YourButton.Attributes does not exist at all.
Nobody even mentioned "Web Forms".
Your description does not match the Windows Forms Button behavior.
The solution for the question you really asked is:
Use a usual Windows Forms Button or override the click event to check again if the button is enabled.

How to properly handle events on labels ontop of a button?

I have a class which I inherit from Button. In this class, I create labels which I can use as custom button captions. The problem I have on these custom buttons is when I click the label nothing happens, and if I click outside the label but still on the button it works as usual. To solve this I wrote:
lblTitle.Click += new EventHandler(LabelClick);
And in LabelClick I wrote:
private void LabelClick(object sender, EventArgs e)
{
base.OnClick(e);
}
This worked good, but then I noticed that I didnt get the clickanimation of the click, when I clicked on the label. So I made the same thing with onMouseDown and Up.
lblTitle.MouseDown += new MouseEventHandler(LabelMouseDown);
lblTitle.MouseUp += new MouseEventHandler(LabelMouseUp);
private void LabelMouseDown(object sender, MouseEventArgs e)
{
base.OnMouseDown(e);
}
private void LabelMouseUp(object sender, MouseEventArgs e)
{
base.OnMouseUp(e);
}
But then I sometimes get the effect of clicking two times on the button. So now I'm wondering, how do I give the user the same click animation when clicked on the button and the label ontop of the button - without making the button click twice?
Thanks in advance and if you gonna downgrade this question, please leave a comment on why!
I think that the problem is that LabelClick & LabelMouseDown & LabelMouseUp all these three events are fired when you click the label which fires them in the base button. so your click event is duplicated when you make a MouseDown event. (this should be a comment, but i dont have enough reputation to leave a comment, hope it will help :) )

Order of radio button events in C#

I have some radio buttons on a form. When a user changes the radio button selection, I update another object with that info. Each of the radio buttons has a different event handler, but all of them are for the CheckedChanged event.
Which event is fired first? The CheckedChanged from the radio button that is losing selection, or the CheckedChanged from the radio button that is being selected? Or is it a race?
It takes one minute to create a new WinForms project, drag two radiobuttons on it and assigning two separate event handlers to their CheckedChanged events. So the answer is, in this case: uncheck fires first.
However, it is not documented, so it could change with any .NET update (unlikely, but still). Don't bind your application logic to it.
If you could explain your actual problem, your question can be answered more specifically. I guess your code looks something like this:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
SomeLabel.Text = "Option 1";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
SomeLabel.Text = "Option 2";
}
Change it to take in account the state of the radiobutton:
if ((sender as RadioButton).Checked)
{
SomeLabel.Text = "Option 1";
}
So your code doesn't depend on the order of the events anymore and doesn't execute when it's actually not required.

How to execute code the second a checkbox is checked? C#

When I usually work with checkboxes, I check to see if the box is checked with the code below:
if (checkBox1.Checked)
{
Label1.BackColor = Color.Red;
}
This code is usually attached to a button that sets into motion when it's clicked by the user. This time, however, I want to do something, like change the color of a label, the very SECOND the checkbox is checked by the user. That is, I don't want to wait until the user pushes some other button to check if the checkbox is checked in order for the label's color to change.
How do I do this?
Sounds like you need a CheckedChanged event handler. That one's for ASP.Net, but there's a version for winforms as well (and xaml, and so on.)
Then you have to write code inside CheckedChanged Event of checkBox as below:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
label1.ForeColor = Color.Red;
}
Why don't you hook up to the CheckChanged event and implement your ideas there?

Changing focus from on textbox to another, when tabpages is changed in C#?

I have several tabs in a form. Each tab has one textbox. When I enter tabpage1 I have managed to set the focus on the textBox1. When I press a button in tabpage1 I jump to a random tab in the controller. What I want now is to have the focus set on textBox in the active tabpage. I have tried using tabpage_Enter event, but it does not seem to work. My code look like this :
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox2.Select();
}
Any suggestions?
I think you need to use SelectedIndexChanged event of TabControl instead of _Enter, using Enter event, focus will change to textBox2 every time the cursor enter the tabPage control.
You can use the Focus() method set the focus on a textbox. I would probably set on the tabPage_Enter event.
private void tabPage_Enter(object sender, EventArgs e){
{
var tab = sender as tabPage;
if(!tab.Focused) tab.focus();
}

Categories