Is there a better way to close buttons on click? - c#

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.

Related

C# Winform Radio button behavior

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.

How can I use mousemove event in c#

The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}

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);
}
}

C# Tabless Control Previous/Back/Return Button failing?

I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.

how to generate a SelectionRangeChanged Event Programatically ChartControl WinForms

want to create a selectionRangeChanged event programatically not really getting how to do it
private void btn_10D_Click(object sender, EventArgs e)
{
double varRange = 10;
double var_Sel1 = DatesX[0].ToOADate();
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionColor = Color.LightGray;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionStart = var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionEnd = varRange + var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.Position = varRange + var_Sel1;
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
}
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
throw new NotImplementedException();
}
thank you
For all events in C# is true that if class creator did not make extra effort to allow event firing form outside of class it is impossible to fire them.
According to MSDN
Chart.SelectionRangeChanged event Occurs when the selection start position or end position is changed.
But from my tests I can see that it is fired only if it is changed by user not program.
If I understand your intention correctly you want to handle those small buttons under your chart and btn_10D_Click method is a click handler for one of them. Try to move this line
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
to your constructor and ensure it is called once (remove it form other handlers). This will ensure your code is executed when user changes selection. If you want to execute same code for your button you should simply extract handler contents to method and call it form button click handler.
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
DoSomething(/*some arguments if you need them*/);
}
private void btn_10D_Click(object sender, EventArgs e)
{
\\your code
DoSomething();
}

Categories

Resources