3 Replies - 218 Views - Last Post: 13 February 2020 - 05:58 AM Rate Topic: -----

#1 Envolvents   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-February 20

How to separate Serial Number into 4 TextBoxes

Posted 08 February 2020 - 12:13 AM

Hello community, so this is my problem, I have 4 TextBoxes and 1 "serial number". What I want is:

When the user pastes the serial number into the first TextBox, the String will be divided into 4 digits for each text box, thus leaving 16 digits in all, however, 4 in each TextBox.

And finally, I wanted a way to check the 4 text boxes to see if the text in them matches the text inside the String "Serial", all of this when clicking on the OK button (Button1_Click). The problem is that I have no idea how to do this, could someone help me with this?

    Function Validacao(serial As String)
        Dim Serial(15) As String
        Serial(0) = "E8V7-J3MK-K3D4-D255"

        For i = 0 To Serial.Count() - 1 Step 1
            If serial = Serial(i) Then
                Return True
            End If
        Next

        Return False
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Validacao(TextBox1.Text) = True Then
            MsgBox("Example Software is activated.", MsgBoxStyle.Information, "Congratulations!")
            My.Settings.SaveSettings = True
            My.Settings.Save()
            My.Settings.Reload()
            Dim dirSavePoint As New IO.DirectoryInfo("C:\ProgramData\SavePoint")
            If Not dirSavePoint.Exists Then
                My.Computer.FileSystem.CreateDirectory("C:\ProgramData\SavePoint")
            End If
            escreveINI("C:\ProgramData\SavePoint\Serial.INI", "Settings", "Serial", TextBox1.Text)
            Form3.Show()
            Me.Hide()
        Else
            MsgBox("Error! Your serial number is invalid.", MsgBoxStyle.Critical, "Error!")
        End If
    End Sub

End Class


Is This A Good Question/Topic? 0
  • +

Replies To: How to separate Serial Number into 4 TextBoxes

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15477
  • View blog
  • Posts: 62,007
  • Joined: 12-June 08

Re: How to separate Serial Number into 4 TextBoxes

Posted 08 February 2020 - 12:40 AM

Not ar my PC, but use string.split on the hyphen inside the text change event. For each string in the resulting collection put in each box.
Was This Post Helpful? 1
  • +
  • -

#3 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 394
  • View blog
  • Posts: 1,648
  • Joined: 07-September 13

Re: How to separate Serial Number into 4 TextBoxes

Posted 08 February 2020 - 01:16 AM

Personally, I would create a Serial Class and DataBind the UI, not going to show you how to do this, there are guides online or if you go down this route, if you decide to go down this route make a stab and ask another question and I am sure someone will help you.

I will however explain how I would handle the Serial Class, because you could technically do the same thing I am about to do but within the Text change events in all the textboxes, but i am not going to show you that because it will be more messy.

So the my serial class would have 5 properties:

  • SerialCode - Would be the full serial code, built up dynamically using string.Join (This could be a function in your form)
  • SerialCodeSection1
  • SerialCodeSection2
  • SerialCodeSection3
  • SerialCodeSection4



The first property would look like this:


Private _serialCodeSection1 As String
Public Property SerialCodeSection1() As String
Get
Return _serialCodeSection1
End Get
Set(ByVal value As String)
' only take either the first 4 character or the length of the value whichever is lowest
_serialCodeSection1 = New String(value.Take(Math.Min(4, value.Count)).ToArray())
If value.Count > 4 Then
' If the value is greater than 4, stick the rest in section 2.
' which will then take 4 and pass it on to section 3.
SerialCodeSection2 = New String(value.Skip(4).ToArray())
End If
End Set
End Property


        Private _serialCodeSection1 As String
        Public Property SerialCodeSection1() As String
            Get
                Return _serialCodeSection1
            End Get
            Set(ByVal value As String)
                Dim workingValue = value.Replace("-", "").Trim();

                ' only take either the first 4 character or the length of the value whichever is lowest
                _serialCodeSection1 = New String(workingValue.Take(Math.Min(4, workingValue.Count)).ToArray())
                If workingValue.Count > 4 Then
                    ' If the value is greater than 4, stick the rest in section 2.
                    ' which will then take 4 and pass it on to section 3. 
                    SerialCodeSection2 = New String(workingValue.Skip(4).ToArray())
                End If
            End Set
        End Property


Documentation about Skip
Documentation about Take
Documentation about Math.Min

The other properties will then mirror this property except changing the section number on the end.

Now there is a flaw with the code I have written and that is because I am not going to provide you will a full solution.

When I set the string across to SeriaCodeSection2 I will wipe out what I have already got there, is that what you want? this will effectively do this with fully implemented code:

1234-5678-9012-3456

and if I typed a 1 at the start:

1123-4--

With a slight tweak to the code you can make it so it will shunt the serial code up so it would do this:

1123-4567-8901-2345

Regards

Edit - Updated code to ignore spaces and hyphens

This post has been edited by maceysoftware: 08 February 2020 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 Envolvents   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-February 20

Re: How to separate Serial Number into 4 TextBoxes

Posted 13 February 2020 - 05:58 AM

View Postmaceysoftware, on 08 February 2020 - 01:16 AM, said:

Personally, I would create a Serial Class and DataBind the UI, not going to show you how to do this, there are guides online or if you go down this route, if you decide to go down this route make a stab and ask another question and I am sure someone will help you.

I will however explain how I would handle the Serial Class, because you could technically do the same thing I am about to do but within the Text change events in all the textboxes, but i am not going to show you that because it will be more messy.

So the my serial class would have 5 properties:

  • SerialCode - Would be the full serial code, built up dynamically using string.Join (This could be a function in your form)
  • SerialCodeSection1
  • SerialCodeSection2
  • SerialCodeSection3
  • SerialCodeSection4



The first property would look like this:


Private _serialCodeSection1 As String
Public Property SerialCodeSection1() As String
Get
Return _serialCodeSection1
End Get
Set(ByVal value As String)
' only take either the first 4 character or the length of the value whichever is lowest
_serialCodeSection1 = New String(value.Take(Math.Min(4, value.Count)).ToArray())
If value.Count > 4 Then
' If the value is greater than 4, stick the rest in section 2.
' which will then take 4 and pass it on to section 3.
SerialCodeSection2 = New String(value.Skip(4).ToArray())
End If
End Set
End Property


        Private _serialCodeSection1 As String
        Public Property SerialCodeSection1() As String
            Get
                Return _serialCodeSection1
            End Get
            Set(ByVal value As String)
                Dim workingValue = value.Replace("-", "").Trim();

                ' only take either the first 4 character or the length of the value whichever is lowest
                _serialCodeSection1 = New String(workingValue.Take(Math.Min(4, workingValue.Count)).ToArray())
                If workingValue.Count > 4 Then
                    ' If the value is greater than 4, stick the rest in section 2.
                    ' which will then take 4 and pass it on to section 3. 
                    SerialCodeSection2 = New String(workingValue.Skip(4).ToArray())
                End If
            End Set
        End Property


Documentation about Skip
Documentation about Take
Documentation about Math.Min

The other properties will then mirror this property except changing the section number on the end.

Now there is a flaw with the code I have written and that is because I am not going to provide you will a full solution.

When I set the string across to SeriaCodeSection2 I will wipe out what I have already got there, is that what you want? this will effectively do this with fully implemented code:

1234-5678-9012-3456

and if I typed a 1 at the start:

1123-4--

With a slight tweak to the code you can make it so it will shunt the serial code up so it would do this:

1123-4567-8901-2345

Regards

Edit - Updated code to ignore spaces and hyphens


Thank you @maceysoftware, I'm a begginer using VB.NET, I have about how to make something like this...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1