अजपा योग क्रिया आणि ध्यान : श्वास, मंत्र, मुद्रा आणि ध्यान यांच्या सहाय्याने मनःशांती, एकाग्रता, चक्र संतुलन आणि कुंडलिनी जागृती. अधिक माहिती आणि आगामी तारखांसाठी येथे जा.


Hello WinForms - An Introduction to Win Forms

What are Win Forms?

In earlier versions of VB and other Visual Studio products there were different 'Forms' engines. So forms developed using VB were different than other languages. With Visual Studio.NET the picture has changed. Now, all the tools supporting NGWS make use of a common forms engine. The forms thus created are called as Win Forms. This leads to many benefits (also refer VS.NET documentation for more details):
  • Common look and feel
  • Added functionality ( Yes! VB forms can now have scrollbars!)
  • Significantly reduces control-container interoperability issues.
  • Win Forms takes full advantage of the security features of the NGWS runtime.
  • Win Forms offers support to Web Services.
  • Win Forms provides rich graphics with the help of GDI+, a new version of the Windows Graphical Device Interface
  • Win Forms offers a rich set of controls
  • Win Forms offers full support for the ADO+ data model.
  • Win Forms offers support for ActiveX controls.
  • Win Forms takes advantage of the NGWS Runtime enhanced licensing model.
  • Win Forms offers a printing framework that enables applications to provide comprehensive reports.

First WinForms application

Now, let us create a simple form which will just display "Hello Win Forms" This is very simple example but illustrates general structure of VB.NET classes for displaying forms.

   Imports System
   Imports System.WinForms

   Namespace Bipin.Samples

       Public Class HelloWinForms
       Inherits System.WinForms.Form

           Shared Sub Main()
               Application.Run(New HelloWinForms())
           End Sub

           Public Sub New()
               MyBase.New
               Me.Text = "Hello Win Forms"
           End Sub

       End Class

   End Namespace

Note the following points :

  • We have imported namespaces called System and Systems.WinForms. The later contains all the controls (like label, button etc) we will be using next.
  • We declared our own namespace called Bipin.Samples
  • The namespace contains a class called HelloWinForms which is inherited from Form class
  • In VB.NET constructors are represented by a method named New(). This method calls constructor of the base class (mybase.New) and sets title of the form to "Hello Win Forms"
  • An instance of the form is created in Shared (which is nothing but Static in C++/Java) method called Main()

Compiling the application

You can compile the application using command line compiler vbc

vbc file_name /r:System.WinForms.dll /r:System.Drawing.dll

Here, file name is the name of source file i.e. xxxx.vb. The switch /r points namespaces [r]eferenced in the application.

Adding Controls to the Form

Now, we will see how to add controls to the form

   Imports System
   Imports System.WinForms
   Imports System.Drawing

   Namespace Bipin.Samples

       Public Class HelloWinForms
       Inherits System.WinForms.Form

			Dim label1 as new label

           Shared Sub Main()
               Application.Run(New HelloWinForms())
           End Sub

           Public Sub New()
               MyBase.New
               Me.Text = "Hello Win Forms"
               label1.text="Hello Win Forms"
               label1.location=new point(100,100)
               me.controls.add(label1)
           End Sub

       End Class

   End Namespace
  

  • We have created an instance of label class called label1
  • The newly created label is added to the form in the constructor using me.add method of the form
  • Then we set some properties like location and text of the control. Note that point class is present in System.Drawing namespace, hence we import it.
  • Also, note that you can set properties of the control at any time - before adding to the form as well as after adding to the form

Event handling

Now, let us proceed further and see how to handle events of the controls.

   Imports System
   Imports System.WinForms
   Imports System.Drawing

   Namespace Bipin.Samples

       Public Class HelloWinForms
       Inherits System.WinForms.Form

		   Dim button1 as new Button

           Shared Sub Main()
               Application.Run(New HelloWinForms())
           End Sub

           Public Sub New()
               MyBase.New
               Me.Text = "Hello WinForms"
               button1.text="Click Me"
               button1.location=new point(100,100)
               button1.addonclick(addressof button_click)
               me.controls.add(button1)
           End Sub

           public sub button_click
           (sender as object,evt as eventargs)
	           	Messagebox.show("Hello Win Forms")
           end sub

       End Class

   End Namespace
  

This example is similar to previous one but uses a command button. When user clicks on the button a message box will be displayed saying "Hello Win Forms" Here is how we handled click event of the button :

  • To indicate that you want to handle certain event you must register a sub or function with that event (Java people will find themselves at home!)
  • This is done using AddOnxxxxx (source as object, evt as eventargs) syntax where xxxxx represents the event name (Click in our case)
  • This method also requires address of the function/sub which handles the event (button_click in our case)
  • The event handling must match syntax shown i.e first argument of type object and second of type eventargs. In some cases (like mouse events) extra information is passed to the event handler via this second argument
  • Note that now you can use same function to handle events from multiple controls
  • We have displayed message box using NGWS Messagebox.show() method. If you want to use old VB methods/functions you need to import Microsoft.VisualBasic in your application

I hope you must have got overall idea about win forms. The next articles of this series will cover techniques like creating menus and using other controls. So, visit soon!


Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 04 February 2001