Checking a radiobutton if a button is clicked - c#

How can I make it so that when I click a button, it checks a radiobutton? I'm trying to make it so that a radiobox is checked when the button is clicked

Did you even tried it yourself?
You just have to set the IsChecked-Property of your Radiobutton in the Click-Eventhandler of the Button.

You can add next line in on click event of the button:
radioButton.Checked = true;

public MainWindow()
{
// This button needs to exist on your form.
//Bind your button click event, this event will be triggered when button is clicked.
myButton.Click += myButton_Click;
}
//Once the button is clicked, now you can set the Checked property of your boolean.
void myButton_Click(object sender, RoutedEventArgs e)
{
radioButton.Checked = true;
}

Related

How do I replace text that is already set in a richtextbox by using another radio button?

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

Visual Studio, Devexpress- set button click event to do nothing

I want to click on the editor button and for it to do nothing. I still want to keep it enabled though. I tried for hours, any suggestions?
Automatically, clicking the button will allow the dropdownlist to come up. I want it to be when I click the button, nothing happens.
private void LookUpEdit1_ButtonClick(object sender, ButtonPressedEventArgs e)
{
// code here
}
Handle the QueryPopUp event. In the event handler, set the e.Cancel parameter to True.
private void LookUpEdit1_QueryPopUp(object sender,
System.ComponentModel.CancelEventArgs e) {
ComboBoxEdit combo = sender as ComboBoxEdit;
e.Cancel = true;
}

Clicking a Button in a button array

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

Determining the source of the click event dynamically

I have a set of buttons that are added dynamically. As the user keeps clicking the buttons, new buttons are added to the window. I am using a winforms. I am binding the onclick event of all these buttons to the same function. I am using the following code.
System.EventHandler myEventHandle= new System.EventHandler(this.Button_Clicked);
Once a new dynamic button is created I add the event handler with the following code:
b1.Click += myEventHandle;
Now in the function Button_Clicked() I want to get the Button which invoked this event. I want to disable this Button so that it cannot be clicked again and I want the name of the Button that was clicked as I want to do various actions depending on button name. I am newbie in C#.
This is what I have tried so far, but doesn't seem to work:
Button b = sender as System.Windows.Forms.Button;
b.Font = new Font(b.Font, FontStyle.Bold);
Console.WriteLine(""+b.Name);
b.Enabled = false;
Use the sender of the event
if(sender is Button)
{
Button b = sender as Button;
b.Enabled = false;
///something = b.Name;
}
Well, your Button_Clicked method must look like
void Button_Clicked(object sender, EventArgs e) {
Button clickedButton = (Button)sender;//if sender is always a Button
clickedButton.Enabled = false;
}

Problem in setting Checked property of radio button

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

Categories