Home > Blockchain >  ComboBox Items from a for loop method
ComboBox Items from a for loop method

Time:08-18

I. MY METHOD

 private void itemLoop(int start, int finish)
            {
                finish  = 1;
                System.Object[] itemobj = new System.Object[finish];
                for (int i = start; i <= finish; i  )
                {
                    itemobj[i] = i;
                }
            }

II. I can't call the method. How can I call this method to create a range of items in ComboBox using loop?

this.cbMonth.Items.AddRange(itemLoop(1, 12));

CodePudding user response:

there is no need to implement a custom range method

this.cbMonth.Items.AddRange(Enumerable.Range(1,12));

CodePudding user response:

The best way is the one that Fubo has explained.

this.cbMonth.Items.AddRange(Enumerable.Range(1,12));

But if you have to use your method, you have to return the array:

            private System.Object[] ItemLoop(int start, int finish)
            {
                finish  = 1;
                System.Object[] itemobj = new System.Object[finish];
                for (int i = start; i <= finish; i  )
                {
                    itemobj[i] = i;
                }
                return itemobj;
            }

At the end you can call this:

this.cbMonth.Items.AddRange(ItemLoop(1, 12));
  •  Tags:  
  • c#
  • Related