How to create an alphanumeric counter on vb.net?

1

Hello, I have a question about the realization of an incremental alphanumeric counter in visual basic.net any idea of how it could be done?

The counter format would be as follows:

PRE-001/13/12/2016

This is a simple numerical counter but I can not adapt it with another type of data like string, rather I do not have an idea of how to do it in the event load of form1 each time the program will be automatically increase in the number one by one and capture the current date:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As Integer
Dim sufijo As String
Dim fecha As Date
sufijo= "PRE"
fecha =  DateTime.Now.ToString("dd/MM/yyyy") 
        con = 0
        con = con + 1
        txtnumero.Text = sufijo+con+fecha

 End Sub

Including the word PRE at the beginning, the number of the generated budget (which is the one that will increase one by one) followed by the creation date is this possible in vb.net ?

    
asked by Jcayala 13.12.2016 в 17:37
source

3 answers

2

With this code you can generate a string like this:

  

PRE-001/14/12/2016

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim con As Integer
    Dim sufijo As String
    Dim fecha As Date
    sufijo = "PRE-"
    fecha = DateTime.Now.ToString("dd/MM/yyyy")
    con = 0
    con = con + 1
    Dim out As String = con.ToString("000")
    txtNumero.Text = sufijo & out & "/" & fecha
  End Sub
    
answered by 14.12.2016 в 23:10
1

Based on your need, I suggest that you save the prefix and the consecutive in app.config, I'll attach the example code to give you an idea:

You need to add the reference of: System.Configuration

Imports System.Configuration

The code in the form would be:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim prefijo, consecutivo As String
        prefijo = LeerAppConfig("prefijo")
        consecutivo = LeerAppConfig("consecutivo")

        Dim cons As Integer = Integer.Parse(IIf(consecutivo = "", "0", consecutivo))
        cons = cons + 1

        txtFolio.Text = prefijo + "-" + cons.ToString("000") + "/" + DateTime.Now.ToString("dd/MM/yyyy")

        ''Guardo en app.config
        GuardarAppConfig("consecutivo",cons)

    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

private function LeerAppConfig(key As String) as string
        Dim result As String = ""
    Try
        Dim appSettings = ConfigurationManager.AppSettings
        result= appSettings(key)
        If IsNothing(result) Then
            result = "Not found"
        End If
    Catch e As ConfigurationErrorsException
        Console.WriteLine("Error reading app settings")
    End Try
        return result
End function

private sub GuardarAppConfig(key As String, value As String) 
    Try
        Dim configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Dim settings = configFile.AppSettings.Settings
        If IsNothing(settings(key)) Then
            settings.Add(key, value)
        Else
            settings(key).Value = value
        End If
        configFile.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name)
    Catch e As ConfigurationErrorsException
        Console.WriteLine("Error writing app settings")
    End Try
End sub

and your app.config file would look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <appSettings>
    <add key="prefijo" value="PRE" />
    <add key="consecutivo" value="0" />
  </appSettings>
</configuration>

Annex images:

    
answered by 15.12.2016 в 00:56
1

Well, your idea does not seem very complicated but it is somewhat confusing. First the counter should be stored somewhere (For example: database, text files, Excell books, etc.) or if you do not have to keep it good you should store it in a variable with a larger scope. That is, for what you write, it seems that you plan to close the form and when you open it generate the new counter, if this is so, you could create a Module and there a variable called counter and then increase it by accessing it every time You see it convenient.

Maybe you could explain the subject a bit more and be able to help you more accurately.

    
answered by 13.12.2016 в 22:27