0

First, I apologize for my insufficient English-skill.

I'm creating my own image button, and I want to make my control interact with each other like a radiobutton controls.

When the user selects one option button (also known as a radio button) within a group, the others should be cleared automatically.

In this situation, here are two images (1m.png, 2m.png). If I click one image button, image changes to 1m.png, while the others automatically change their image to 2m.png.

Thank you for reading and please help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mguin
  • 13
  • 1
  • Hi, welcome to stack overflow. Please refer the [ask] link for more details on how to ask a question and update your question accordingly. – Jeroen Heier Oct 30 '17 at 04:48
  • You can customize `RadioButton` painting. For example, take a look at [Circular RadioButton List in Windows Forms](https://stackoverflow.com/q/38366222/3110834) – Reza Aghaei Oct 30 '17 at 05:39
  • Thank you for answers! I solved it. by using a parent property. – mguin Nov 03 '17 at 08:21

1 Answers1

0

You can achieve it by overriding OnClick event. I've just written this code and it works smoothly. I did tested by changing the BackColor of the control. Based on your needs, you can change the Image, the BackgroundImage or any other property.

using System;
using System.Linq;
using System.Windows.Forms;

namespace StackOverflow
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
    }

    public partial class ImageButton : Button
    {
        //Override OnClick event.
        protected override void OnClick(EventArgs e)
        {
            /*
              "Unselect" every ImageButton that belongs to the 
              same group as  the current ImageButton and 
              select the current one.
            */
            do
            {
                /*
                    Change Image of current ImageButton. 
                    I'm changing the BackColor for simplicity. You have to remove this line.
                */
                this.BackColor = System.Drawing.Color.Green;
                //this.BackgroundImage = 1m.png;

                //If ImageButton parent is null, then it doesn't belong in a group.
                if (this.Parent == null)
                    break;

                /*
                    Else loop through all other ImageButtons of the same group and clear them. 
                    Include System.Linq for this part.
                */
                foreach (ImageButton button in this.Parent.Controls.OfType<ImageButton>())
                {
                    //If button equals to current ImageButton, continue to the next ImageButton.
                    if (button == this)
                        continue;

                    //Else change the Image.
                    button.BackColor = System.Drawing.Color.Red;
                    //this.BackgroundImage = 2m.png;
                }
            }
            while (false);

            //Continue with the regular OnClick event.
            base.OnClick(e);
        }
    }
}

enter image description here

Efthymios
  • 253
  • 3
  • 10