Correct way to initialize a ListBox during Form Load


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.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??


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.

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
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.

call it from the Page Load event...
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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(?????)
End Sub

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
oop...i're in the designer files...i was talking about page load on form itself...excuse error...
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

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:
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.