Correct Way to Open a Windows Form in VB.NET

Submitted by: 
Visitors have accessed this post 4890 times.

Few months ago I encounter a problem with my windows form bound using a DataSet. When I open the form the first time, it seems that everything is fine. But when I open it again the second time, the fields are become blank. This happens when you trigger the “PerformClick” of “BindingNavigator” under the Form Load event.

This is not actually the common mistake that I encounter. But maybe, you encounter this problem also if you are used with VB 6.0 before.

Here’s the code to open the windows form the correct way:

  1. Dim SalesReturn As New frmSalesReturn
  3. With SalesReturn
  4. .InvoiceID = InvoiceID
  6. .ShowDialog()
  7. End With

The above code is just one of the examples on my POS in VB.NET version. In the line:

  1. Dim SalesReturn As New frmSalesReturn

You have to declare the form as new instance. Because in VB.NET you cannot directly refer to a form like this:

  1. With frmSalesReturn
  2. ‘your statement here
  3. End With

So this means that every time you’d like to open your form, you have to use the “New” keyword to free it from memory when you close the form.

In VB 6.0 we can set the form to “Nothing” in the Form_Unload event. But this technique is not supported now in VB.NET.

I just want to share this to you because I mistakenly refer the wrong variable a few days ago in my code. Instead of SalesReturn I declare the frmSalesReturn in the “With” statement making the variable SalesReturn useless.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.