Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 11th, 2011, 7:39 PM   #1
Zeno
Newbie
 
Join Date: May 2011
Posts: 13
Rep Power: 0 Zeno is on a distinguished road
C# Control Arrays

Hi,

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
               System.Windows.Forms.RadioButton();
            // Add the button to the collection's internal list.
            this.List.Add(rButton);
            // Add the button to the controls collection of the form 
            // referenced by the HostForm field.
            HostForm.Controls.Add(rButton);
            // 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;
            this.AddNewRadioButton();
        } 



        public System.Windows.Forms.RadioButton this[int Index]
        {
            // EXPOSING ARRAYS - Creating the default property:
            get
            {
                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?

Thanks,
James
Zeno is offline   Reply With Quote
Old Jun 12th, 2011, 2:07 AM   #2
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 15 The Dark will become famous soon enoughThe Dark will become famous soon enough
Re: C# Control Arrays

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.
The Dark is offline   Reply With Quote
Old Dec 3rd, 2014, 12:38 AM   #3
bonave
Newbie
 
Join Date: Jun 2010
Posts: 6
Rep Power: 0 bonave is on a distinguished road
Re: C# Control Arrays

Check this simple program on...C# Control Array

Bona
bonave is offline   Reply With Quote
Old Dec 4th, 2014, 1:03 AM   #4
Zeno
Newbie
 
Join Date: May 2011
Posts: 13
Rep Power: 0 Zeno is on a distinguished road
Re: C# Control Arrays

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.
Zeno is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting multi level Associative arrays AMERLOC PHP 0 Sep 8th, 2009 8:54 AM
cration of a new control, problem with adding new controls beni_dude C# 5 Apr 15th, 2008 4:31 PM
Saving image from picture control darkies22 C++ 1 Jan 31st, 2008 5:53 AM
How to add inherited control to toolbox. InfoGeek C# 3 Feb 8th, 2007 9:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC