tags:

views:

28

answers:

1

I build a component class it extends a combo box and generates some random numbers. But when I drag and drop my component from the toolbox to a form it auto generates Me.Randtest1.Items.AddRange(...) in the Designer which uses static numbers. The idea was to have different numbers each time and not the same.

Imports System.Windows.Forms

Public Class randtest
  Inherits ComboBox

  Public Sub New()
    setDefaultText()
    fillComboBox()
  End Sub

  Private Sub setDefaultText()
    Text = Rnd(10)
  End Sub

  Private Sub fillComboBox()

    For count = 0 To 5
      Items.Add(Rnd(10))
    Next

  End Sub
End Class

Thanks

+1  A: 

Add this to the top of the constructor:

If DesignMode Then Exit Sub

That way, it won't generate anything at design time, only at run time.

Just out of curiosity, is this so that the user can pick a random number, or just programatically?

Mike Caron
+1 It also might be a good idea to `Randomize` in there somewhere. Maybe in the `Sub New` or even a static constructer `Shared Sub New` http://stackoverflow.com/questions/1380990/using-randomize-before-rnd-in-vb-net
MarkJ
Randomize doesn't really belong in a control. It should go in the form or the Main() function instead.
Mike Caron