C# Winform Radio button behavior - c#

I have 2 radio buttons calling 2 methods on _CheckedChanged event
private void manual_radioBtn_CheckedChanged(object sender, EventArgs e)
{
SendDataManual();
}
private void auto_radioBtn_CheckedChanged(object sender, EventArgs e)
{
SendDataAuto();
}
Now when I check the radiobutton, doesn't matter which one, both functions are being triggered.
When I check manual_radioBtn, both SendDataManual() & SendDataAuto() are being called and if I check
auto_radioBtn also, both SendDataManual() & SendDataAuto() are being called.
I know this behaviour won't occur if am using a Click event rather than CheckedChanged. But isn't CheckedChanged the default even for radiobutton.
Is this a normal behavior with radiobuttons or specific behavior when using 2 radiobuttons
What if there are 3 buttons .Will all the methods under the 3 button event trigger at the same time?

The event name gives away it's purpose - CheckedChanged which indicates that the Checked state of the RadioButton has changed. It's not stating that Checked = true (or otherwise), just that the value of it changed.
From the docs, CheckedChanged:
Occurs when the value of the Checked property changes.
One way to handle this is to check the value of the Checked property in your code:
private void manual_radioBtn_CheckedChanged(object sender, EventArgs e)
{
if (manual_radioBtn.Checked)
SendDataManual();
}
private void auto_radioBtn_CheckedChanged(object sender, EventArgs e)
{
if (auto_radioBtn.Checked)
SendDataAuto();
}
My preference would be to handle this in a single function though, something like:
private void manual_radioBtn_CheckedChanged(object sender, EventArgs e)
{
SendData();
}
private void auto_radioBtn_CheckedChanged(object sender, EventArgs e)
{
SendData();
}
private void SendData()
{
if (manual_radioBtn.Checked)
SendDataManual();
else if (auto_radioBtn.Checked)
SendDataAuto();
}
An advantate of using a single function to handle the response to the event is that you only need one event handler for all of the appropriate CheckedChanged events, rather than one for each RadioButton. That might not seem important when you have two but imagine you have 20 of them.

Related

Bring custom control to front

I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?

Is there a better way to close buttons on click?

I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.

one Click event for multiple buttons with Text property

I want to make a click event for a bunch of buttons. The problem is that I want to use the button's Text, and pass it to a function. Now the click event is passed a object sender. When I tried changing that to Button sender, it gave errors. But I don't know how else I can work with the senders Text.
Here is the normal code, which gave a single error:
private void guess_Click(object sender, EventArgs e)
{
guess(sender.Text);
}
I changed it to this, which gave errors:
private void guess_Click(Button sender, EventArgs e)
{
guess(sender.Text);
}
Question:
How can I work with the Button's Text property within this click event, which is a single click_event for multiple buttons?
Step 1: You need to subscribe to the Button Click event of all your buttons to the same EventHandler. so that button click on all your Buttons will fire the same `Event Handler.
Step 2: You need to cast the object sender into Button and then access its Text property to get the Button Text.
Try This:
button1.Click += new System.EventHandler(MyButtonClick);
button2.Click += new System.EventHandler(MyButtonClick);
button3.Click += new System.EventHandler(MyButtonClick);
private void MyButtonClick(object sender, EventArgs e)
{
Button btnClick = (Button)sender ;
guess(btnClick.Text);
}
Cast sender to type button.
Example:
private void guess_Click(object sender, EventArgs e)
{
guess(((Button)sender).Text);
}
You need to cast the sender object to the Button type and use that:
private void guess_Click(object sender, EventArgs e)
{
Button senderBtn = senderBtn as Button;
if(senderBtn != null)
{
guess(senderBtn.Text);
}
}

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

2 events calling each other

I was wondering about this problem for a while, but couldn't really come up with a solution. I have 2 different event handlers calling each other recursively. As soon as event A is fired, it triggers event B which triggers event A again and so on...
Basically I want to be able to select text in a RichTextBox and show the corresponding font size in a combo box. When I choose a different font size from the ComboBox, I want it's value to be applied to the selected text.
The 2 events are:
1) The selection changed event of text inside a RichTextBox:
private void MyRTB_SelectionChanged(object sender, RoutedEventArgs e)
{
//Get the font size of selected text and select the concurrent size from the ComboBox.
}
2) The selected index changed event of a Combobox:
private void CmbFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Apply the chosen font size to the currently selected text of the RichTextBox.
}
What would be the best solution to make sure they each only "do their thing" and do not fire the other event in doing so?
Sometimes changing a property of a control in code fires an event unintentionally. Changing the data source of a ListBox or a ComboBox will fire the SelectedIndexChanged event, for example. Use a flag to handle this case
private bool _loading;
...
_loading = true;
// Fill the ComboBox or ListView here
_loading = false;
In the event handler do this
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (_loading) return;
...
}
Refactor your code so that A calls DoSomethingA() and B calls DoSomethingB(). This way, if you want A to do the functionality of B you can just call DoSomethingB() and not have any recursive calls.
Just use a bool (maybe called dontFireA) and set it in A just before calling B
notifying properties (used in order to enable binding from WPF to non-WPF properies) use this technique:
public object MyProperty
{
get
{
return myField;
}
set
{
if (value != myField)
{
myField = value;
NotifyProperyChanged("MyProperty"); // raise event
}
}
}
The if (value != myField) condition prevents infinite recursion (stackoverflowexception).
In some cases (e.g. floating point numbers and inaccurate value transfers) if (Math.Abs(value - myField) > someConstant) is used instead to break the recursion.
Could you apply a similar technique to your problem?
If both events are on the same object or the owners have references to each other, you could also store a flag on each e.g.
private void OnEvent()
{
DoSomething();
}
private void DoSomething()
{
this.IsBusy = true;
// do work
// raise event
if (!other.IsBusy)
RaiseEvent();
}
I am going to make the educated guess that you are not raising Event A or Event B yourself; let's say Event A is the TextBox1.TextChanged event and Event B is the TextBox2.TextChanged event, and they have handlers like:
public void Textbox1_TextChanged(object sender, EventArgs e)
{
...
TextBox2.Text = someString;
}
public void Textbox2_TextChanged(object sender, EventArgs e)
{
...
TextBox1.Text = someOtherString;
}
In this case, the handlers are each going to raise the other textbox's TextChanged event by virtue of changing the text, leading to infinite recursion.
The first thing you can do, if you want both to run once and once only, is to mark that they're already running (changing the text of the other textbox results in that textbox's event handler running within the same call stack:
public void Textbox1_TextChanged(object sender, EventArgs e)
{
if(handler1Running) return; //the second time through we exit immediately
handler1Running = true;
...
TextBox2.Text = "Something"; //the other event handler is invoked immediately
handler1Running = false;
}
public void Textbox2_TextChanged(object sender, EventArgs e)
{
if(handler2Running) return; //the second time through we exit immediately
handler2Running = true;
...
TextBox1.Text = "Something Else"; //the other event handler is invoked immediately
handler2Running = false;
}
Now, the deepest it will go is three levels; 1's handler invokes 2's handler which invokes 1's handler again, which sees that 1's handler is already running and quits before doing anything that would deepen the recursion. Same thing if you start by changing TextBox2.
The other thing you can do is make sure you aren't trying to set the textbox to the same value that's already there. Changing from one string reference to another, even if both references are the same string value, will fire the TextChanged event. If the recursion must continue naturally but will reach a steady state, this is actually the first thing to try:
public void Textbox1_TextChanged(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
... //build string
//now, even though the builder's ToString will produce a different reference,
//we're making sure we don't unnecessarily change the text.
if(builder.ToString != TextBox2.Text)
TextBox2.Text = builder.ToString();
}
public void Textbox2_TextChanged(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
... //build string
//now, even though the builder's ToString will produce a different reference,
//we're making sure we don't unnecessarily change the text.
if(builder.ToString != TextBox1.Text)
TextBox1.Text = builder.ToString();
}

Categories

Resources