Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I am currently busy on a program in which when a record is added i need to increment a letter in a text box. Please could someone help me with incrementing letters , have never done it before?
 
thanks
Posted 13-Nov-12 1:55am
isi19439
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this code
(for sake of simplicity it assumes the input string being uppercase)
Public Function Increment(ByVal s As String) As String
    Dim index As Integer = s.Length - 1
    Dim n As Integer
    While index > 0 And s.Chars(index) = "Z"
        index = index - 1
    End While
 
    Increment = ""
    For n = 0 To index - 1
        Increment = Increment + s.Chars(n)
    Next
 
    If index > 0 Then
        Increment = Increment + Microsoft.VisualBasic.ChrW(Microsoft.VisualBasic.AscW(s.Chars(index)) + 1)
 

        For n = index + 1 To s.Length - 1
            Increment = Increment + "A"
        Next
    Else
        Increment = "A" + s
    End If
 
End Function
 

Sub Main()
    Dim nxt As String
    nxt = Increment("ZZABZZ")
End Sub
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks for all the help
i have found an easier solution
 

Dim letter As String = "A"
letter = Chr(Asc(letter) + 1)
Me.letter.Text = letter
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
You will need to load the the alphabet in an array
Dim letters(25) As String
 
letters(0) = "A"
letters(1) = "B"
letters(2) = "C"
letters(3) = "D"
letters(4) = "E"
etc.
Then You will need to grab the Text box's contents.
Dim letterGrab As String
 
letterGrab = tbLetter.Text
Now get the letter's index
Dim found As Boolean = False
Dim FoundIndex As Integer = 0
For i As Integer = 0 To 25
 
    If letters(i) = letterGrab Then
        found = True
        FoundIndex = i
        Exit For
    End If
Next
Finally get the new Letter and put it in the text box
If found = True Then
    FoundIndex = FoundIndex + 1
    tbLetter.Text = letters(FoundIndex)
        
Else
'What to do if end of array reached
End If
 
If you need case insensitive comparison, change code block 3 to this:

Dim found As Boolean = False
Dim FoundIndex As Integer = 0
For i As Integer = 0 To 25
Dim CaseDiff As Integer  = String.Compare(letters(i), letterGrab, True)
 
    If CaseDiff = 0 Then
        found = True
        FoundIndex = i
        Exit For
    End If
Next
 
Hope This Helps
Jacques
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 364
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Sergey Alexandrovich Kryukov 166
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100