Friday, February 11, 2011

Add a button to a page programmatically

Add a button to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create new button
  
     Dim btnNew As New Button
  
     'set properties
  
     btnNew.Text = "CLICK ME"
  
     btnNew.Top = 45
  
     btnNew.Left = 175
  
     'Add the button to the form
  
     Me.Controls.Add(btnNew)
  
   End Sub
  
 End Class  

Add a textbox to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim txtNew As TextBox = Nothing
  
     'set textbox properties
  
     txtNew = New Windows.Forms.TextBox
  
     txtNew.Name = "NewText"
  
     txtNew.Location = New System.Drawing.Point(6, 10)
  
     txtNew.Size = New System.Drawing.Size(250, 50)
  
     txtNew.TabIndex = 0
  
     'add text box to form
  
     Me.Controls.Add(txtNew)
  
   End Sub
  
 End Class  

Add a label to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim lblNew As New Label
  
     'set textbox properties
  
     lblNew.SetBounds(10, 50, 100, 25)
  
     lblNew.Text = "I... Am a Label"
  
     'add text box to form
  
     Me.Controls.Add(lblNew)
  
   End Sub
  
 End Class  

Add an item list to a page programmatically
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create item
  
     Dim lstAddress As ListBox
  
     lstAddress = New System.Windows.Forms.ListBox()
  
     'set list box properties
  
     lstAddress.IntegralHeight = False
  
     lstAddress.Location = New Point(20, 85)
  
     lstAddress.Name = "Adresses"
  
     lstAddress.Size = New System.Drawing.Size(250, 250)
  
     lstAddress.TabIndex = 0
  
     'add list box to form
  
     Me.Controls.Add(lstAddress)
  
   End Sub
  
 End Class  

Wednesday, February 9, 2011

Practice #2- Conditionals

1.)Mr. Spacely has been rethinking the company's shipping schemes. As an employee for Spacely's Sprockets, Spacely has asked you to write a quick program that will be used to determine shipping costs.

-A total purchase of items totaling under $50, we charge $5 shipping.
-A total purchase of items totaling $50 and over, there is no shipping charge.

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim dblPurchase, dblShipping, dblTotal As Double
  
     'Input
  
     dblPurchase = CDbl(txtPurchase.Text)
  
     dblShipping = 6
  
     'Calculate
  
     If dblPurchase >= 50 Then
  
       dblTotal = dblPurchase
  
     Else
  
       dblTotal = (dblPurchase + dblShipping)
  
     End If
  
     'Display Total
  
     MessageBox.Show = ("Your total is " & FormatCurrency(dblTotal) & " .")
  
   End Sub
  
 End Class  

2.)You are looking for something to control your heat in your apartment and you discover there is NOT an app for that. It's about time that someone created one. You decide that you are the one to do it. Here's what you want to do.

-You want to turn the heat on when the temp has dropped below 72
-You also want to turn the AC on when the temp gets above 76

Your app should display in a message box if the heat is on, the AC is on, or if the system is idle. Plug in different temps for the room in a variable to see what the thermostat will do with it.
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decTemp As Decimal
  
     'Get Input
  
     decTemp = CDec(txtTemperature.Text)
  
     'Calculate
  
     If decTemp < 72 Then
  
       MessageBox.Show("The heat is on.")
  
     ElseIf decTemp >= 72 And decTemp <= 76 Then
  
       MessageBox.Show("The system is idle")
  
     ElseIf decTemp > 76 Then
  
       MessageBox.Show("The A/C is on")
  
     End If
  
   End Sub
  
 End Class  

3.)You are working on a clothing website where people can buy kids socks. It's really hard for the customers to know what size they should buy for what age. It would be a good idea for the customer to have a tool to input their child's age and have the website suggest a size for them. Write a tool where you can set the age as a variable and have it suggest on of the sizes below:

a.0-2 yrs -XS
b.3-4 yrs -S
c.5-8 yrs -M
d.9-12 yrs -L
e.13+ yrs -XL
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim intAge As Integer
  
     'Get Input
  
     intAge = CInt(txtAge.Text)
  
     'Calculate
  
     If intAge <= 2 Then
  
       MessageBox.Show("Your child's size is X-Small")
  
     ElseIf intAge >= 3 And intAge <= 4 Then
  
       MessageBox.Show("Your child's size is Small")
  
     ElseIf intAge >= 5 And intAge <= 8 Then
  
       MessageBox.Show("Your child's size is Medium")
  
     ElseIf intAge >= 9 And intAge <= 12 Then
  
       MessageBox.Show("Your child's size is Large")
  
     ElseIf intAge >= 13 Then
  
       MessageBox.Show("Your child's size is X-Large")
  
     End If
  
   End Sub
  
 End Class  

Case Statement

■A case statement
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim strSnack As String
  
     Dim strDinner As String
  
     strSnack = TextBox1.Text
  
     Select Case strSnack
  
       Case "Had a Snack"
  
         strDinner = "Will not have"
  
       Case "Had a little"
  
         strDinner = "Will have a little"
  
       Case Else
  
         strDinner = "Will have"
  
     End Select
  
     MessageBox.Show(strDinner & " Dinner.")
  
   End Sub
  
 End Class  

If Statements

■An if statement that makes one decision
 Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
  
    'Declare variables    
  
    Dim dedGrade1, decGrade2, decBetterGrade As Decimal    
  
    'Convert text box to double    
  
    decGrade1 = CDec(txtSecondGrade.Text)    
  
    decGrade2 = CDec(txtSecondGrade.Text)    
  
    'Find results    
  
    If decGrade1 > decGrade2 Then     
  
      decBetterGrade = decGrade1    
  
    Else     
  
      decBetterGrade = decGrade2    
  
    End If    
  
    'Display results    
  
    txtBestGrade.Text = "Your best grade is " & decBetterGrade & "."   
  
   End Sub  
  
 End Class  

■An if statement where it's either or
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGPA As Decimal
  
     If decGPA < 3.5 Then
  
       MessageBox.Show("You did not make the dean's list")
  
     Else
  
       MessageBox.Show("You made the dean's list")
  
     End If
  
   End Sub
  
 End Class  

■An if statement where many decisions are made
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGrade As Decimal
  
     If decGrade >= 91 Then
  
       MessageBox.Show("You get an A")
  
     ElseIf decGrade >= 82 Then
  
       MessageBox.Show("You get a B")
  
     ElseIf dedGrade >= 70 Then
  
       MessageBox.Show("You get a C")
  
     ElseIf decGrade >= 60 Then
  
       MessageBox.Show("You get a D")
  
     Else
  
       MessageBox.Show("You get an F")
  
     End If
  
   End Sub
  
 End Class  

Monday, February 7, 2011

Practice #1 -Values and Formulas

1.)Morgan loves collecting small stuffed animals. She has 6 cows and 7 sheep. How many animals does she have in her collection?

  Public Class Form1   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    'Declare variables   
  
 Dim intCows, intSheep, intTotal As Integer    
  
 'Set values    
  
 intCows = 6   
  
 intSheep = 7   
  
 intTotal = C + S   
  
 'Display results    
  
 MessageBox.Show("Morgan has " & intTotal & " stuffed animals.")   
  
 End Sub  
  
 End Class  

2.)Diane bought 7 mega burgers. Each mega burger cost $4. How many dollars did she spend on the mega burgers?

 Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
  
  'Declare variables    
  
 Dim intMegaBurgers As Integer
  
 Dim decCost, decTotalPrice As Decimal
  
 'Set values    
  
 intMegaBurgers = 7    
  
 decCost = 4    
  
 decTotalPrice = intMegaBurgers * decCost   
  
 'Display results   
  
 MessageBox.Show("The Mega Burgers cost " & FormatCurrency(decTotalPrice))   
  
 End Sub 
  
 End Class  

3.)Ole has 15 apples and 12 oranges. How many pieces of fruit does he have?
-Hard code the initial facts
  Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables   
  
 Dim intApples, intOranges, intFruit As Integer    
  
 'Set values    
  
 intApples = 15    
  
 intOranges = 12    
  
 intFruit = intApples + intOranges    
  
 'Display results    
  
 MessageBox.Show("Ole has " & intfFruit & " pieces of fruit.")   
  
 End Sub  
  
 End Class  

4.)There are 33 horses in a field. 15 horses go into the barn. Then 7 of them come back out. How many horses are standing in the field?
-Hard code the initial values
  Public Class Form1   
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intField, intBarn, intOut, intTotalHorses As Integer    
  
 'Set values    
  
 intField = 33    
  
 intBarn = 15    
  
 intOut = 7    
  
 intTotalHorses = intField - intBarn + intOut    
  
 'Display results    
  
 MessageBox.Show("There are " & intTotalHorses & " horses in the field.")   
  
 End Sub
  
 End Class  

5.)Inglebert has 15 apples and 3 times as many oranges. How many pieces of fruit does she have?
-Hard code the initial facts
 Private Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intApples, intOranges, intPiecesFruit As Integer    
  
 'Set values    
  
 intApples = 15    
  
 intOranges = intApples * 3    
  
 intPiecesFruit = intApples + intOranges    
  
 'Display results    
  
 MessageBox.Show("Inglebert has " & intPiecesFruit & " pieces of fruit.")   
  
 End Sub  
  
 End Class   

6.)Don't make this table. This is just for your reference
English Geography Mathematics Science
Andrew 12 19 18 7
Brian 22 15 7 22
The table shows quiz marks out of 25 in each subject.

-How many marks did Brian totally obtain in Mathematics and Science?
 Private Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intMath, intScience, intMarks As Integer    
  
 'Set values    
  
 intMarks = 7 'Marks in Mathmetics   
  
 intScience = 22 'Marks in Science   
  
 intMarks = M + S    
  
 'Display results    
  
 MessageBox.Show("Brian has " & intMarks & " marks in Mathematics and Science.")   
  
 End Sub 
  
 End Class   
-How many more marks does Andrew need for a perfect score in Mathematics?
 Private Class Form1
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intMath, intScience, intMarks As Integer    
  
 'Set values    
  
 intMath = 18 'Brian's actual marks   
  
 intScience = 25 'total possible   
  
 intMarks = intScience - intMath    
  
 'Display results    
  
 MessageBox.Show("Brian needs " & intMarks & " marks for a perfect score in Mathematics.")   
  
 End Sub  
  
 End Class   
-What is Andrew's percentage for all of the quizzes together?
 Private Class Form1  
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
  
 'Declare variables    
  
 Dim intEnglish, intGeography, intMath, intScience As Integer    
  
 Dim decMarks As Decimal    
  
 'Set values 
  
 intEnglish = 12 'marks in English  
  
 intGeography = 15 'marks in Geography 
  
 intMath = 18 'marks in Mathematics    
  
 intScience = 7 'marks in Science          
  
 decMarks = (intScience + intMath + intEnglish + intGeography) / 100    
  
 'Display results    
  
 MessageBox.Show("Brian has " & FormatPercent(decMarks) & " marks for all the quizzes together.")   
  
 End Sub  
  
 End Class  

Sunday, February 6, 2011

String Concatenation

String Concatenation
 Dim strHotel as String
  
 Dim strPartOne as String = "This is"
  
 Dim strPartTwo as String = "the best"
  
 strHotel= strPartOne & strPartTwo  

Convert Decimal to Integer

Convert Decimal to Integer
 Dim decLose as Decimal = 23
  
 Dim intControl as Integer
  
 intControl= CInt(decLose)  

Convert Integer to Decimal

Convert Integer to Decimal

 Dim intControl as Integer= 56
  
 Dim decLose as Decimal
  
 decLose= CDec(intControl)  

Wednesday, February 2, 2011

Convert a Number Variable into a String

Blog post: blog how to convert a number variable into a string
 Dim intNumbers as Integer

 Dim strNumber as String
  
 intNumbers= "24"
  
 strNumber = CStr(intNumbers)
  

Tuesday, February 1, 2011

String, Boolean, Integer, Decimal

String, Boolean, Integer, Decimal

 Dim strOne as String

 strOne= "One"
  
 Dim RunningVB as Boolean

 RunningVB = True
  
 Dim intStocks as Integer

 intStocks= 37
  
 Dim decSeconds as Decimal

 decSeconds = 7.5