I have a Repeater Control with various buttons in it.
When the button gets clicked, it needs to disable itself so it cant be clicked again. Working.
However, when I click that button, it needs to enable any other button but it.
So, When I click on it, it needs to disable. When I click on another one, the previous button must enable, and that one must disable.
So for I've tried:
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Button b in e.Item.Controls.OfType<Button>().Select(c => c).Where(b => b != btnLoad))
{
b.Enabled = true;
}
btnLoad.Text = "Currently Viewing";
btnLoad.Enabled = false;
But it isnt working. Depending on where I put it, its either leaving all the buttons enabled (But still changing its text), or not doing anything at all.
What do I need to do to make this work?
Edit: The code is found here:
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
Which is why I use Button btnLoad = (Button)e.Item.FindControl("btnLoad");.
The method is found in :
switch (e.CommandName)
{
case "LoadDoc":
//Above code
break;
}
Assuming you want that code in the Button's Click-event handler:
protected void Button_Clicked(Object sender, EventArgs e)
{
Button thisButton = (Button) sender;
thisButton.Text = "Currently Viewing";
RepeaterItem item = (RepeaterItem) thisButton.NamingContainer;
IEnumerable<Button> buttons = item.Controls.OfType<Button>();
foreach(var btn in buttons)
{
btn.Enabled = btn != thisButton;
}
}
Try a simple if statement in a loop as below
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Control c in e.Item.Controls)
{
if (b is Button)
{
if(b == btnLoad)
{
b.Enabled = false;
}
else
{
b.Enabled = true;
}
}
}
Related
How to check radio button default
For example, if I have 5 radio buttons in the group, I want to check the default 1st radio button.
I need a code for updating it, like when the project is running, and I click the 2nd radio button, and the update button default should change to the 2nd radio button.
Select all RadioButtons with Strg
Go to Events and double click CheckedChanged
Then just check whether the button is checked or not
private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
//Do something
}
else if (radioButton2.Checked)
{
//Do something
}
else if (radioButton3.Checked)
{
//Do something
}
}
If you are using Winforms
Add Settings to you project (Set int value)
Add the Code will save the current radio checked Add in all radios
Properties.Settings.Default.currentRadio = 1;
Properties.Settings.Default.Save();
In the form initializing set the current checked Radio
if (Properties.Settings.Default.currentRadio == 1)
{
radioButton1.Checked = true;
}
else if(Properties.Settings.Default.currentRadio == 2)
{
radioButton2.Checked = true;
}
I am trying to disable the thin and crispy checkbox when traditional checkbox is clicked. I have these in a group due to me enabling the whole group when the numericUpDown value is set to 1. When I click traditional checkbox, it doesn't disable the thin and crispy checkbox. I am using windows form application
Code
private void NudQuantity1_ValueChanged(object sender, EventArgs e)
{
if (NudQuantity1.Value == 0)
{
gbCheesePizza.Enabled = false;
}
else
{
gbCheesePizza.Enabled = true;
}
if (CBXTraditional1.Checked == true)
{
CBXthinandcrispy1.Enabled = false;
}
}
When I run this code outside of a groupbox, it works perfectly.
I don't think this block should be inside the event handler
if (CBXTraditional1.Checked == true)
{
CBXthinandcrispy1.Enabled = false;
}
It means that, provided you've got no other event handling for the checkboxes, this code will only be executed when you change the value of NudQuantity1 so it won't execute anything when you click the checkboxes afterwards.
Try use radio buttons as Steve mentioned. They do this for you.
I have the code below.
public void WepaonEquip(Object sender, System.EventArgs e)
{
if (button[0].BackColor == Color.Beige)
{
button[0].BackColor = Color.OrangeRed;
}
else if (button[1].BackColor == Color.Beige)
{
button[1].BackColor = Color.OrangeRed;
}
else if (button[2].BackColor == Color.Beige)
{
button[2].BackColor = Color.OrangeRed;
}
}
The code in the class containing this chunk of code generates a button array. What I want is that the user will click a button and the colour of the button clicked will change.
However, when the user clicks, lets say, the 3rd button, the first button in the array changes colour, not the one clicked. Any idea as to why this isn't working? I believe the logic of the code works, perhaps I'm missing something.
Set each button in the panel to use the same Click Event handler. In the handler cast sender as a button and change the color
Assuming that WeaponEquip is the click event handler for the buttons it would look something like this:
public void WepaonEquip(Object sender, System.EventArgs e)
{
Button clickedbutton = (Button)sender
clickedbutton.BackColor = Color.OrangeRed;
}
I have a Problem with addressing a button. I have many buttons in my program and I have a function which is used by every button.
I'm getting the name of the last clicked button with this:
foreach (Control t in this.Controls)
{
if (t.Focused)
{
ClickedButton = t.Name;
}
}
Then I want to change the Text of the button:
ClickedButton.Text = "Whatever";
But I can't use ClickedButton as the name of the button.
Thank you in advance!
Assuming this is an event, you can just do something like this
Button btn = (Button)sender;
btn.Text = "Whatever";
If you are writing this in your button_click event,
You can Get the button like this :
Button BTN = sender as Button;
BTN.Text = "This Button Has Been Clicked!";
If you're having the clicked button call into the Click event, you should have sender as an argument, which you can cast to a Button and get the name of the control.
Since you have the button reference, you could then also set the control's text.
protected void btnTest_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if ((b != null) && (b.Name == "btnTest"))
{
b.Text = "yay";
}
}
I have a winforms app and the main (and only) form has several buttons. When the user clicks a button, I want all other buttons to be disabled.
I know I could do this the long way by setting "Enabled" to false on all buttons but the one which has been clicked in the clicked event handler of that button, but this is a long and tedious approach.
I got the collection of controls on the form, but this does not include the several buttons I have:
System.Windows.Forms.Control.ControlCollection ctrls = this.Controls;
How can I go about implementing this functionality in a mantainable fashion?
Thanks
DisableControls(Control c)
{
c.Enable = false;
foreach(Control child in c.Controls)
DisableControls(child)
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
{
Button btn = (Button)ctrl;
btn.Click += ButtonClick;
}
}
}
private void ButtonClick(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
{
Button btn = (Button)ctrl;
if (btn != (Button)sender)
{
btn.Enabled = false;
}
}
}
}
You could use databinding, so you only iterate once on startup:
public partial class Form1 : Form
{
public bool ButtonsEnabled { get; set; }
public Form1()
{
InitializeComponent();
// Enable by default
ButtonsEnabled = true;
// Set the bindings.
FindButtons(this);
}
private void button_Click(object sender, EventArgs e)
{
// Set the bound property
ButtonsEnabled = false;
// Force the buttons to update themselves
this.BindingContext[this].ResumeBinding();
// Renable the clicked button
Button thisButton = sender as Button;
thisButton.Enabled = true;
}
private void FindButtons(Control control)
{
// If the control is a button, bind the Enabled property to ButtonsEnabled
if (control is Button)
{
control.DataBindings.Add("Enabled", this, "ButtonsEnabled");
}
// Check it's children
foreach(Control child in control.Controls)
{
FindButtons(child);
}
}
}
Expanding on Alex Reitbort's answer, here's an example method I just wrote 20 minutes ago for this project I'm doing:
private void Foo(bool enabled)
{
foreach (Control c in this.Controls)
if (c is Button && c.Tag != null)
c.Enabled = enabled;
}
This function is not recursive like his is. However, because I know there are no Buttons within a container other than my form so I don't need to worry about that. If there were child controls (ie controls within a GroupBox for example), I'd probably modify that code to be something like this;
private void ToggleControls()
{
Foo(this.Controls, false) // this being the form, of course.
}
private void Foo(Control.ControlCollection controls, bool enabled)
{
foreach (Control c in controls)
{
if (c is Button && c.Tag != null)
c.Enabled = enabled;
if (c.HasChildren)
Foo(c.Controls, enabled);
}
}
I actually like his method a little more, that style of recursion looks a little cleaner than mine I think. Just showing you an alternate way.
Note; I have 6 buttons on my form and I only want 4 of them to have their Enabled property modified by this method. To accomplish this, I set their Tag property to some random string. That is why I check for c.Tag in my if statements. You can remove that if you know you want to disable every control.