CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2013
    Posts
    10

    Visual Basic Coding Help: Last Name Won't Capitalize

    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.

    Code:
    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

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Visual Basic Coding Help: Last Name Won't Capitalize

    your problem is that your only finding the first space in the full name and attempting to split 3 words from there...
    Code:
                index = fullName.IndexOf(" ")
                If index > -1 Then
                    firstName = fullName.Substring(0, index)
                    middleName = fullName.Substring(index + 1)
                    lastName = fullName.Substring(index + 2)
    rather try something like this..
    Code:
                index = fullName.IndexOf(" ")
                index2 = fullName.IndexOf(" ", index+1)
    then of course you just have to validate if the second index does exist.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Visual Basic Coding Help: Last Name Won't Capitalize

    Why not use the StrConv function and ProperCase flag?
    Code:
    s = StrConv("hello world", VbStrConv.ProperCase)
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured