none
Universal Click event? RRS feed

  • Question

  • Hello,

    I've just started learning programming and started with C#. I'm working in Visual Studio C# .Net and bought a book with example programs with code. So i stumbled upon an example(the description text from the example is the following "To all RadioBox-es apply one universal Click Event threw which the object sender appoints which RadioButton is clicked )" where it is written that i have to assign an universal Click event on Radio boxes(Three radio boxes). In the answer, or to be exact in the finished code, when assigned it shows up like this:

    private void rbPrevozUniversal_Click(object sender, EventArgs e)

    {

    }

    Hhow do i assign the universal click event on those 3 radio boxes? I'm literally new to all this i know its a stupid question but can't find the solution.

    An extra thing, the radio boxes are inside the groupBox and i need the universal event click because i'm trying to learn how StringBuilder and object sender function.

    Thanks.



    • Edited by Zivanovic Wednesday, January 29, 2020 11:04 PM
    Wednesday, January 29, 2020 11:02 PM

Answers


  • George Frias - AWWshop @ Wikidot, Wiki1978

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:38 PM
    Thursday, January 30, 2020 12:37 AM
  • Greetings Zivanovic.

    Karen's reply is absolutely correct and efficient, but I worry it might be a tad difficult for a complete beginner to follow (typical Karen).

    Here's a slightly simpler illustration of the principle.

          public Form1()
          {
             InitializeComponent();
    
             // Assuming we have three radiobuttons, called radioButton1, radioButton2, and radioButton3.
             this.radioButton1.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
             this.radioButton2.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
             this.radioButton3.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
    
          }
          private void rbPrevozUniversal_Click(object sender, EventArgs e)
          {
             // Check which button is the 'sender'.
             if (sender == radioButton1)
             {
                MessageBox.Show("Clicked 1");
             }
             else if (sender == radioButton2)
             {
                MessageBox.Show("Clicked 2");
             }
             else if (sender == radioButton3)
             {
                MessageBox.Show("Clicked 3");
             }
          }
    

    Assuming this is a Forms application, you can also add a click event handler to a control by going to Properties, selecting the little lightning bolt icon in the Properties toolbar, then double-clicking on the event you want (in this case, the click). This is the usual way to do it, but it will create a different event handler for each control, which is not what you want in this case.

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:39 PM
    Wednesday, January 29, 2020 11:40 PM
  • In order to assign the same handler to just inserted radio-buttons, select all of them. Use mouse, or select the first control, then hold <Ctrl> and click other controls. Then go to Properties, Events tab, and double-click the Click event.

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:31 PM
    Thursday, January 30, 2020 6:08 AM

All replies

  • This is how you can have one Click event for several RadioButton controls, in this case in a GroupBox.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                // For each Radio button in GroupBox1 subscribe to a single click event
                groupBox1.Controls.OfType<RadioButton>().ToList()
                    .ForEach(rb => rb.Click += groupBox1RadioButton_Click);
            }
    
            private void groupBox1RadioButton_Click(object sender, EventArgs e)
            {
                // get name
                Console.WriteLine(((RadioButton)sender).Name);
    
                // get the selected that is checked
                var selected = groupBox1.Controls.OfType<RadioButton>()
                    .FirstOrDefault(rb => rb.Checked);
    
                // test for a checked RadioButton and show if checked
                if (selected != null)
                {
                    MessageBox.Show($"Selected {selected.Name}");
                }
            }
        }
    }

    Get names

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(string.Join("\n", 
            groupBox1.Controls.OfType<RadioButton>().Select(rb => rb.Name)
                .OrderBy(rb => rb.Name()).ToArray()));
    }

    Get names and who is checked

    private void button1_Click(object sender, EventArgs e)
    {
        var sb = new StringBuilder();
        var results = groupBox1.Controls.OfType<RadioButton>()
            .Select(rb => new { Name = rb.Name, Checked = rb.Checked })
            .OrderBy(rb => rb.Name).ToArray();
    
        foreach (var item in results)
        {
            sb.AppendLine($"Name: {item.Name} - Checked: {item.Checked}");
        }
    
        MessageBox.Show(sb.ToString());
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Wednesday, January 29, 2020 11:22 PM
    Moderator
  • Greetings Zivanovic.

    Karen's reply is absolutely correct and efficient, but I worry it might be a tad difficult for a complete beginner to follow (typical Karen).

    Here's a slightly simpler illustration of the principle.

          public Form1()
          {
             InitializeComponent();
    
             // Assuming we have three radiobuttons, called radioButton1, radioButton2, and radioButton3.
             this.radioButton1.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
             this.radioButton2.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
             this.radioButton3.Click += new System.EventHandler(this.rbPrevozUniversal_Click);
    
          }
          private void rbPrevozUniversal_Click(object sender, EventArgs e)
          {
             // Check which button is the 'sender'.
             if (sender == radioButton1)
             {
                MessageBox.Show("Clicked 1");
             }
             else if (sender == radioButton2)
             {
                MessageBox.Show("Clicked 2");
             }
             else if (sender == radioButton3)
             {
                MessageBox.Show("Clicked 3");
             }
          }
    

    Assuming this is a Forms application, you can also add a click event handler to a control by going to Properties, selecting the little lightning bolt icon in the Properties toolbar, then double-clicking on the event you want (in this case, the click). This is the usual way to do it, but it will create a different event handler for each control, which is not what you want in this case.

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:39 PM
    Wednesday, January 29, 2020 11:40 PM
  • There is information in How to: Connect Multiple Events to a Single Event Handler in Windows Forms that should help.

    I'd like to add to it to make it a bit more clear.

    Add the new event and then just use handle, so from that page you see "Handles Button1.Click, Button2.Click" following the method.


    George Frias - AWWshop @ Wikidot, Wiki1978

    Thursday, January 30, 2020 12:20 AM
  • I just found out that doesn't work exactly the same for VB and C#, trying to figure that out...

    George Frias - AWWshop @ Wikidot, Wiki1978


    • Edited by GeoFrias Thursday, January 30, 2020 12:30 AM
    Thursday, January 30, 2020 12:30 AM

  • George Frias - AWWshop @ Wikidot, Wiki1978

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:38 PM
    Thursday, January 30, 2020 12:37 AM
  • In order to assign the same handler to just inserted radio-buttons, select all of them. Use mouse, or select the first control, then hold <Ctrl> and click other controls. Then go to Properties, Events tab, and double-click the Click event.

    • Marked as answer by Zivanovic Thursday, January 30, 2020 12:31 PM
    Thursday, January 30, 2020 6:08 AM
  • That's what i was looking for! Thanks for the help!
    Thursday, January 30, 2020 12:32 PM
  • Found it at last, thanks!
    Thursday, January 30, 2020 12:33 PM
  • That's a cool approach that i didn't know, by the book it looks like they went over the click event handler in properties. Just found it, thanks for the help! Very deeply appreciated!
    Thursday, January 30, 2020 12:36 PM
  • Thanks for the reply,

    will try that approach, for me at this moment it looks challenging but will go over it eventually!


    Thursday, January 30, 2020 12:40 PM
  • So I'm guessing you don't care for efficient code?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 30, 2020 1:03 PM
    Moderator