code_junkie

How to determine if radioButton.Checked is set programatically or via a mouse click?

Nov 11th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. this.radioButtonAdd.Checked = true;
  2.  
  3. savedFlag = modifyingProgrammatically;
  4. modifyingProgrammatically = true;
  5. this.radioButtonAdd.Checked = true;
  6. modifingProgrammatically = savedFlag;
  7.  
  8. if(modifyingProgramatically) {
  9. // The event was raised by an assignment within the code.
  10. } else {
  11. // The event was raised by a user action.
  12. }
  13.  
  14. radioButton.Tag = "ignore"
  15. radioButton.Checked = true
  16.  
  17. private void radioButton_CheckedChanged(object sender, EventArgs e)
  18. {
  19. if (radioButton.Checked)
  20. {
  21. // Tag will be null in cases where the user clicks
  22. if (this.radioButtonAdd.Tag == null)
  23. {
  24. // do something
  25. }
  26. else
  27. {
  28. // swallow action
  29. // reset Tag
  30. this.radioButtonAdd.Tag = null;
  31. }
  32. }
  33. }
  34.  
  35. private bool _mouseEvent;
  36. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  37. {
  38. if (_mouseEvent)
  39. MessageBox.Show("Changed by mouse click.");
  40. else
  41. MessageBox.Show("Changed from code.");
  42. }
  43.  
  44. private void radioButton1_MouseClick(object sender, MouseEventArgs e)
  45. {
  46. _mouseEvent = false;
  47. }
  48.  
  49. private void radioButton1_MouseDown(object sender, MouseEventArgs e)
  50. {
  51. _mouseEvent = true;
  52. }
  53.  
  54. private void button1_Click(object sender, EventArgs e)
  55. {
  56. // This simulates a change from code vs. a change from
  57. // a mouse click.
  58. if (radioButton1.Checked)
  59. radioButton2.Checked = true;
  60. else
  61. radioButton1.Checked = true;
  62. }
Add Comment
Please, Sign In to add comment