Auto increment data

Joined
Aug 15, 2012
Messages
6
Programming Experience
1-3
Hi i would like to know if i have a textboxt with alpha numeric numbers 001 ,002,003 , numbers.text, and a second textbox called suffix.text how would i be able to auto increment the suffix so if the user enters 001 as the first entry in the numbers.text and then the next value you they add in the numbers.text is 001,i need the suffix.text to automatically show, A
so it would look like


number.text suffix.text
001
001 a
001 b
002
002 a


Etc
 
Not really what I meant. Can they enter the numbers out of order because that's a slightly more complicated scenario than if they have (and can always be relied upon) to enter the numbers in sequence?
 
Ok. Trying to do this with two textboxes was a complete nightmare so I've elected for a two column DataGridView instead; Column 0 is Prefix, Column 1 is Suffix. This also means that the entries are editable at any time and needn't be entered in sequence.

Dim Unique As List(Of String) = New List(Of String)
    Dim c As Integer = 0




    Private Sub dgv_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit


        If dgv.Rows.Count = 1 Then
            Unique.Add(dgv.Rows(0).Cells(0).Value.ToString) 'this just ensures that there's no null exception
        End If


        For i = 0 To dgv.Rows.Count - 2
            If Not Unique.Contains(dgv.Rows(i).Cells(0).Value.ToString) Then 'if value is new to the grid add to unique values list
                Unique.Add(dgv.Rows(i).Cells(0).Value.ToString)
            End If
        Next


        For i = 0 To Unique.Count - 1 'match all the unique values ...
            c = 0
            For n = 0 To dgv.Rows.Count - 2 'with all the entries
                If dgv.Rows(n).Cells(0).Value.ToString = Unique(i) Then
                    c = c + 1 'how many times unique entry appears
                    If c > 1 Then
                        dgv.Rows(n).Cells(1).Value = ChrW(Asc("a") + c - 2).ToString 'add suffix
                    End If
                End If
            Next
        Next


    End Sub
 

Latest posts

Back
Top