Create loop to change name of combo boxes

Create loop to change name of combo boxes

Anonymous
Not applicable
829 Views
5 Replies
Message 1 of 6

Create loop to change name of combo boxes

Anonymous
Not applicable

I am going thru the learning process with C# & .NET so I was wondering if anyone can help me out with this issue.

I have created a Windows Form which has 12 combo boxes (in rows of 3 numbered 1-12). On the Form_Load event I am trying to populate all the boxes with values obtained from the registry (previously set), please see sample below. At the moment it is written with the actual combo box names i.e. combobox1, combobox2 & combobox3 and does exactly what it should. However, I would like to wrap this in a loop that will run 4 times, changing the number of the Printer & each combo box accordingly. For example, on the 1st run we would have Printer1, combobox1, combobox2 & combobox3. The next run would then have Printer2, combobox4, combobox5 & combobox6...etc.

I am struggling to find a way to rename the comboboxes as they are not strings. Any help would be greatly appreciated.

Steve

 

private void Form1_Load(object sender, EventArgs e)
{
////for testing get the editor so we can write test strings to the command line
//Document doc =
// acadApp.DocumentManager.MdiActiveDocument;
//Editor ed = doc.Editor;
////////////////////////////////////////////////////////////////////////////

//Make the cancel button default
this.ActiveControl = button6;
//Get the plot device list & make a collection of the names
PlotSettingsValidator psv = PlotSettingsValidator.Current;
StringCollection devlist = psv.GetPlotDeviceList();

 

***START LOOP HERE***
//See if we have already reistered a device & if so set the combobox to that device
int printer = 1; //this is just me testing the following line...it should be set by the repeat loop
string regPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Ezcad\\EzQPlot\\Printer" + printer;
string name = (EzReg.EzRegGet.getRegentry(regPath, "Name").ToString());
string media = (EzReg.EzRegGet.getRegentry(regPath, "Media").ToString());
string style = (EzReg.EzRegGet.getRegentry(regPath, "Style").ToString());
if (devlist.Contains(name))
{

Ez_Combo_Add.EzComboList.setCombo(devlist, comboBox1);
this.comboBox1.SelectedIndex = devlist.IndexOf(name);
}
else
{
Ez_Combo_Add.EzComboList.setCombo(devlist, comboBox1);
this.comboBox1.SelectedIndex = 1;
}
//Get the plot settings to access the media
PlotSettings ps = new PlotSettings(true);
using (ps)
{
// We should refresh the lists, in case setting the device impacts the available media
psv.SetPlotConfigurationName(ps, name, null);
psv.RefreshLists(ps);
StringCollection medlist = psv.GetCanonicalMediaNameList(ps);
for (int i = 0; i < medlist.Count; i++)
{
string localMedia = (psv.GetLocaleMediaName(ps, medlist[i]));
comboBox2.Items.Add(localMedia);
if (localMedia == media)
{
string conName = medlist[i];
this.comboBox2.SelectedIndex = medlist.IndexOf(conName);
}
}
}
//Get style list
StringCollection styleList = psv.GetPlotStyleSheetList();
if (styleList.Contains(style))
{
Ez_Combo_Add.EzComboList.setCombo(styleList, comboBox3);
this.comboBox3.SelectedIndex = styleList.IndexOf(style);
}

0 Likes
830 Views
5 Replies
Replies (5)
Message 2 of 6

Keith.Brown
Advisor
Advisor

You should look at the MSDN documentation for forms.  Scroll down until you see the controls property.  This property will give you access to all of the controls on your form as a collection.  You can use it in a loop to access the controls that you want.

 

You might also want to check out the Control.ControlCollection class as you can use the documentation here to see how to access the control with its index or its name.  See the Item[Int32] property.

0 Likes
Message 3 of 6

Anonymous
Not applicable

Hi Keith,

Thanks for your reply. I am not sure if we have crossed wires here but...

I am not trying to change the names or properties of the combo boxes on the form, I want the loop to change which combo box it calls in each iteration. So I guess that each reference to a combo box should be a variable whose value changes on each pass...at the moment they are hard coded calls.

i.e

The snippet below shows what should be called on the 1st pass.

 

{

Ez_Combo_Add.EzComboList.setCombo(devlist, comboBox1);
this.comboBox1.SelectedIndex = 1;

Ez_Combo_Add.EzComboList.setCombo(medialist, comboBox2);
this.comboBox2.SelectedIndex = 1;

Ez_Combo_Add.EzComboList.setCombo(stylelist, comboBox3);
this.comboBox3.SelectedIndex = 1;
}

 

Then the 2nd pass would need to be:

 

{

Ez_Combo_Add.EzComboList.setCombo(devlist, comboBox4);
this.comboBox4.SelectedIndex = 1;

Ez_Combo_Add.EzComboList.setCombo(medilist, comboBox5);
this.comboBox5.SelectedIndex = 1;

Ez_Combo_Add.EzComboList.setCombo(stylelist, comboBox6);
this.comboBox6.SelectedIndex = 1;
}

 

etc. up until combobox12 is reached.

 

Note: the above is just an example to show what needs to change in the loop...

If I am missing your point I do apologise, but like I say I am still learning.

 

Thanks again,

 

Steve

0 Likes
Message 4 of 6

Keith.Brown
Advisor
Advisor

I think you misunderstood what I wrote.  If you go to the documentation in the links i provided you can see how to access the combobox by either its name or its index.  Lets assume you want to access it by its name.  If you have 12 comboboxes and are changing 3 each time through the loop then you will need 4 loops and one indexer for the name.

 

So you can create a loop that will index the combobox using a combination of its name and the index variable.  Psuedocode is below as I am doing this off the top of my head.

 

// psuedocode
int index = 1;
loop 4 times;
{
    Ez_Combo_Add.EzComboList.setCombo(devlist, this.controls["comboBox" + Index.ToString]);
    this.controls["comboBox" + Index.ToString].SelectedIndex=1;
    index++
    Ez_Combo_Add.EzComboList.setCombo(medialist, this.controls["comboBox" + Index.ToString]);
    this.controls["comboBox" + Index.ToString].SelectedIndex=1;
    index++
    Ez_Combo_Add.EzComboList.setCombo(stylelist, this.controls["comboBox" + Index.ToString]);
    this.controls["comboBox" + Index.ToString].SelectedIndex=1;
    index++
}

 

 

 

 

0 Likes
Message 5 of 6

Anonymous
Not applicable

Thanks Keith,

I was kinda thinking along the lines you have shown but was struggling a little after this.Controls.

I will try implementing this later.

Thanks again,  Steve.

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi again,

 

I tried to implement your idea like so...casting the control to a combobox for the helper function:

 

//extract from Form_Load event

Ez_Combo_Add.EzComboList.setCombo(devlist, ((ComboBox)this.Controls["comboBox" + index.ToString()]));
((ComboBox)this.Controls["comboBox" + index.ToString()]).SelectedIndex = devlist.IndexOf(name);
index++

 

 

//function to add list to combobox

    namespace Ez_Combo_Add
    {
        public class EzComboList
        {
            static public void setCombo(StringCollection coll, ComboBox box)
            {
                foreach (string item in coll)
                {
                    box.Items.Add(item);
                    }
                }
            }
        }

 

But on execution it produces the same error for both lines: "Object reference not set to an instance of an object"

I am sorry for being such a pain but I am lost at the moment.

Steve

0 Likes