In my project, There are two radioButtons. To which I have given same CheckedChanged event by doing
something like this:
DoctorRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
PatientRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
I kept both the RadioButtons in a Panel to make them one true while other one is false.
Now the problem is that I am implementing a very big code in the RadioButton_CheckedChanged event.
Whenever the user is changing the state of any of the two RadioButtons, the event is raising two times.
After so many hours I got the answer, the event is raising two times because both the RadioButton states are being changed(Hence, the event will be raised two times). To solve this problem I am trying to unhook the event temporarily something like this:
RadioButton_CheckedChanged Event: (Not Working)
if (DoctorRadioButton.Checked)
{
PatientRadioButton.CheckedChanged -= RadioButton_CheckedChanged; //Un
//
//My functions
//
PatientRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
}
else
{
DoctorRadioButton.CheckedChanged -= RadioButton_CheckedChanged;
//
//My functions
//
DoctorRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
}
Eventhough the event is executing two times. I know I am doing something wrong in Hooking and Unhooking. Please Help.
You can check the sender RadioButton and place your code accordingly like this -
void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderRadioButton = sender as RadioButton;
if (senderRadioButton.Equals(DoctorRadioButton))
// OR senderRadioButton.Name == "DoctorRadioButton"
{
// Place your code here for DoctorRadioButton.
}
else
{
// Place your code here for PatientRadioButton.
}
}
Update
If you can't use two different handlers for both radioButtons and want to execute code only in case checkbox is checked you can do this -
void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton senderRadioButton = sender as RadioButton;
if (senderRadioButton.IsChecked)
{
// Place your code here for check event.
}
}
For an extremely simple (albeit crude) solution would be to not hook both the radio buttons, and hook only one of them to the handler: since checking one radio unchecks the other one, it would work as intended.
A more complicated way would be to use a backing property, like this:
class myForm
{
private bool radioStatus = false; // depends on the default status of the radios
private bool RadioStatus
{
get{return radioStatus;} set {radioStatus = value; Checked_Changed();}
}
public myForm()
{
// Lambdas as handlers to keep code short.
DoctorRadioButton.CheckedChanged += (s,args)=>
{ if((s as RadioButton).Checked) RadioStatus = true; };
PatientRadioButton.CheckedChanged += (s,args)=>
{ if((s as RadioButton).Checked) RadioStatus = false; };
}
void Checked_Changed()
{
if (RadioStatus) // = true --> DoctorRadioButton was checked
{
//code
}
else // = false --> PatientRadioButton was checked
{
//other code
}
}
}
This approach has the advantage of allowing you to abstract from the UI a bit.
Put both radio buttons in the same panel or groupbox and automatically they will be grouped so that only one can be selected at a time.
Its a late solution but i found there is no correct answer for your question so i am posting it may be it works For You
Create Click Event for both radio button and simple put your code beacuse on every click your radio button got checked and your code executes :):):)
private void DoctorRadioButtons_Click(object sender, EventArgs e)
{
//Your code on Doctor Radio Button
}
private void PatientRadioButton_Click(object sender, EventArgs e)
{
// Your code on Patient Radio Button
}
Related
I have a simple WPF application that uses a "frame" for multi-page navigation. One of that pages creates a series of CheckBoxes and adds a couple of handlers (Checked/Unchecked) for each checkbox created. The CheckBoxes work as intended and are programmatically accessible, they can be checked or unchecked by click but none of the two events is ever fired if I click.
Here is the creation of the CheckBoxes:
ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].Checked += new RoutedEventHandler(ModuleCheckBoxClick);
ModuleCheckBoxes[i].Unchecked += new RoutedEventHandler(ModuleCheckBoxClick);
Where I go from 0 to 30. Then I have the handler:
private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
int CheckBoxCounter = 0;
for(int i=0;i<30;i++)
{
if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
}
if(CheckBoxCounter > 1)
{
Button_QueryStatus.IsEnabled = false;
}
}
But nothing is fired.
Someone has got an idea?
Try to use CheckedChanged instead of Checked and Unchecked.
For example:
public bool checkedthecheckbox { get; set; }
CheckBox testchbox = new CheckBox();
private void Form1_Load(object sender, EventArgs e)
{
testchbox.CheckedChanged += new EventHandler(testchbox_CheckedChanged);
}
void testchbox_CheckedChanged(object sender, EventArgs e)
{
if (testchbox.Checked)
checkedthecheckbox = true;
else
checkedthecheckbox = false;
}
In your case:
ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].CheckedChanged += new EventHandler(ModuleCheckBoxClick);
private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
int CheckBoxCounter = 0;
for(int i=0;i<30;i++)
{
if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
}
if(CheckBoxCounter > 1)
{
Button_QueryStatus.IsEnabled = false;
}
}
Good news! Not all the checkboxes had the event handler because of a fault in the creation of the buttons. 3 on 30 had it, and these buttons represented a different kind of item.
I just dealt with a similar issue on a WPF app that I did not write the front end for and here it came down to: Checked and similar event handlers will fire if the checkbox is triggered manually or programmatically. Other types are not guaranteed in the same way if you are changing the checkbox by setting to IsChecked.
ex. in my case, they attempted to use a Clicked event handler which would only fire from actual user interaction and not programmatic change, which makes perfect sense because setting the IsChecked status is truly not a click event (even though both can potentially check or uncheck the checkbox)
I have a treenode which displays a checklist from a SQL database. I have a method to get the selected workflows.
I want to enable the run button if a checkbox is checked and disable the button if nothing is checked and on load.
I'm not sure where to put this if statement. I have tried putting it under the run button on the click action but it is not working correctly.
Any help is appreciated.
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel();
var workflowList = GetSelectedWrokflows();
if (workflowList.Count == 0)
{
button.enabled = false;
}
else
{
button.enabled = true;
}
One way to do this is to create a method that will do the work of determining the selected workflow items and enabling or disabling the button. By putting the code in a single method, it allows you to call it from multiple places, and if you need to change the behavior, you only have one place to make the modifications.
Then you can just call this method from the Form_Load event, and from the checked list box's ItemCheck event:
public partial class Form1 : Form
{
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel>();
private void SetRunButtonState()
{
workflowViewList = GetSelectedWorkflows();
button.Enabled = workflowViewList.Count > 0;
}
private void Form1_Load(object sender, EventArgs e)
{
SetRunButtonState();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
SetRunButtonState();
}
// Rest of class code omitted...
}
I have a group of 8 radiobuttons, is there a simple way to check if "checked" status has changed for the group? I don't need to know which button has been checked, just if a different button has been checked. Sort of like a CheckedChanged event for the whole group.
You can assign the same CheckedChanged event handler to all of the radiobuttons. When you check a radiobutton, the method will be called twice (for the radiobutton losing the checkmark and for the radiobutton being checked). So only handle the event for the one that is checked.
private void anyRadioButton_CheckedChanged(object sender, EventArgs e)
{
// The radio button that raised the event
var radioButton = sender as RadioButton;
// Only do something when the event was raised by the radiobutton
// being checked, so we don't do this twice.
if(radioButton.Checked)
{
// Do something here
}
}
I think what you are looking for is this, a common handler for all radio buttons in the group
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
You can wire up all of the RadioButtons' CheckedChanged event to the same handler. Follow this code.
public Form1()
{
rB1.CheckedChanged += new EventHandler(rB_CheckedChanged);
rB2.CheckedChanged += new EventHandler(rB_CheckedChanged);
}
private void rB_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (rB1.Checked)
{
}
else if (rB2.Checked)
{
}
}
I have absolutely no programmatic links or properties set such that my CheckedChanged fires as a result of anything except checking the radio button.
However, when I click a different, unrelated button, the button's click handler fires (this is expected). In this click handler, the button disables itself (it re-enables on a different button's click), which then triggers myRadioButton_CheckedChanged handle for an unrelated radiobutton fires.
The call stack that I'm seeing is essentially
myRadioButton_CheckedChanged (...)
myButton_Click(...)
Main(...)
The line in myButton_Click that is triggering the myRadioButton_CheckedChanged is apparently
myButton.Enabled = false;
The related code is:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// L-R
if (radioButton1.Checked == true)
{
orientation_left = 3;
pictureBox2.Invalidate();
Debug.Print("left {0}", orientation_left);
}
}
private void select1_Click(object sender, EventArgs e)
{
Debug.Print("select1click");
if (select1Down == false)
{
// ... stuff
select1.Enabled = false; // Causing the CheckedChanged to fire
select2.Enabled = false;
select1Down = true;
}
}
Ok, got it.
Check the TabOrder on your Button and RadioButton.
Seems that when you disable the Button, the focus is shifted to the next control, which is probably your RadioButton, causing it to become checked.
On my test From, all I had to do was to make sure that the RadioButton's TabOrder was not right after the Button.
Cheers
EDIT:
This seems to be a known problem as I just found this MSDN thread: http://social.msdn.microsoft.com/Forums/windows/en-US/77fbec3b-1f63-42e1-a200-19b261b63794/the-radiobutton-clicked-event-is-fired-without-the-radio-button-beeing-clicked-?forum=winforms
Okay, it's kinda hacky but it works without changing anything to the tab order:
private void select1_Click(object sender, EventArgs e)
{
if (!select1Down)
{
// ... stuff
SendKeys.SendWait("{Tab}");
select1.Enabled = false;
select2.Enabled = false;
select1Down = true;
}
}
I'm creating listviews in a flowpanel at run time which later will accept drag and dropped files. the reason being is i want these to act as folders so a user double clicks and gets a window displaying the contents.
i'm having difficulty setting up the events for my listviews as they are added.
how do i create some events (like MouseDoubleClick and DragDrop) dynamically for each added listview? can i create a single function for both of these events and have listview1, listview2, listviewX use it?
i have a button that is adding the listviews, which works fine. please advise, i apologize if this is too conceptual and not exact enough.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
flowPanel.Controls.Add(newListView);
}
You would have to have the routine already created in your code:
private void listView_DragDrop(object sender, DragEventArgs e) {
// do stuff
}
private void listView_DragEnter(object sender, DragEventArgs e) {
// do stuff
}
and then in your routine, your wire it up:
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
flowPanel.Controls.Add(newListView);
}
You would have to check who the "sender" is if you need to know which ListView control is firing the event.
You can also just use a lambda function for simple things:
newListView.DragEnter += (s, de) => de.Effect = DragDropEffects.Copy;
Just make sure to unwire the events with -= if you also remove the ListViews dynamically.
To answer the other half of your question, you can use a single handler for any event, from any source, that has the handler's signature. In the body of the handler, you just have to check the sender argument to determine which control raised the event.
You need a way to tell one control from a different one of the same class, however. One way to do this is to make sure to set the Name property on each control when you create it; e.g., newListView.Name = "FilesListView".
Then, before you do anything else in your event handler, check the sender.
private void listView_DragDrop(object sender, DragEventArgs e) {
ListView sendingListView = sender as ListView;
if(sendingListView == null) {
// Sender wasn't a ListView. (But bear in mind it could be any class of
// control that you've wired to this handler, so check those classes if
// need be.)
return;
}
switch(sendingListView.Name) {
case "FilesListView":
// do stuff for a dropped file
break;
case "TextListView":
// do stuff for dropped text
break;
.....
}
}