none
Auto generate ID, after user restart program it can not continues to next number RRS feed

  • Question

  • Dim ids As List(Of Integer) = New List(Of Integer)()
            For i As Integer = 1 To 100
                ids.Add(i)
            Next

            Dim idstring As String = "INV" & ids(count).ToString().PadLeft(4, "0"c)
            Textbox1.Text = idstring
            count += 1

    for example INV001 ,INV002,when user restart the program the ID will recalculate from to INV001. is i write wrongly something


    • Edited by christing Tuesday, August 6, 2019 4:56 AM
    Tuesday, August 6, 2019 4:53 AM

Answers

  • Hi,

    add an ini file:


    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public Class Form1
        Dim count As Integer
        Dim ids As List(Of Integer) = New List(Of Integer)()
        Dim fileini As String = "D:\count.ini"
        <DllImport("kernel32")>
        Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
        End Function
        <DllImport("kernel32")>
        Private Shared Function WritePrivateProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i As Integer = 1 To 100
                ids.Add(i)
            Next
            Dim sb As StringBuilder = New StringBuilder(50)
            GetPrivateProfileString("Myini", "count", "", sb, 50, fileini)
            count = CInt(sb.ToString())
            Dim idstring As String = "INV" & ids(count).ToString().PadLeft(4, "0"c)
            TextBox1.Text = idstring
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            count += 1
    
            Dim idstring As String = "INV" & ids(count).ToString().PadLeft(4, "0"c)
            TextBox1.Text = idstring
        End Sub
    
        Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            WritePrivateProfileString("Myini", "count", count.ToString, fileini)
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Alex Li-MSFT Tuesday, August 6, 2019 7:44 AM
    • Marked as answer by christing Tuesday, August 6, 2019 9:28 AM
    Tuesday, August 6, 2019 6:16 AM