1

In VB.NET, whenever I try to call a form from my main sub, I have to define a dim with the datatype as the formname itself.

Doesn't that mean that every window form is a class of its own in VB.NET?

I thought that all windows forms boil down to one class.

Can someone explain to me? Many thanks.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51

2 Answers2

1

In VB.NET a Form is a class. All your forms inherit from the base class Form. this mean that the form is a class on its own. to use your form you need to create an instance (object) of your class with dim. for example

dim formInstance as MyOwnForm

This mean they all have Form as their base Type but are a new class type, in this example MyOwnForm.

once you have your instance you can show you form and do any other operation on it.

user2660616
  • 95
  • 1
  • 8
  • Thank you. That confirmed my suspicion. How do I stop the form that hiding? How do you pause the the main sub and tell to wait for the form and close? Many Thanks @User2660616 – Amen Jlili Dec 22 '14 at 09:06
  • On the Main sub at some point you should use `Application.Run(mainForm)` passing your newly created form instance. That will begin the main message loop for the application which will stay until the main window closes. Have a look at the default template for Windows Forms applications. – Alejandro Dec 22 '14 at 09:14
  • you can use the Show() to show the form and Hide() to hide the Form. To close the form use close() or dispose(). to show the form as a dialog box, which the method is ShowDialog(). this let your method wait. For the main method i'm not sure in vb.net Visual studio gives you some options here. – user2660616 Dec 22 '14 at 09:17
  • Not true - you don't have to (explicitly) create an instance of a form to use it in VB.NET. – Matt Wilko Dec 22 '14 at 15:54
0

In .NET every Form is a class that inherits from the Windows.System.Forms.Form class which in turn inherits from the Object class

But in VB.NET you don't have to create an instance of a form to use it. This is something that was added in VS2005 to make it more backwards compatible with VB6 where Forms weren't classes.

See this answer for more information on this: Why is there a default instance of every form in VB.Net but not in C#?

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143