C# Control Arrays


I'm creating control arrays using the MSDN Library instructions here:
Creating Control Arrays

The system works perfect and I'm able to modify it to create an array of any type of control I like.

However, I' d like for this code to be independent of the specific form that is using it. In other words I'd like to be able to use this code by calling it from arbitrary forms.

I was able to make it abstract all but for the actual "Clickhandler" routine.

Here's the code I'm using:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Braiding_Console
    /// <summary
    /// ********** Public Class Radio_Buttons **********
    /// Generic routine to create an indexed Radiobutton array
    /// </summary>
    public class Radio_Buttons : System.Collections.CollectionBase
        private readonly System.Windows.Forms.Form HostForm;
        public System.Windows.Forms.RadioButton AddNewRadioButton()
            // Create a new instance of the RadioButton class.
            System.Windows.Forms.RadioButton rButton = new
            // Add the button to the collection's internal list.
            // Add the button to the controls collection of the form 
            // referenced by the HostForm field.
            // Set intial properties for the button object.
            rButton.Tag = this.Count;
            rButton.Click += new System.EventHandler(ClickHandler);
            return rButton;
         // Replace the default constructor with this one.
        public Radio_Buttons(System.Windows.Forms.Form host)
            HostForm = host;

        public System.Windows.Forms.RadioButton this[int Index]
            // EXPOSING ARRAYS - Creating the default property:
                return (System.Windows.Forms.RadioButton)this.List[Index];
        public void Remove()
            // Check to be sure there is a button to remove.
            if (this.Count > 0)
                // Remove the last button added to the array from the host form 
                // controls collection. Note the use of the indexer in accessing 
                // the array.
                HostForm.Controls.Remove(this[this.Count - 1]);
                this.List.RemoveAt(this.Count - 1);
        public void ClickHandler(Object sender, System.EventArgs e)
            // Braiding_Console.FrmConsole.numStrands 
            string temp;
            temp = ((System.Windows.Forms.RadioButton)sender).Tag.ToString();
            Braiding_Console.FrmConsole.numStrands = int.Parse(temp) + 1;

The part I'd like to free up is in the last routine "ClickHandler".

I can use this routine as-is but that ties it to a specific form that's using these controls. In other words, I need to actually use the address of the form that called it in order to know that a control has been clicked. This can be seen in the last line where I use "Braiding_Console.FrmConsole.numbstrands" in order to update a variable on my main form.

What I would like to do is have this "ClickHandler" actually pass control to the "Host Form" that is using this control array.

I don't know how to set this up.

I'm thinking that it has something to do with a knowledge of how to use "Host" to access the host form.


Or even better yet, if I could have the eventhandler actually point to host form without have to hardwire that, this would be good too.

In other words, maybe in the following line of code I could do something to point to the host form?

rButton.Click += new System.EventHandler(ClickHandler);

Instead of calling out "ClickHandler", if I could call out something like Host.Clickhandler. or HostForm.Clickhandler, that would be ideal.

In other words, I want to be able to call the above code from various different forms, and when the control array is created those controls would activate a clickhandler routine on the form that actually created the control array.

I would only be calling this routine from a single form at a time. But I would like for it to be independent of the form that uses it.


I hope my concern here makes sense.

I've freed this up for everything except the "Clickhandler" routine. I don't know how to make that abstract. (i.e. not needing to be "hard-wired" to a specific form like I have it above.

Does anyone know how to do this?



  • Here are some ways you could do it:
    1. Have the caller pass the click handler into the AddNewRadioButton function as a parameter (either as a function, or a System.EventHandler). Something like:
            public System.Windows.Forms.RadioButton AddNewRadioButton(System.EventHandler OnClick)
                if (OnClick != null)
                   rButton.Click += OnClick;
                return rButton;
    That way, the caller could have different click handlers for different buttons.

    2. As #1 except pass the OnClick function in to the Constructor, that way they don't need to pass it in for each add.

    3. Change your constructor to accept an object of your own class (e.g. ClickableForm), which inherits from System.Windows.Forms.Form and has an overrideable RadioButtonClicked method that they can implement. I', not at my Windows computer at the moment, so I can't try it out to see if it will work.
  • Check this simple program on...C# Control Array

  • Thanks for the Link Bona

    I asked this question in 2011. I've actually gotten away from programming since then. But I may still find this useful yet. Thanks for the link. I do enjoy programming and I might actually return to it. I used to love control arrays in Visual Basic and I used them all the time, so when they were missing from C# that was a real bummer.

    I might actually get back into programming now just to try this out.
Sign In or Register to comment.