Hi there! Seems you are visiting my site for the first time. Hmm, Why dont you subscribe to RSS or use email subscription service to get all posts at your inbox (No spam). You can also follow me as google friend.
Please carry on reading my blogs, if you like, please give your feedback too. Have a wonderful day! (Dismiss)

Monday, October 13, 2008

.NET Code Construct

Okey... Lets proceed our discussion a bit more.. In my previous topic posted, I have gave you a brief Idea of the whole thing where the technology is facing... Now I will go a bit indepth... and show you how to use the existing technology of .NET to built the most beautiful and fully featured software applications.

To start with VB.NET, I must say, its far far better from the previous version of VB. It is fully Object Oriented Programming Language with all the functionalities of OOPS, so that all the programmers feel better in this language. Also with the full support of .NET Framework, you will have to do only a few codes in this environment.
To work with VB.NET you need the Visual Studio .NET in your machine. Lets start with that one. Go to File->New-Project. Give Location of your application and choose windows Application from the right hand side of the dialog box while choosing the language VB in the left. After you click on open, it should open a new blank form in front of you...

Well, How does everything happened so fast without writing not even a single line of code. Well, Microsoft did it for us already. In case of JAVA you find a Frame class already built in the java.awt package. Here also in case of microsoft's VB, it is just a class already built in by microsoft called Form Class.
Actually, Just similar to JAVA, microsoft has made a hierarchy of classes and stored in a number of namespaces. Namespaces means just a scope that holds classes and other namespaces. The concept is just similar to our so called folder hierarchy... Suppose we think a namespace as a folder and a class a file. So there is drives which we call roots, just similarly here also there is a root namespace called System. Everything that corresponds to system, will be stored within this package. Under this there are other namespaces too.. like Data, Windows etc... Every namespace holds classes or other namespaces. Just similar to the concept that every folder holds other folders or files.
Now coming to our point whenever I open the project, I saw a new blank form in front of me... Its just inherited from a class System.Windows.Forms.Form, so everything defined within the form will come to this form. Well well, thats just a flick of a second, that all the associated properties and functions in the class comes to your place. Isnt it beautiful.
So your form1 is inherited from the already created form with all the listeners of events associated already. Now you can start adding other controls.
Microsoft had created a separate region and made everything within that area... This is Windows Designer Generated Code. Here you will find Sub new() which is nothing but a constructor of your class, so it gets automatically called whenever you run your application, means when you create actual object. The initializecomponent function will initialize the components just before the object comes into work, and all the settings that you have modified with, will be in place properly. Thus whenever you run, everything that you made in this form gets displayed.

Now lets work with some of the controls. In the left hand side you will find a toolbox, where you get all the tools that you require to place in your form. Those are called controls... So whenever you drag any control in your form, a new object of that class is created and it gets displayed over your form.... Is it that much simple... What do you think. ??? Its not.. It looks very simple, but it actually a bit more. I must say, in form class, there is an array called controls array. Now this array is very helpful. In VB.NET everything that you add in this array will be displayed on the form, so within the class form there is something I am sure that draws the entire thing in the form if you have added the same in Controls array.
So whenever you drag one control, say its a button It creates two lines, in general,
Friend withevents button1 as button
Controls.add(button1)

Now withevents keyword is very helpful, in regards to event handling. Whenever we create an object normally, it is not associated with any action listener of the events. Now withevents will automatically add up all the listeners to the control and everything will come under your way....
Otherwise if you create one button without withevents keyword, you need to manually add a listener to the control
Addhandler button1.click, new EventHandler(Addressof button1_click)
that is, we are adding a listener and whenever it occurs, it will call an eventhandler button1_click which is an eventhandler procudure. Otherwise withevents keyword will automatically add up all the events with the button class and call a blank event procedure.

Now lets us look to the solution explorer. You will find a solution explorer, a window, just if you dont see one, go to view and find solution explorer there. After you get it, you will see a hirerchy there also, the first line suggests the name of your solution, means a number of projects can be added up for a single solution. This solution will be written in a file with .SLN extension.
Next, Line is the name of your project. Under this project, the first thing you notice is some references, they are some locations which you can call directly, they are references added automatically, like System.Windows.Forms so to call Form class under this namespace without giving the whole path you can use just Form and get the class name.
After references, there is a file called AssemblyInfo.VB.
Now if you open this file, you will get the metadata associated with your project, mainly class id of your application and many more. The next option is your form, to create a form Microsoft creates two files, one is for code with .VB extension and other is for visual interface with .RESX extension. So if you delete .RESX file from the location, your application looses visual interface. Ok.
The property window helps us to change the default properties of controls, so whenever I select a control, the properties window will change its content dramatically and display all the associated properties associated with that particular control.
Now let us create a button..
In a blank form drag a button, and then double click on the button placed in the form, the code window appears, Write the following

private sub button1_click(Byval sender as System.Object, ByVal e as System.EventArgs)Handles Button1.click
MessageBox.Show("Hello Welcome to the World of VB.NET")
End Sub
[EXPLANATION]
Now if you run your application, and click on the button, it will show a messagebox in front of you.
Now to understand an event procudure, its just like normal procedure, but with some strict rules, Its signature is just similar to what I have shown, otherwise it will not be treated as an event procedure one... So for every event procedure , you must write two arguments, The first one is Object type, now this argument will take the actual object inside your event procedure which have generated the event. So here button1 and sender is the same thing if you are writing within the block.Now what for, just in the very next you have seen handles keyword, its the list of events associated with the event procedure. So now here, one event procedure can handle multiple events. The second argument will take event related information. We will discuss them later on.
Now lets take another textbox in the form and add textbox1.click just after handles button1.click after giving a comma... so that it looks like this

private sub button1_click(Byval sender as System.Object, ByVal e as System.EventArgs)Handles Button1.click,textbox1.click
if typeof(sender) is Button then
MessageBox.Show("Hello You clicked a button")
else if typeof(sender) is TextBox then
MessageBox.Show("Hello You clicked a Textbox")
End Sub

Just in this case you can detect if the procedure is invoked using textbox.click of button1.click. Isnt it look handy.
Now if you look at the messagebox class,there is a shared method show, when called with an argument of string type will print the string in a messagebox...
The return type of show function is DialogResult...
So if I want to trap the result of Messagebox.show we can trap by creating an object of DialogResult
Dim result as DialogResult
result=Messagebox.show("Message","Caption",MessageboxButtons.OKCANCEL)
if result=DialogResult.OK then
Me.Text="OK Clicked"
else
Me.Text="CANCEL Clicked"
end if

Here me represents the Current class, just like this pointer .... of C++ or C#. So the text of the title bar of your form will get changed.

Now Let us create another form, to create a form, right click on the project icon of the solution explorer(Second line of Solution explorer hierarchy) and add->new Item. From the dialog box choose form and click open. A blank for comes in a new tab. Just drag a label here and Write something in its text proerty of properties window.
Nest go to the form1 again, drag another button Let it be button2 and write the code below in the code window

public sub button2_click(ByVal sender as Object, ByVal e as EventArgs) Handles Button2.click
Dim f as new Form2
f.show()
Me.hide()
End Sub

Now whenever you run the application and click on button2, a new form will be displayed in front of you.. So by this way you can navigate from one form to the other. The first line creates a new object for form2 class, next line shows the form, making Form2.Visible=true and Me means Form1.Visible=False. Just to make form2 as startup, you again right click on the project of the solution explorer and go to properties. Change the startup object to form2 and start running.. The form2 will be now your startup form...
Now I think you all can start working with some codes with VB.NET, I will proceed with more discussions in my next posts, so keep watching my posts...
Thanks a lot

No comments: