VB basic 2010 project.

harleygilly
harleygilly used Ask the Experts™
on
I have a project to code a program that will return a color when the 3rd chacture is B, G, r, or w. The program will return the values as long as I enter a captial letter. I am having problems converting it to take lower case letters. Below is what I have

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtItem_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItem.TextChanged
        lblColor.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        lblColor.Text = String.Empty
        Dim strId As String
        'converts to uppercase
        strId = txtItem.Text.Trim.ToUpper
        If strId.Length <> 5 Then
            MessageBox.Show("Item Number must contain 5 characters.",
            "Item Number",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information)

        Else
            If Mid(txtItem.Text, 3, 1) = ("B") Then
                lblColor.Text = "BLUE"
            ElseIf Mid(txtItem.Text, 3, 1) = ("G") Then
                lblColor.Text = "GREEN"
            ElseIf Mid(txtItem.Text, 3, 1) = ("R") Then
                lblColor.Text = "RED"
            ElseIf Mid(txtItem.Text, 3, 1) = ("W") Then
                lblColor.Text = "WHITE"



            Else
                MessageBox.Show("The item number must contain the color code B, G, R or W in the third position",
                "Item Number",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information)
            End If
        End If
    End Sub
End Class
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
harleygilly:

The compare is case sensitive.  Try the following:

      Else
            If Mid(txtItem.Text, 3, 1) = ("B") Then
                lblColor.Text = "BLUE"
            ElseIf Mid(txtItem.Text, 3, 1) = ("G") Then
                lblColor.Text = "GREEN"
            ElseIf Mid(txtItem.Text, 3, 1) = ("r") Then
                lblColor.Text = "RED"
            ElseIf Mid(txtItem.Text, 3, 1) = ("w") Then
                lblColor.Text = "WHITE"

Best regards,

AielloJ
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Try this out:
    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim strId As String = txtItem.Text.Trim.ToUpper
        If strId.Length = 5 Then
            Select Case strId.Substring(2, 1) ' zero based index
                Case "B"
                    lblColor.Text = "BLUE"
                Case "G"
                    lblColor.Text = "GREEN"
                Case "R"
                    lblColor.Text = "RED"
                Case "W"
                    lblColor.Text = "WHITE"
                Case Else
                    lblColor.Text = String.Empty
                    MessageBox.Show("The item number must contain the color code B, G, R or W in the third position", _
                        "Item Number", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)
            End Select
        Else
            MessageBox.Show("Item Number must contain 5 characters.", _
                "Item Number", _
                MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
        End If
    End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial