Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

HOW TO: populate indvidual controls with data from array

I have a groupbox with 10 (0-9) command buttons (btn0, btn1...btn9) in it.

I have an array of data that may have more than 10 items in it, but for now lets assume that the array has 10 or less items.  I would like to be able to iterate through the controls based on their placement or tab index and assign properties from the array to each command button.  Below is the code I have so far.  Can someone please help??

private void LoadPOSTransationTypes()
{
    try
    {
        // declare array and fill it with data from DB
        ItemX[] itms = Utilities.GetItemXs();
        // if there items returned continue
        if (itms.Length > 0)
        {
            // iterate through the controls in the groupbox
            foreach (Control control in this.groupBox.Controls)
            {
                    // if the control is a button
                    if (control is Button)
                    {
                        // set the properties of the button with the item in the array here
                    }
            }
        }
        else
        {
             // throw some type of exception, data not found            
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
     }
}

0
wenyonk
Asked:
wenyonk
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Assuming they all start with "btn", followed by a number, then a simplistic approach would be to parse the number out of the controls name and then use that as in Index to grab data from your array.

Something like...

    int index = int.Parse(control.Name.Substring(3));
    control.Text = itms[index].someThing;
0
 
Seraph_78Commented:
You could use the Tag property of the button to assign it the appropriate array index that contains the properties for that button.  That way you can name all of the buttons whatever you like.

So set btn0.Tag = 0, btn1.Tag = 1, etc.

Then in your if statement all you have to do is:

If(control is Button)
{
   control.SomeProperty = itms[control.Tag].SomeProperty;
}


0
 
TheAvengerCommented:
Or you can use the Control.GetNextControl method to go over the controls in the tab order. You only need to know the first control. More info: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol.aspx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vo1dCommented:
how is your relation between buttonX and itemX?
always 1 to 1 or could it be 1 to n ?
if you got your db data, does it come in the correct order like your button order?
or does your db data have a unique id?
if so, you will only have to save that unique id to your buttons tag object and if your relation between buttonX and itemX is 1 to n, i would recommend using a hashtable where the key is your unique id and your values are the items for that key.
0
 
wenyonkAuthor Commented:
Essentially:

I fill the array from the DB in the desired display order.  Item1 should be assigned to button1, item2 to button2, and so on.  if there are 5 items returned to from the DB to the array I want to display data in the first 5 buttons and disable the remaining 5 buttons.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Have you tried any of the suggestions yet?

(inside you loop)
Something like...

    int index = int.Parse(control.Name.Substring(3));
    if (index < itms.Length)
    {
        control.Enabled = true;
        control.Text = itms[index].someThing;
    }
    else
    {
        control.Enabled = false;
    }
0
 
wenyonkAuthor Commented:
Additional Questions Please see my next post  
0
 
wenyonkAuthor Commented:
Title: HOW TO: populate indvidual controls with data from array  Part2
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now