Thursday, July 4, 2013

Code for Food Menu - VB.Net

Situation: A Menu Form with the following controls: Checkbox, Label
Checkbox - For Menu - When an item is checked, its value should show and add up to the label total below.
But Sauce should be checked first in order to enable the the other Checkboxes.


Default Interface: 
  • Open MS Visual Studio.
  • Open a New project in Visual Basic.
  • Locate ToolBox by pressing Ctrl+Alt+X.
  • Double click on Group Box to include it in the form. Move the Group Box to the center of the form.
  • Put 7 Checkboxes inside the Group Box.
  • Set the 7 Checkboxes to the following:
    • By default the name of Checkboxes are Checkbox 1, Checkbox 2 and so on.
    • We will change its property to: (Properties to change: Name Property, Text Property and Enabled Property)
Checkbox 1 - Name: chkHam Text: Ham P25.00 Enabled: False
Checkbox 2 - Name: chkCheese Text: Cheese P10.00 Enabled: False
Checkbox 3 - Name: chkPineapple Text: Pineapple P15.00 Enabled: False
Checkbox 4 - Name: chkOlives Text: Olives P20.00 Enabled: False
Checkbox 5 - Name:chkPepperoni Text: Pepperoni P25.00 Enabled: False
Checkbox 6 - Name: chkBacon Text: Bacon P30.00 Enabled: False
Checkbox 7 - Name: chkSauce Text: Sauce P10.00 Enabled: False
 
  • Put 2 Labels on the form
  • Change its properties to the following:

Label 1 - Name: lbltotal Text: TOTAL
Label 2 - Name: lbltotalamount Text: 0
 


 

Code:


'Code by: Emeliza Ducos
' July 4, 2013

 
Public Class Form1
Dim hamAmt As Integer
Dim cheeseAmt As Integer
Dim pineappleAmt As Integer
Dim olivesAmt As Integer
Dim pepperoniAmt As Integer
Dim baconAmt As Integer
Dim sauceAmt As Integer
Dim total As Integer
 
 
'Double Click chkSauce and enter the following code
Private Sub chkSauce_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSauce.CheckedChanged
'Exp: If chkSauce is not checked the other 6 checkboxes is disabled. And "Select Sauce First" is displayed on the lbltotalamount.
If chkSauce.CheckState = 0 Then
sauceAmt = 10
total = total - sauceAmt
chkHam.Enabled = False
chkCheese.Enabled = False
chkPineapple.Enabled = False
chkOlives.Enabled = False
chkPepperoni.Enabled = False
chkBacon.Enabled = False
lbltotalamount.Text = "Select Sauce First"
'Exp: If chkSauce is checked the other 6 checkboxes will be enabled. And amount of sauce is displayed on the lbltotalamount.
Else
chkHam.Enabled = True
chkCheese.Enabled = True
chkPineapple.Enabled = True
chkOlives.Enabled = True
chkPepperoni.Enabled = True
chkBacon.Enabled = True
sauceAmt = 10
total = total + sauceAmt
lbltotalamount.Text = total
End If
End Sub
 
'Double Click chkHam and enter the following code
Private Sub chkHam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHam.CheckedChanged
'Exp: If chkHam is checked, the amount of Ham PLUS amount of Sauce PLUS other amount of any checkbox that has the value of CheckState = 1 (Checked) is displayed on the lbltotalamount. The amount is collected through the Integer total (total = total + hamAmt)
If chkHam.CheckState = 1 Then
hamAmt = 25
total = total + hamAmt
lbltotalamount.Text = total
'Exp: If we unchecked chkHam, the value of ham is deducted from the Integer total.
Else
hamAmt = 25
total = total - hamAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkCheese and enter the following code
Private Sub chkCheese_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheese.CheckedChanged
If chkCheese.CheckState = 1 Then
cheeseAmt = 10
total = total + cheeseAmt
lbltotalamount.Text = total
Else
cheeseAmt = 10
total = total - cheeseAmt
lbltotalamount.Text = total
End If
End Sub
 
'Double Click chkPineapple and enter the following code 
Private Sub chkPineapple_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPineapple.CheckedChanged
If chkPineapple.CheckState = 1 Then
pineappleAmt = 15
total = total + pineappleAmt
lbltotalamount.Text = total
Else
pineappleAmt = 15
total = total - pineappleAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkOlives and enter the following code
Private Sub chkOlives_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOlives.CheckedChanged
If chkOlives.CheckState = 1 Then
olivesAmt = 20
total = total + olivesAmt
lbltotalamount.Text = total
Else
olivesAmt = 20
total = total - olivesAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkPepperoni and enter the following code
Private Sub chkPepperoni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPepperoni.CheckedChanged
If chkPepperoni.CheckState = 1 Then
pepperoniAmt = 25
total = total + pepperoniAmt
lbltotalamount.Text = total
Else
pepperoniAmt = 25
total = total - pepperoniAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkBacon and enter the following code
Private Sub chkBacon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBacon.CheckedChanged
If chkBacon.CheckState = 1 Then
baconAmt = 30
total = total + baconAmt
lbltotalamount.Text = total
Else
baconAmt = 30
total = total - baconAmt
lbltotalamount.Text = total
End If
End Sub
End Class
 
Tell me what you think?
Ask some questions / give feedback on what to correct / suggestions to simplify or improve the program.  
You can ask for the VB.Net file for this program. Email me @ ducos.emeliza@live.com
 

Friday, June 28, 2013

Getting Started in VB.Net

We can create VB.Net Programs through Microsoft Visual Studio for Windows and Mono for other operating systems. We can actually compile VB.Net programs online. You can try it here. We have 4 versions of Visual Studio Software. Visual Studio 2008, 2010, 2012 and 2013.

Visual Studio 2008 - Not recommended for Download
Download Visual Studio 2010 here.
Download Visual Studio 2012 here.
Download Visual Studio 2013 here.

You need to sign up first to download the application.

After installing the Microsoft Visual Studio. I can now show you how to create a VB.Net project.
  1. Open MS Visual Studio (I'm using Visual Studio 2010)
  2. Click New Project or key in Ctrl+Shift+N on your keyboard
  3. Find Visual Basic on Intalled Templates tab on the left-side of the pop-up window.
  4. Click Windows Forms Application
  5. Rename the default filename to My First Program in VB.Net (A new folder will be created on the File directory with the same name as your filename. This will hold all the files that is associated with your project. 
  6. Keep the location same as default.
  7. Click OK.
  8. Now a form be seen on the screen. The default name is Form1. You can see its properties by Right-Click on the mouse and click Properties.
  9. You can rename the form. Find the Name Property and rename it to MyForm1. Find Text Property and enter My First Form. 
  10. Try running this program by pressing F5 or click debug on the menu bar. 
  11. You can see that there are 2 different views. The design environment and debug environment.
  12. Design environment is where we add controls (label, textbox, button, etc. ) to the form and code of course. Debug is where we test our program.
  13. To add controls (label, textbox, button etc.) press Ctrl+Alt+X or click View on the menu bar and click ToolBox. 
  14. Under ToolBox, we'll see set of controls. We're going to create a simple form with a label, textbox and button.
  15. Double click label on the ToolBox. Place it on the left side of the form and Right-click and click Properties and locate Name Property and rename it to lblName, set Text Property to Name:
  16. Add textbox to your form. Double click or single click and click on the form and click and drag the textbox next to label. Change its Name property to txtboxName.
  17. Last Add Button to your form. Change the Name property to btnOk. Change Text Property to OK. 

  18. Double Click on the Button. Write the following code where the cursor goes when you click on the button control.
        Dim name As String
        name = txtboxName.Text
        MsgBox("Hello " & name)




PRESS F5 and you should see this. Enter your name to the textbox.


That's it! You've just created your First VB.Net Form

Please email me @ lizducos17@gmail.com or leave a comment below.

Wednesday, June 26, 2013

Introduction to VB.Net


Visual Basic.Net


VB.Net is a simple, modern, object-oriented computer programming language developed by Microsoft to combine the power of the .NET Framework and the common language runtime with the productivity benefits that are the hallmark of Visual Basic.

Visual Basic .NET is one of the two flagship languages (with C#) for the .NET framework from Microsoft. Despite being called Visual Basic, it is actually not backwards-compatible with VB6, and any code written in the old version will not compile under VB.NET.

As a language, Visual Basic.NET has the following traits:

Object-Oriented


As with all .NET languages, VB.NET includes full-blown support for object-oriented concepts, including simple inheritance. Everything in VB.NET is an object, including all of the primitives (Short, Integer, Long, String, Boolean, etc.) as well as types, events, and even assemblies. Everything inherits from the Object base class.

Event-Driven


All previous versions of Visual Basic were event-driven, but this feature is heavily enhanced under the .NET framework. Events are no longer recognized because they use a certain naming convention (ObjectName_EventName), but now are declared with a Handles ObjectName.EventName clause. Event handlers can also be declared at runtime using the AddHandler command.

.NET Framework


As the name implies, VB.NET runs on top of Microsoft's .NET framework, meaning the language has full access to all of the supporting classes in the framework. It's also possible to run VB.NET programs on top of Mono, the open-source alternative to .NET, not only under Windows, but even Linux or Mac OSX.

HISTORY


Visual Basic was initially introduced in 1991 as the first programming language that directly supported programmable graphical user interfaces using language-supplied objects. From that time until 2002, there were five other versions released, each version having features that increased the power of the language. In 2001, Microsoft released the .NET (pronounced “dot net”) platform. Visual Basic .NET, or VB.NET, is an upgrade to the last version of VB (version 6.0) that conforms to the .NET platform. As you will see in subsequent chapters, the changes in VB.NET allow programmers to write Web or desktop applications within the same language. In addition, VB.NET is fully object-oriented as opposed to prior versions that had many, but not all, of the elements of an object-oriented language. This book is based on VB.NET. In the balance of the book we will sometimes refer to Visual Basic as VB, omitting .NET.

 

The following reasons make VB.Net a widely used professional language:

·         Modern, general purpose.
·         Object oriented.
·         Component oriented.
·         Easy to learn.
·         Structured language.
·         It produces efficient programs.
·         It can be compiled on a variety of computer platforms.
·         Part of .Net Framework.

Strong Programming Features VB.Net


VB.Net has numerous strong programming features that make it endearing to multitude of programmers worldwide. Let us mention some of these features:

·         Boolean Conditions
·         Automatic Garbage Collection
·         Standard Library
·         Assembly Versioning
·         Properties and Events
·         Delegates and Events Management
·         Easy to use Generics
·         Indexers
·         Conditional Compilation
·         Simple Multithreading