# Colour gradient menuitem

**URL:** <https://forum.xojo.com/t/colour-gradient-menuitem/37479>\
**Category:** General\
**Created:** [May 5, 2017, 6:48pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479 "2017-05-05T18:48:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lennox\_Jacob1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Lennox\_Jacob1](https://forum.xojo.com/u/Lennox_Jacob1)\
**Post date:** [May 5, 2017, 6:48pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479/1 "2017-05-05T18:48:54Z")

</div>

Hello,

I have a menu with 12 menuitem - “”, “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”

So when I select a menuitem from 1 to 10 I would like the appropriate shade for the Textfield

```
  [code] Dim MyColor() As Color
  dim g as Graphics
  dim startcolor as color
  dim endcolor as color
  
  startcolor = &cFCBAB9
  endcolor = &cFF0000
  
  Dim xx As Integer
  For xx = 1 to 10
    MyColor(xx) = rgb( endcolor.red - (endcolor.red-startcolor.red) *xx/10, _
    endcolor.green - (endcolor.green-startcolor.green) *xx/10, _
    endcolor.blue - (endcolor.blue-startcolor.blue) *xx/10)
  next
  
  
  If hitItem.Text = "" then
    Me.BackColor = &cFFD5A5 'Amber
  elseIf hitItem.Text = "" then
    Me.BackColor = &c69CC49 'Green
  else
    Me.BackColor = MyColor(Val(hitItem.Text))
  end if
  
end if[/code]

```

I am getting “An exception of class OutOfBoundsException was not handled. The application must shut down.”

How can I fix that?

Thanks.

lennox

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [May 5, 2017, 7:00pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479/2 "2017-05-05T19:00:07Z")

</div>

you are doing it wrong  
you cannot say

```auto
MyColor(xx) = rgb( endcolor.red - (endcolor.red-startcolor.red) *xx/10, _
```

because mycolor(xx) does not exist!  
the correct way is

```auto
MyColor.append rgb( endcolor.red - (endcolor.red-startcolor.red) *xx/10, _
```

but realize they will be numbered from ZERO to NINE, not ONE to TEN

either that or

```auto
DIM myCOLOR(10) as color
```

plus this is redundant

```auto
  If hitItem.Text = "" then
        Me.BackColor = &cFFD5A5 'Amber
      elseIf hitItem.Text = "" then
        Me.BackColor = &c69CC49 'Green
```

you are checking for “” TWICE and expecting different results

also may myCOLOR a global array, and populate it ONCE in a module, not every time you call this routine

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Lennox\_Jacob1](https://forum.xojo.com/u/Lennox_Jacob1)\
**Post date:** [May 5, 2017, 7:12pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479/3 "2017-05-05T19:12:50Z")

</div>

Thanks Dave,  
Great.  
Lennox

---

<div class="post-metadata">

**Author:** ![Lennox\_Jacob1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Lennox\_Jacob1](https://forum.xojo.com/u/Lennox_Jacob1)\
**Post date:** [May 5, 2017, 7:20pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479/4 "2017-05-05T19:20:44Z")

</div>

Thanks Dave,

If hitItem.Text = “” then  
Me.BackColor = &cFFD5A5 'Amber  
elseIf hitItem.Text = “” then  
Me.BackColor = &c69CC49 'Green  
That was an oversight

This is the corrected code…

[code] Dim MyColor() As Color  
dim g as Graphics  
dim startcolor as color  
dim endcolor as color

```
startcolor = &cFF0000
endcolor = &cFCBAB9

Dim xx As Integer
For xx = 1 to 10
  MyColor.append rgb( endcolor.red - (endcolor.red-startcolor.red) *xx/10, endcolor.green - (endcolor.green-startcolor.green) *xx/10, endcolor.blue - (endcolor.blue-startcolor.blue) *xx/10)
next

// Now display the menu
dim hitItem as MenuItem
hitItem = base.PopUp
if hitItem <> nil then
  Me.Text = hitItem.Text
  
  If hitItem.Text = "" then
    Me.BackColor = &cFFD5A5 'Amber
  elseIf hitItem.Text = "0" then
    Me.BackColor = &c69CC49 'Green
  else
    Me.BackColor = MyColor(Val(hitItem.Text) - 1)
  end if

```

[/code]

Lennox

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:45pm UTC](https://forum.xojo.com/t/colour-gradient-menuitem/37479/5 "2020-10-29T18:45:11Z")

</div>


