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;
}
Related
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.
When I click on one radio button, it sets the text in my richtextbox. If I click on another one, it'll do nothing. Is it possible to replace the text with another radio button?
private void M_buttonComment_CheckedChanged(object sender, EventArgs e)
{
if (M_buttonComment.Checked) //If checked == true
{
// Set the text to be "Comment" //
M_TitleTextBox.Text = "Comment - ";
}
}
You need to subscribe to the same CheckChanged event for both the radio buttons.
Set this property for both radio buttons. (Name the method whatever you want, but make sure the name of the method is the same in the code.)
Then in your code:
private void SomeCustomEvent(object sender, EventArgs e)
{
if (radBtnOne.Checked) //If checked == true
{
M_TitleTextBox.Text = "From radio button one";
}
else if(radBtnTwo.Checked)
{
M_TitleTextBox.Text = "From radio button two";
}
}
Notice that the same thing is happening if either radio button is checked in my example. If you don't care which radio button was checked and just want to do the same thing regardless then the following would work. sender in this case would be the radio button clicked.
But you could also figure out which radio button was clicked by looking at their .Name property.
private void SomeCustomEvent(Object sender, EventArgs e) {
RadioButton rb = (RadioButton)sender;
if (rb.Checked) { // From either radio button
M_TitleTextBox.Text = "A radio button was clicked.";
if(rb.Name = "radBtnOne") // To check which one was checked.
{
// Now we know which radio button was clicked. Same process for the second
}
}
}
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 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;
}
}
}
I have designed a gridview with a radio button inside the itemtemplet. I also have a confirm button. A user is allowed to check several radio button and when a confirm button is pressed then database is updated with the value of radio button.
But when i check the button it is shown as checked in UI but when I check through code behind, it shows checked property of radio button as false.
for (int i = 0; i < gvTransaction.Rows.Count; i++)
{
if (!((String.IsNullOrEmpty(gvTransaction.Rows[i].Cells[0].Text)) || (gvTransaction.Rows[i].Cells[0].Text == " ")))
{
string transactionID = gvTransaction.Rows[i].Cells[0].Text;
RadioButton btn = (RadioButton)gvTransaction.Rows[i].FindControl("rbtSelect");
if (btn.Checked)
{
// Although the radiobutton is checked code never reach here.
}
}
}
If this is Winforms, my guess would be because the edited Radio button cell value is not committed until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler like this:
void gvTransaction_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (gvTransaction.IsCurrentCellDirty)
{
gvTransaction.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Use this if the radiobutton.checkedchanged event.
How are you binding the GridView? If you are doing it in Page Load, make sure that you are not reloading the gridView on page PostBacks
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Bind GridView here }
do let us know if this help