How do I address controls created at runtime?

I am creating my own User Control that replicates some of the features of a listbox (I’m doing this because I need a fully transparent listbox). I have (# of) Rows and Columns as public properties of my control that are set at design time. And I have one instance of a Desktoplabel, lbl(0), created and in place on top of a Canvas that covers the entire control. I created the instances of lbl() using …

For r As Integer = 0 To rows -1
  For c As Integer = 0 To columns -1
    If r <> 0 Or c<>0 Then 'don't need to redo first cell (0,0)
      Var newLabel As DesktopLabel = New lbl
      newLabel.Text = r.tostring + "," + c.ToString
    End If
  Next c
Next r

This works fine. But I need to resize the labels when the control is resized and I cannot figure out how they are addressed in subsequent events or methods within my custom control. For example, in my resize event I have tried;

lbl(i).left= ...

… but this fails due to a nil exception. lbl(i) does not exist. What is the right way to address multiple instances of a control created at runtime?


You know, you can use the DesktopListbox.PaintCellBackground-Event to make the Listbox fully transparent (read, remove the Listbox’ background)?

Function PaintCellBackground(g As Graphics, row As Integer, column As Integer) As Boolean
  // Clears the Listbox' background.
  g.ClearRectangle(0, 0, g.Width, g.Height)
End Function

Hi Martin. Didn’t know that. And that works. Now how do I explain the half of a day I wasted trying to re-invent the wheel.

Thank you!


Might have spoken too soon. My listbox is sitting on top of a Canvas where I have drawn a gradient. When I use g.clearRectangle on the cellpainevent, it seems to clear the underlying Canvas as well. See image.

What I’m trying to do is have a gradient canvas image show through a grid of text. Short of computing partial gradients for each cell of the listbox, I can’t figure out how to do this.


return TRUE from the PaintCellBackground to stop the default paint activity