Correct way to initialize a ListBox during Form Load

Greetings,

I have a Form and I want to load a ListBox with values as the Form loads.  I put my own function at the end of the Form's InitializeComponent() like this:

Private Sub InitializeComponent()     ' Form initialization
     ...
     ...
     Me.PerformLayout()
     Me.LoadSiteLocations()     'My vb file code

End Sub

However, if I try to view the Form in the designer it gives me the error seen in the picture below.

If I run the program it runs fine, I just can't view the Form in the designer.  If I remove the call to LoadSiteLocations() I can view the designer but obviously the ListBox won't get filled.

Just how should I be calling this method??

Thanks


designer-error.JPG
John500Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WilliamCommented:
Do you need this method to run during design time? in the ide

Please read this for some design time fixes and how too's

http://bytes.com/forum/thread386210.html
0
John500Author Commented:
>> Do you need this method to run during design time

No, I just want the ListBox to be filled by the time the Form is displayed.  I don't the user to have to click a button to load the ListBox - I wanted it filled at the start.

0
silemoneCommented:
call it from the Page Load event...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

John500Author Commented:
How does that work.  The 'MyForm.Designer.vb' code doesn't contain the event.  Neither does the MyForm.vb code have it.

Should I create it and put it in the MyForm.vb file?  What does the Page Load event code look like exactly:

Private Sub Page_Load(?????)
      LoadSiteLocations()
End Sub

Thanks!
0
WilliamCommented:
go to the form designer with the problematic code commented out
and from the designer go to the foms property window
click on the lightning bolt to see the lists of event and double click the load
this will take to you back to your code page with the event already registerd.
place the code there
0
silemoneCommented:
oop...i apologize...you're in the designer files...i was talking about page load on form itself...excuse error...
0
silemoneCommented:
so from the form itself, this is where you would do this by pageload event...

Then you shouldn't get any errors...
and yes, the code will look similar to what you posted....

except the parameter will be:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   <-- aspnet form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   <--winform

what's most important regardless of name is Handles MyBase.Load is attached which lets the page know that this will be the load event
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John500Author Commented:
That was the ticket, thanks!

How about taking a look at this question when you get a chance:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23918782.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.