Monday, May 9, 2011

Methods #2

In this post i will demonstrate how to create functions for different buttons to condense cluttered code.

 Public Class Form1
  
   Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label ' Declares each new label
  
   Dim WithEvents butRoll As New Button 'declares each new button
  
   Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer ' sets each hand as an Integer 
  
   Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
  
   Dim rnd As New Random
  
   Private Sub addDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
  
     lbl.Text = 0 'Sets the text of each label
  
     lbl.Location = New Point(x, y) 'vasiably sets the location of each label
  
     lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
  
     lbl.Height = 40 'Sets the height
  
     lbl.Width = 40
  
     Me.Controls.Add(lbl) ' Adds each button to the form controls
  
   End Sub
  
   Private Sub points(ByRef txt As TextBox, ByVal x As Integer, ByVal y As Integer, ByRef c As String)
  
     txt.Text = c
  
     txt.Location = New Point(x, y) ' Sets the locations variably
  
     txt.Width = 150
  
     Me.Controls.Add(txt) ' Adds the textbox to the controls
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     addDice(lblDice1, 10, 20) ' Sets the location 
  
     addDice(lblDice2, 70, 20)
  
     addDice(lblDice3, 130, 20)
  
     addDice(lblDice4, 190, 20)
  
     addDice(lblDice5, 250, 20)
  
     points(lblYatzee, 20, 140, "Yatzees: 0") ' Sets the text in each textbox
  
     points(lblFourOfAKind, 20, 180, "Four Of A Kind: 0")
  
     points(lblThreeOfAKind, 20, 220, "Three Of A Kind: 0")
  
     butRoll.Text = "Roll" 'Sets the roll text
  
     butRoll.Location = New Point(100, 90) 'New location of butRoll
  
     Me.Controls.Add(butRoll)
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     Roll(lblDice1) 'Rolls the dice 
  
     Roll(lblDice2)
  
     Roll(lblDice3)
  
     Roll(lblDice4)
  
     Roll(lblDice5)
  
     getstats() ' retrieves stats from the textboxes and labels
  
     updatetext() ' updates the stats of each text box 
  
   End Sub
  
   Private Sub Roll(ByRef lbl As Label)
  
     lbl.Text = rnd.Next(1, 7) ' makes each label show a random number 
  
   End Sub
  
   Private Sub getstats()
  
     Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0} ' Gets stats function
  
     For Each lbl As Label In Me.Controls.OfType(Of Label)()
  
       arrNumbers(lbl.Text - 1) += 1
  
     Next
  
     For Each i As Integer In arrNumbers
  
       If i = 5 Then
  
         nYatzee += 1
  
       ElseIf i = 4 Then
  
         nFourOfAKind += 1
  
       ElseIf i = 3 Then
  
         nThreeOfAKind += 1
  
       End If
  
     Next
  
   End Sub
  
   Private Sub updatetext() 'updates the textboxes
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
 End Class
  

Methods

Here is how Methods are done using different Functions .

  Public Class Form1
  
 Public Class Form1
  
   Dim nChar As Integer
  
   Dim nWord As Integer
  
   Dim nSpace As Integer
  
   Dim nSentence As Integer
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim c As String = txtPhrase.Text
  
     nChar = textLength(c)
  
     nWord = CountWords(c)
  
     nSentence = sentenceCount(c)
  
     nSpace = wordSpace(c)
  
     updateChar()
  
   End Sub
  
   Private Sub updateChar()
  
     TextBox1.Text = nChar
  
     TextBox2.Text = nWord
  
     TextBox3.Text = nSentence
  
     TextBox4.Text = nSpace
  
   End Sub
  
   Private Function textLength(ByVal str As String) As String
  
     Return str.Length
  
   End Function
  
   Public Function CountWords(ByVal value As String) As Integer
  
     Return value.Split(" ").Count()
  
   End Function
  
   Public Function wordSpace(ByVal space As String) As String
  
     Dim y As Integer
  
     y = space.Split(" ").Count
  
     y -= 1
  
     Return y
  
   End Function
  
   Public Function sentenceCount(ByVal sentence As String) As String
  
     Dim x As Integer
  
     x = sentence.Split(".").Count()
  
     x -= 1
  
     Return x
  
   End Function
  
 End Class  

Sunday, May 8, 2011

Programmatic adding of elements

 This code will create a times table..

  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     For x = 1 To 10
  
       For y = 1 To 10
  
         Dim button1 As New Button
  
         button1.Location = New Point(40 * x, 40 * y)
  
         button1.Width = 40
  
         Me.Controls.Add(button1)
  
         button1.Text = x * y
  
       Next
  
     Next
  
   End Sub  

Saturday, May 7, 2011

Values and Formulas

In this blog i will show how to problem solve :
1. morgan collecting stuffed animals prooblem
  • set variables for the animals being counted
    • cows(c) sheep(s) and total(t)
  • determine a formula for solving the problem
    • c + s= t
  • Morgan has 6 cows and 7 sheep so
    • 6+7= 13
2. Mega burgers
  • burgers * price
    • 4*7= 28
3. Fruit question
  • dim apples as double = 15
  • dim oranges as double = 12
4. Horse problem
  • dim horse as double = 33
5. apples to oranges problem
  • dim apples, oranges as double
  • apples= 15
  • oranges = apples*3
6. Grade subject problem
dim score ,math, science,eng, geo as decimal
dim score as double =
dim total as decimal
  •  math+ science = 25
  • score - total= 7
  • total /4 = 14

Conditionals

Problem # 1 :  Here i am gonna demonstrate how to make a shipping calculator:

  Dim shipping As Decimal = 50
  
   Dim price As Decimal
  
   Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
  
     price = shipping + txtPurchasePrice.Text
  
     If price > 50 Then
  
       shipping = 0
  
     End If
  
     MsgBox(("Your Total Price is: " & FormatCurrency(price)))
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     txtPurchasePrice.Text = ""
  
   End Sub  


This is an example of a program that will control the heater and AC

  Dim temperature As Double
  
   Dim Heat, AC As Double
  
   Private Sub btnTempControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTempControl.Click
  
     temperature = txtCalcTemp.Text
  
     If temperature < 72 Then
  
       MsgBox("The Heater is being activated ")
  
     Else
  
       If temperature > 76 Then
  
         MsgBox("The AC is being activated")
  
       Else
  
         MsgBox("the system is idle")
  
       End If
  
     End If
  
   End Sub  

 Here i am going to demonstrate a clothing size calculator:

 Dim age As Double
  
   Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
  
     age = txtAgeInput.Text
  
     If age <= 2 Then
  
       MsgBox("you should go with XS")
  
     Else
  
       If age <= 4 Then
  
         MsgBox("Small")
  
       Else
  
         If age <= 8 Then
  
           MsgBox("Medium is the way to go")
  
         Else
  
           If age <= 12 Then
  
             MsgBox("Large")
  
           Else
  
             If age > 13 Then
  
               MsgBox("XL")
  
             End If
  
           End If
  
       End If
  
     End If
  
     End If
  

Friday, April 1, 2011

Array Project (days of the week)

Days of the week Project


 Dim week As New Hashtable 'declare at class leve
  
 Dim day As DictionaryEntry
  
 week.Add("Sunday", "Day 1")
  
 week.Add(Monday", "Day 2")"
  
 week.Add(Tuesday", "Day 3")"
  
 week.Add(Wednesday", "Day 4")"
  
 week.Add(Thursday", "Day 5")"
  
 week.Add("Friday", "Day 6")"
  
 week.add("Saturday", "day 7")'Display a single Item
  
 MsgBox(day.Key)For Each day In week
  
 Next
  
 txtTitle.Text = ""
  

Array Project *revised

 Here is an example of a Printer Queue:
  Dim printJobs As New Queue
  
   Private Sub btnSendJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendJob.Click
  
     Dim printer As New Hashtable
  
     printer.Add("Title", (txtTitle.Text))
  
     printer.Add("Pages", (cbPages.Text))
  
     printJobs.Enqueue(printer)
  
     lstJobs.Items.Clear()
  
     For Each h As Hashtable In
  
      printJobs
  
       lstJobs.Items.Add(h.Item("Title") & " " & (h.Item("Pages")))
  
     Next
  
     txtTitle.Text = ""
  
     Me.cbPages.Items.Remove("")
  
   End Sub
  
   Private Sub btnClearCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearCurrent.Click
  
     lstJobs.Items.Clear()
  
   End Sub
  
 End Class
  

 In this example i am going to demonstrate how to create an array that will  store  users; Name(first and last), and Email address.
  Dim Names As New Hashtable
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Names(1) = (txtFirstName.Text)
  
     Names(2) = (txtLastName.Text)
  
     Names(3) = (txtEmail.Text)
  
     txtEmail.Clear()
  
     txtFirstName.Clear()
  
     txtLastName.Clear()
  
   End Sub
  
   Private Sub btnFirstName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstName.Click
  
     MsgBox(Names(1))
  
   End Sub
  
   Private Sub btnLastName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastName.Click
  
     MsgBox(Names(2))
  
   End Sub
  
   Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  
     MsgBox(Names(3))
  
   End Sub
  
 End Class
  

Tuesday, March 8, 2011

In this post I will show how to set different variables types.

This is code show how to set  integer, decimal,and string data types:

1:  Dim Apples As Integer   2:  Dim bible As Decimal 3:  Dim corral As String

Sunday, March 6, 2011

int and string arrays , ArrayList, HashTables and Queue

This Code will show how how to create an arraylist, hashtable and a queue

 Public Class frmTestArrayHash
  
   Dim queueList As New Queue
  
   Dim weeks As New Hashtable
  
   Dim sport As ArrayList
  
   Private Sub btnHashTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHashTable.Click
  
     Dim weeks As New Hashtable
  
     Dim day As DictionaryEntry
  
     weeks.Add("1", "Sun")
  
     weeks.Add("2", "Mon")
  
     weeks.Add("3", "Tue")
  
     weeks.Add("4", "Wed")
  
     weeks.Add("5", "Thu")
  
     weeks.Add("6", "Fri")
  
     weeks.Add("7", "Sat")
  
     'Display a single Item
  
     MsgBox(weeks.Item("5"))
  
     'Search an Item
  
     If weeks.ContainsValue("Tue") Then
  
       MsgBox("Find")
  
     Else
  
       MsgBox("Not find")
  
     End If
  
     'remove an Item
  
     weeks.Remove("3")
  
     'Display all key value pairs
  
     For Each day In weeks
  
       MsgBox(day.Value)
  
     Next
  
   End Sub
  
   Private Sub btnArrayList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArrayList.Click
  
     Dim sport(5) As String
  
     Dim i As Integer
  
     sport(0) = "Soccer"
  
     sport(1) = "Cricket"
  
     sport(2) = "Rugby"
  
     sport(3) = "Aussie Rules"
  
     sport(4) = "BasketBall"
  
     sport(5) = "Hockey"
  
     For i = 0 To sport.Length - 1
  
       MessageBox.Show("The sport you chose is " & sport(i))
  
     Next
  
     'displaying value from array
  
   End Sub
  
   Private Sub btnQueue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQueue.Click
  
     Dim vince As String
  
     queueList.Enqueue("Sun")
  
     queueList.Enqueue("Mon")
  
     queueList.Enqueue("Tue")
  
     queueList.Enqueue("Wed")
  
     queueList.Enqueue("Thu")
  
     queueList.Enqueue("fri")
  
     queueList.Enqueue("Sat")
  
     Do While queueList.Count > 0
  
       vince = queueList.Dequeue
  
       MsgBox(vince)
  
     Loop
  
   End Sub
  
 End Class
  

Monday, February 28, 2011

Practice #4 number 2



Dim total, goal, months, pay, years As Integer months = 0
 
pay = 25 

months += 1
total += pay 'adds pay CInt(txtMonthyIncome.Text) 
Do While total < 10000
LooplblYears.Text = (months / 12).ToString ' loops months so that a counnter is kept

Friday, February 18, 2011

Do While and For each loops.

This code shows Do While First

  • The Do While...Loop is used to execute statements until a certain condition is met. The following Do Loop counts from 1 to 100.
Dim number As Integer
number = 1
Do While number <= 100
number = number + 1
Loop
Do (While last)
For
 For  n = 1 To 3
            MsgBox(n)
        Next
Her is an example of a for each loop
    For Each singleChar In siteName
            MsgBox(singleChar)
        Next

Monday, February 14, 2011

Selecting items in a listbox

This code will show how to select items in a listbox

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim shirt, pants, shoes As String 
shirt = "shirt"
pants = "pants"
shoes = "shoes"


Me.Close()
End Sub ClassMe.lstTotalItems.Items.Add(shirt)Me.lstTotalItems.Items.Add(pants)Me.lstTotalItems.Items.Add(shoes)Me.lstTotalItems.ScrollAlwaysVisible = TrueEnd Sub
lstTotalItems.SelectedItem = Private Sub btnItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem1.Click"shirt"End Sub
lstTotalItems.SelectedItem = Private Sub btnItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem2.Click"pants"End Sub
lstTotalItems.SelectedItem = Private Sub btnItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem3.Click"shoes"End Sub 



EndPrivate Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

Adding Items to a listbox

This code will demonstrate how to add items and select them in a listbox

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     Dim shirt, pants, shoes As String
  
     Shirt = "shirt"
  
     pants = "pants"
  
     shoes = "shoes"
  
     Me.lstTotalItems.Items.Add(shirt)
  
     Me.lstTotalItems.Items.Add(pants)
  
     Me.lstTotalItems.Items.Add(shoes)
  
     Me.lstTotalItems.ScrollAlwaysVisible = True
  
   End Sub
  
   Private Sub btnItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem1.Click
  
     lstTotalItems.SelectedItem = "shirt"
  
   End Sub
  
   Private Sub btnItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem2.Click
  
     lstTotalItems.SelectedItem = "pants"
  
   End Sub
  
   Private Sub btnItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem3.Click
  
     lstTotalItems.SelectedItem = "shoes"
  
   End Sub
  
   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  
     Me.Close()
  
   End Sub
  
 End Class
  

Adding Buttons and labels programmatically ...

This code shows how to add textboxes and labels and set their location background color etc...

  Public Class Form1
  
 Dim lbl1, lbl2, lbl3 As New Label
  
 Dim WithEvents txt1, txt2, txt3 As New TextBox
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
 lbl1.Text = "Hello"
  
 lbl1.Location = New Point(8, 163)  
 lbl2.AutoSize = true
  
 Me.Controls.Add(lbl1)
  
 lbl2.Text = "Press the reset button"
  
 lbl2.Location = (123, 5)
  
 lbl3.AutoSize = true
  
  Me.Controls.Add(lbl2)
  
 lbl3.Text = "Live life "
  
 lbl3.Location = New Point(5, 236)True
  
 txt1.Location =New Point(1, 6)
  
  txt1.Name ="txt1"
  
  Me.Controls.Add(lbl3)
  
 Me.Controls.Add(txt1)" 
  
  txt2.Location =New Point(65, 6)
  
  txt2.Name =)"txt2" 
  
  txt3.Location = New Point(124, 240) 
  
 txt3.Name ="txt3" 
  
  Me.Controls.Add(txt2)
  
 New Point(54, 166)   
  
 Me.Controls.Add(txt3) 
  
  
  
 End Sub 
  
 End Class
  

Manipulationg Buttons and Textboxes

This code will manipulate a textbox on a form

 Public Class Form1
  
   Private Sub btnBackGroundColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackgroundColor.Click
  
     'This changes the text colors back and forth between tomato and white
  
     If txtOutputbox.BackColor = Color.White Then
  
       txtOutputbox.BackColor = Color.Tomato
  
     Else
  
       txtOutputbox.BackColor = Color.White
  
     End If
  
   End Sub
  
   Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  
     'Enables the textbox
  
     If txtOutputbox.Enabled = False Then
  
       txtOutputbox.Enabled = True
  
     Else
  
       txtOutputbox.Enabled = False
  
     End If
  
   End Sub
  
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  
     'This one it the same as the two above.
  
     If txtOutputbox.BorderStyle = BorderStyle.Fixed3D Then
  
       txtOutputbox.BorderStyle = BorderStyle.None
  
     Else
  
       txtOutputbox.BorderStyle = BorderStyle.Fixed3D
  
     End If
  
   End Sub
  
   Private Sub btnText_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnText.Click
  
     If txtOutputbox.Text = "If heaven was a mile away" Then
  
       txtOutputbox.Text = " "
  
     Else
  
       txtOutputbox.Text = "If heaven was a mile away"
  
     End If
  
   End Sub
  
 End Class
  

in this post i will Show how manipulate events on a button

These lines of code will change the location of the button.

Public Class Form1Dim var As Integer 

var += 1
LblCounter.Text = var
Private Sub form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ClickEnd Sub



Private Sub button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnterDim p1 As New Point(52, 214) 'changes the point of the button when the mouuse is entered Dim p2 As New Point(227, 10)If Button1.Location = p1 Then  'if the buttons location doesnt equal the 1st point then the second locations is enabledButton1.Location = p2
ElseButton1.Location = p1 ' (Else If) the point goes back to the 1st point
End If 
 

End
End Sub Class

Monday, February 7, 2011

How to add a button, and a textbox to a page programmatically.

I will demonstrate how to add a textbox to a program programatically.

Dim textbox1 As New TextBoxtextbox1.Size =
TextBox8.ReadOnly = True  'makes the text box Read-onlytextbox1.Name = "TextBox1" 'Creates a name for the textboxTextBox1.Location =
TextBox8.Width = AutoSize
TextBox8.Height = AutoSize


'These lines of code Place a button of code Dim button As New Button 'adds a button to the formbutton.AutoSize = Truebutton.Text = "Exit" 'Places a name on the button
button.Location =

'This code adds a list box to the form
Dim lstShoes As New ListBox
Me.Controls.Add(button) 'Adds button to controlsNew Point(5, 239) ' Places the button in a specific area on the form.
Me.Controls.Add(textbox1) 'Adds textbox1 to the control
New Point(60, 185)'this operation sets the location of the new textboxes
New Size(150, 20)' sets size

Monday, January 31, 2011

Case statements

Here is an example of how to effectively write a Case statement. This Code is very useful in a prgram multiple outcomes to a problem.

 Dim fries As String
  
 Dim DietState As String 
  
 fries = TextBox1.Text
  
 Select Case creamcake
  
 Case "Eaten"
  
 DietState = "Diet Ruined"
  
 Case "Not Eaten"
  
 DietState = "Diet Not Ruined"
  
 Case Else
  
 DietState = "Didn't check"
  
 End Select
  
 MsgBox DietState
  

If statement that makes one decision

  In this code i will show  an if statement  that makes only one decision. This would be helpful when making a program or application that would have to make one or more decisions.

 Dim scriptEngine as String
  
 If scriptEngine = "VB" Then
  
   runningVB = True
  
 End If
  
 messageBox.Show(runningVB)
  
 In this code many decisions are made.
  
 Dim intW As Integer = 10
  
 If intW > 11 Then
  
   MessageBox.Show("intX > 11")
  
 ElseIf intW < 5 Then  MessageBox.Show("intW < 5")
  
 Else
  
   MessageBox.Show("Fallback code block")
  
 End If
  

How to convert a number variable into a string

This code describes how to convert a number variable to a string:
    • Convert number to string
      • CStr
      • IntVar.ToString
       Dim eggs As Decimal = 62
        
        Dim Charles As Integer = eggs.ToString 
        
        MessageBox.Show(Charles)  
      

Wednesday, January 26, 2011

Converting Integers to decimal

In this code I will show how to convert an integer to a decimal. 

Converts an decimal to a Integer




 Dim milk AS Decimal 
  
 Dim Charles as Integer
  
 Eggs = 24
  
 charles =CInt(eggs) or Decimal.ToInt32(eggs) 
  
 MessageBox.Show(charles.GetType.ToString)
  

Join a string to another string, also known as string concatenation.

This code joins two strings and is known as concatenation


Dim FirstName As String
Dim LastName As String
Dim FullName As String

FirstName = “Vincent”
LastName= “Webb”
FullName = FirtName & LastName

MessageBox.Show(FullName)

Sunday, January 16, 2011

Friday, January 14, 2011

This is my new Programming 2 blog Welcome. Since i dont know what else to say ....