Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Visual Basic Coding Help: Last Name Won't Capitalize


  • Please log in to reply

#1
Moon_Dew

Moon_Dew

    Member

  • Member
  • PipPip
  • 10 posts
The program I'm working on allows the user to enter names (first, middle, and last names, like "First Middle Last") into a list box in the correct case. I have two problems so far when I use the debug command. The last name won't capitalize and the middle name reappears after the last name, except the first letter is removed and the letter after that is capitalized. I could use a little help figuring this out.

The rest of the code seems to be working, it's just the name part, but I've uploaded the full code just in case there was an error I missed.

In case it makes any difference, I'm using Microsoft Visual Basic 2010 Express.

Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    Private Sub nameTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles nameTextBox.Enter
        ' select the existing text

        nameTextBox.SelectAll()
    End Sub

    Private Sub fileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles fileExitMenuItem.Click
        Me.Close()

    End Sub

    Private Sub filePrintMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles filePrintMenuItem.Click

        PrintForm1.PrintAction =
            Printing.PrintAction.PrintToPreview
        PrintForm1.Print()

    End Sub

    Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click

        Dim fullName As String
        Dim firstName As String
        Dim middleName As String
        Dim lastName As String
        Dim index As Integer

        fullName = nameTextBox.Text.Trim
        If fullName = String.Empty Then
            MessageBox.Show("Please enter a name",
                            "Bucky Burgers",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information)
        Else
            index = fullName.IndexOf(" ")
            If index > -1 Then
                firstName = fullName.Substring(0, index)
                middleName = fullName.Substring(index + 1)
                lastName = fullName.Substring(index + 2)

                firstName =
                    firstName.Substring(0, 1).ToUpper &
                    firstName.Substring(1).ToLower

                middleName =
                    middleName.Substring(0, 1).ToUpper &
                    middleName.Substring(1).ToLower

                lastName =
                    lastName.Substring(0, 1).ToUpper &
                    lastName.Substring(1).ToLower
                fullName = firstName & " " & middleName & " " & lastName
            Else
                fullName =
                    fullName.Substring(0, 1).ToUpper &
                    fullName.Substring(1).ToLower

            End If

            namesListBox.Items.Add(fullName)
        End If
        nameTextBox.Focus()
        nameTextBox.SelectAll()

    End Sub
End Class

  • 0

Advertisements


#2
RKinner

RKinner

    Malware Expert

  • Expert
  • 24,598 posts
  • MVP
I'm not much of a programer but it appears to me that there is something wrong here:


index = fullName.IndexOf(" ") <==Find the location of the first space in the full name

If index > -1 Then <==If there is a space then:

firstName = fullName.Substring(0, index) <==Define firstName as substring from zero to the location of the space.


middleName = fullName.Substring(index + 1) <== We need a comma and some way of telling when the middle name ends? Otherwise it just grabs everything from the first space to the end.


lastName = fullName.Substring(index + 2) <==This one is even odder. Just pick the second letter of the middlename as the starting point for what it thinks is the lastName.

I think you needs to locate the second space in the name and call it Index2. Then you could say:
middleName = fullName.Substring(index + 1, Index2)
lastName = fullName.Substring(index2 + 1)

Also what happens if they have no middle name or if they tack on Jr or III?
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP