HELP!!!!!! ComboBox in a "for" loop

[b][red]This message was edited by ray55 at 2006-3-2 19:40:20[/red][/b][hr]
Help! I'm writing a program using Borland Visual C++ version 5.0. I have a large number of ComboBoxes in this program (about 110). I want to use ComboBox in a "for" loop to set the ItemIndex of each ComboBox to the values in a specific array.

Example code:
...
...
for (x=0; x<120;x++)
{
ComboBox[x]->ItemIndex=array[y];
}
...
...

Unfortunately, this won't compile without a host of errors, one being that "ComboBox[x]" is not recognized. Is there any way to do this without the errors? If not, does a later version of Borland C++ Builder allow this?


Comments

  • : Help! I'm writing a program using Borland Visual C++ version 5.0.
    Don't confuse Microsoft Visual C++ with Borland C++ Builder.

    : I have a large number of ComboBoxes in this program (about 110).
    : I want to use ComboBox in a "for" loop to set the ItemIndex of each
    : ComboBox to the values in a specific array.
    :
    : Example code:
    : ...
    : ...
    : for (x=0; x<120;x++)
    : {
    : ComboBox[x]->ItemIndex=array[y];
    : }
    : ...
    : ...
    :
    : Unfortunately, this won't compile without a host of errors, one being that "ComboBox[x]" is not recognized. Is there any way to do this without the errors? If not, does a later version of Borland C++ Builder allow this?


    Of course this won't compile! You have not defined an array of ComboBoxes.

    In Unit1.h after 'public:':
    [code]
    TComboBox* mComboBoxes[110]; //An array of type TComboBox*
    [/code]

    In Unit1.cpp add to the constructor

    [code]
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    mComboBoxes[0] = ComboBox0;
    mComboBoxes[1] = ComboBox1;
    mComboBoxes[2] = ComboBox2;
    mComboBoxes[3] = ComboBox3;
    //Etcetera
    }
    [/code]

    Now you CAN iterate through the ComboBoxes.

    Have fun,
    bilderbikkel

  • [b][red]This message was edited by ray55 at 2006-3-3 7:56:29[/red][/b][hr]
    [b][red]This message was edited by ray55 at 2006-3-3 7:35:9[/red][/b][hr]
    I tried to set up the array and it compiled and executed correctly in the builder, but upon closing the execution window I receive a messge box with this error:

    "Project HIndexII.exe raised exception class EAccess Violation with message 'Access Violation at address 40003AB4....Process stopped..."

    Code:
    ...
    ...
    mComboBoxes[0]=ComboBox0;
    etc.
    ...
    ...
    int initial[]={16,5,13,0,13,5,11,8,14,0,0,0,0,0,0,0,0,0,0,0,1,0, 1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,9,0,
    1,8,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
    0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,0,0,0,1,0,1,0,1,0};
    for (int y=5;y<125;y++)
    {
    mComboBoxes[y]->ItemIndex=initial[y-5];
    }

    Any ideas or solutions?


  • [b][red]This message was edited by bilderbikkel at 2006-3-3 8:8:42[/red][/b][hr]
    You can check it yourself:

    [code]
    //Include assert, see www.codepedia.com/1/CppAssert for more info
    //There you'll see its importance
    #include
    //To get a vector, I assume now that you put the stuff in a std::vector
    //see www.codepedia.com/1/CppVector for more info
    //Or define the size of your arrays somewhere
    #include

    : int initial[]={16,5,13,0,13,5,11,8,14,0,0,0,0,0,0,0,0,0,0,0,1,0, 1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,9,0,
    : 1,8,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,
    : 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
    : 0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
    : 1,0,0,0,1,0,1,0,1,0};
    : for (int y=5;y<125;y++)
    : {
    : //mComboBoxes[y]->ItemIndex=initial[y-5];
    //Check for out of range error
    assert(y>=0 && y < mComboBoxes.size());
    TComboBox * comboBox = mComboBoxes[y];
    assert(comboBox!=0);
    const int index = y - 5;
    //Check for out of range error
    assert(index >= 0 && index < initial.size());
    const int itemIndex = initial[index];
    //Check for item index
    assert(itemIndex < comboBox->Items->Count);
    comboBox->ItemIndex = initial[index];
    : }
    [/code]

    Now you will find out your incorrect assumption!

    See ya,
    bilderbikkel



  • Yeah, found the error. Everything works correctly. Thanks!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories