Wednesday, May 30, 2012

BookstoreOrderForm

 Today I found that to add items to a list at runtime, you use the following code:

lstItemsOrdered.Items.Add

LoanPayment & ComboBoxes

Today I found out that information that is entered in a combobox is actually treated as a string as opposed to the textbox format, where the property of text from a textbox can be treated as a Textbox.

GetPercentAmount(Me.cboRate.Text, rate, validData)


Sub GetDollarAmount(ByVal txtUserData As TextBox, ByRef dollars As Decimal, _
    ByRef isValid As Boolean)



Sub GetPercentAmount(ByVal cboUser As String, ByRef percent As Double, ByRef isValid _
    As Boolean)

Wednesday, May 23, 2012

PerfectSquare

First, a random thought:
The word "perfect" is one of such wonderfully rounded curves that seem to convey the meaning of itself - perfection.


I was trying to pursue perfection in this application that I created, and found it marred by errors instead.


First, I awkwardly forgot what an Int() function does. It rounds a number up to the nearest integer. 


Then I got my variables mixed up:
number was the user-entered number; numRt was the square root of that number

I originally wrote:

If Int(number) = numRt Then
            Return True
        End If

What happened, of course, is that only 1 returned a true statement (only the square root of 1 could possibly equal 1).


This below would return the correct answer.


If Int(numRt) = numRt Then
            Return True
        End If


This is my final code

  Dim num As String = Val(Me.txtNumber.Text)

        If PerfectSquare(num) = True Then
            Me.lblAnswer.Text = num & " is a perfect square."
        Else
            Me.lblAnswer.Text = num & " is not a perfect square."
        End If

    End Sub
Function PerfectSquare(ByVal number As Double) As Boolean

        Dim numRt As Double = Math.Sqrt(number)

        If Int(numRt) = numRt Then
            Return True
        End If

    End Function

    Private Sub txtNumber_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumber.TextChanged
        Me.lblAnswer.Text = ""
    End Sub

Thursday, May 17, 2012

MetricConversion

This application involved entering a number and being able to convert between metric/customary units for various lengths.

I made a few errors while typing this program
The first one was that I treated a function like a procedure and typed:
finalNumber=(initialNum*2.54)
Return finalNumber

I changed this to:
 Return (initialNum * 2.54)
after Ms. Stark pointed out my error.

The second mistake I made was I predefined finalNumber as being equal to zero:
Dim finalNumber As Double

The third mistake I made was that I forgot to type in the tags. Oops.

Thursday, May 10, 2012

LetterGrade

This program returns a letter grade when a test score is entered.
The program that my modified version is based on uses A=90 or above, B=80 and above, etc. I changed it to display A+, A-, etc. by adding in additional ElseIf statements. I also had to change the Function's type from Char to String.


Function LetterGrade(ByVal score As Double) As String
        If score >= 97 Then
            Return "A+"
        ElseIf score >= 93 Then
            Return "A"
        ElseIf score >= 90 Then
            Return "A-"

Wednesday, May 9, 2012

Add Coins

I made a very simple mishap here. I used two equal signs and wrote total = (quart * 0.25) + (dime * 0.1) =  (nick * 0.05) + (penny * 0.01), instead of total = (quart * 0.25) + (dime * 0.1) + (nick * 0.05) + (penny * 0.01) with only one equal sign.

CD DVD Tips


This was the code I wrote originally:

Private Sub btnTip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTip.Click

        Call Message(Me.lblTip)

    End Sub

    'After button has been clicked, a random number will be chosen that represents one of the three tips
    'post: the tip will show up in the label
    Sub Message(ByRef lblMessage As Label)
        Randomize()
        Dim tipNumber As Integer
        tipNumber = Int(3 * Rnd()) + 1

        Select Case tipNumber
            Case 1
                lblMessage.Text = "Handle CD/DVDS by the center hole or by the edges."
            Case 2
                lblMessage.Text = "Keep CD/DVDs away from extreme temperatures and moisture."
            Case 3
                lblMessage.Text = "Store discs in a jewel case or sleeve to prevent scratches."
        End Select

This is the updated version with parameters based on TestRndIntFunction

   Dim lowNum As Integer = 1
        Dim highNum As Integer = 3
        Dim chosenNum As Integer
        Call RndInt(lowNum, highNum, chosenNum)

        If chosenNum = 1 Then
            Me.lblTip.Text = "Handle CD/DVDS by the center hole or by the edges."
        ElseIf chosenNum = 2 Then
            Me.lblTip.Text = "Keep CD/DVDs away from extreme temperatures and moisture."
        Else
            Me.lblTip.Text = "Store discs in a jewel case or sleeve to prevent scratches."
        End If

    End Sub

    'After button has been clicked, a random number will be chosen that represents one of the three tips
    'post: the tip will show up in the label
    Sub RndInt(ByVal firstNum As Integer, ByVal secondNum As Integer, ByRef thirdNum As Integer)
        Randomize()
        thirdNum = ((secondNum - firstNum + 1) * Rnd()) + firstNum

Tuesday, May 1, 2012

Acronym - unresolved

I haven't figured this out yet:
the original mess:
Dim numEntered As String
        Dim numWords As Integer
        Dim acronym As String
        Dim wordEntered As String
        Dim letter As String
        Dim fullWord As String

        numEntered = InputBox("Number of words", "Enter the number of words in the acronym")
        numWords = Val(numEntered)

        fullWord = Nothing
        For count As Integer = 1 To numWords
            wordEntered = InputBox("Word", "Enter the word.")
            wordEntered.ToUpper()
            letter = wordEntered.Chars(0)
            letter = fullWord.Chars(count)
            acronym = Nothing
            acronym = String.Concat(acronym, letter)
        Next count


Name Backwards


I made this one far too complicated.

I tried to split up the program to do the first name and last name separately. I also had the characters picked out one by one and... things just got really messy.

The new and improved code:

    Dim origName As String = Me.txtName.Text
 
        Dim lengthName As Double = origName.Length
        Dim backName As String = Nothing
        Dim currentLetter As String

        For count As Integer = (lengthName - 1) To 0 Step -1
            currentLetter = origName.Chars(count)
            backName = String.Concat(backName, currentLetter)
        Next count

        Me.lblBackwards.Text = backName

Wednesday, April 11, 2012

Remove String

Today's mishap of the day:

Dim newSentence As Char
versus
Dim newSentence As String

That was all it took to make the program mess up. Head-shaking. The program's objective is to, given a sentence and a phrase to remove from that sentence, remove that phrase. For example, if you type "I dislike vegetables," and remove "dis," the new statement should read "I like vegetables." The error above meant only "I" showed up in the new statement.

Tuesday, April 10, 2012

CD Calculator

This calculates the number of years for an initial investment to accumulate (thanks to interest) to a certain amount based on given initial amount, final amount, and interest rate.

A few things I learned from this:

a) When setting up a counter, define an initial value for the counter.
years=0
years += 1

b) Argh! Use decimal format for interest rate. It's much more logical.

c) Check signs (ie greater than versus less than).

Thursday, March 29, 2012

Unique Random Numbers

This program has an input box that prompts the user for a number and then generates 3 unique numbers between the given number and 1. A simple mistake that I made, typing in givenNum=InputBox(PROMPT, TITLE) twice, resulted in the input box popping up multiple times before finally generating the unique numbers. Argh.

Also, Juan taught me something pretty nifty regarding setting the maximum width of a label by going to the Properties bar in the designer window. That makes life much prettier.

Tuesday, March 27, 2012

Phone Bill


I considered going with ElseIf statements but suspected it might not work, so I just used If Then statements.

Dim Charge As Decimal = 25

        If Me.chkForward.Checked Then
            Charge = 3.5 + Charge
        End If
        If Me.chkWaiting.Checked Then
            Charge = 3.5 + Charge
        End If
        If Me.chkID.Checked Then
            Charge = 3.5 + Charge
        End If

        Me.lblAnswer.Text = Charge

Tuesday, March 20, 2012

Rock Paper Scissors - using nested If then else


This really got me confused. The correct format is:

If Me.radRock.Checked And computerChoice = rock Then
            Me.lblMessage.Text = "Computer throws rock. It's a draw! Cordial handshake."
        Else
            If Me.radPaper.Checked And computerChoice = rock Then
                Me.lblMessage.Text = "Computer throws rock. You win! Computer sniffles."
            Else
                If Me.radScissors.Checked And computerChoice = rock Then
                    Me.lblMessage.Text = "Computer throws rock. Computer wins! Computer dances happily."
                End If
            End If
        End If

I initially did not have the computerChoice defined on every line so it didn't work. Also, just using Else (without an If statement) would not work.

Thursday, March 15, 2012

Test Grade


I have yet to figure out how Visual Basic reads things in a predictable manner. For example, when I placed the list below (of Select Case) in reverse order, the program would only show me "D" for any number greater than 60. I had to switch the order with "A" on top and "F" on the bottom for it to work:
Dim Grade As Double
        Grade = Val(Me.txtGrade.Text)
        Select Case Grade
            Case Is > 90
                Me.lblMessage.Text = "A"
            Case Is >= 80
                Me.lblMessage.Text = "B"
            Case Is >= 70
                Me.lblMessage.Text = "C"
            Case Is >= 60
                Me.lblMessage.Text = "D"
            Case Is < 60
                Me.lblMessage.Text = "F"
        End Select

Tuesday, March 13, 2012

Circle Area (with If Then Statements)

Syntax, once again, is very complicated. I originally wrote the text for the program to calculate circle area so that:

radius = Val(Me.txtRadius.Text)
        If radius < 0 Then
            Me.lblArea.Text = "Negative radii are illegal."
        End If
        area = PI * (radius ^ 2)
        Me.lblArea.Text = area 'Shows answer of area of circle

This should have resulted in no answer displaying if the radius entered was negative. However, an answer still showed up when I pressed Debug, so I went back and rearranged things:

radius = Val(Me.txtRadius.Text)
        area = PI * (radius ^ 2)
        Me.lblArea.Text = area 'Shows answer of area of circle

        If radius < 0 Then
            Me.lblArea.Text = "Negative radii are illegal."
        End If

Friday, March 9, 2012

Change/Time Conversion

I was finding it difficult initially to get the right operations to work for these two programs, especially with the Change program. I did the division out on a piece of paper, trying to figure out what information was needed for the remainder, etc.

Thursday, March 8, 2012

Energy

Yesterday, I discovered that "e" and "E" in VB are predefined numbers/operations (e=2.71) that can't be used as variables.

Tuesday, March 6, 2012

Temperature Converter Update

Today I managed to figure out how to make the temperature converter less clunky and more user-friendly. When the user clicks on Celsius from the drop down menu, for example, the label next to the text box will read: "Enter the temperature in Fahrenheit" and the label next to where the answer will be reads: "The temperature in Celsius is." The message changes depending on what is chosen from the menu. Clicking on the drop-down option also causes the appropriate button to light up.

After keying in the temperature to convert from, the user clicks on the lit-up button to see the appropriate temperature. The answer is cleared if new text is keyed in or a different temperature type is selected from the menu.

Friday, March 2, 2012

Temperature Converter

The converter I currently have works, but is harder to use. To convert, you have to click on Celsius/Fahrenheit in the menu, then type in the number, then click on the appropriate temperature again in the menu.

Wednesday, February 29, 2012

Skyhook International

Syntax really does matter. Rather, matter really does syntax.

I discovered this while performing operations for the Skyhook International program:
hook \ 3 = box -> incorrect
box = hook \ 3 -> correct

Confusingly, though, when you want something to appear:

Me.lblBoxAns.Text = box

Computers are confusing. But they're fun.

Tuesday, February 28, 2012

Rectangle Area

Had a momentary slip-up today when I forgot to key in Dim area As Integer (not limiting area), which confused the computer.

Also, labels are finicky. txt not text for textbox names.

Friday, February 17, 2012

System Clock

This one really stumped me. I tried to use different combinations of Me.lbl with the word TimeString incorporated but it didn't work for me. I used the Help section and found a Me.lbl.Text= "The time now is" & TimeString. By tying it to a button command, that worked for me.

I wondered if there was anything else simpler. So I consulted a few neighbors, who had used other techniques - the Timer and using Me.lblTime.Text= TimeString.

Wednesday, February 15, 2012

Lightbulb Moment

I just figured out how to change the text alignment in a single label!
Me.label.TextAlign=(choose option)

I kept on getting stuck. I thought I had to do something with "Application," but nothing showed up with IntelliSense. Then I used Me.Text (but that naturally didn't work). I finally realized that I needed to follow the formatting of the Me.Label.Text= command that we'd used in the past.

The second part to making this work was changing AutoSize to False. This allowed me to resize the label.

Onward ho to the next challenge!

Tuesday, February 14, 2012

Calculations

I forgot how to do the calculation code. Sheepish grin. So, here it goes: Choose the button, then the declaration (Click). Then type in Me.Label.Text=numbers here.