• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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
0
John500
Asked:
John500
  • 3
  • 3
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now