How to separate Serial Number into 4 TextBoxes

DoJo

New member
Joined
Feb 8, 2020
Messages
2
Programming Experience
3-5
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?

VB.NET:
    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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,250
Location
Sydney, Australia
Programming Experience
10+
I would handle the TextChanged event of the first TextBox, get the Text property, split on the dash and then populate the TextBoxes from the results. Fairly simple stuff; about 5 lines of code.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
You are close. This will give you something to play with. Try this :
VB.NET:
    Public Function Split_Serial(serial As String) As String
        Dim serial_parts As String() = serial.Split(CType("-", Char())).ToArray
        For i = 0 To serial_parts.Length - 1
            Select Case i
                Case 0
                    TextBox2.Text = serial_parts(0)
                Case 1
                    TextBox3.Text = serial_parts(1)
                Case 2
                    TextBox4.Text = serial_parts(2)
                Case 3
                    TextBox5.Text = serial_parts(3)
            End Select
        Next
        Return serial
    End Function
    Dim serial_Collection As String() = {"E8V7-J3MK-K3D4-D255"}
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Split_Serial(TextBox1.Text).Equals(serial_Collection(0)) 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
Separately, you can also do something like this using John's idea with the TextChanged event :
VB.NET:
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text.Length = 19 AndAlso Split_Serial(TextBox1.Text).Equals(serial_Collection(0)) Then

            'Validate the serial complete

        End If

    End Sub
You should also be writing with : Option Strict On AND Option Explicit On. To enable these options, put this at the top of your code file :
VB.NET:
Option Strict On
Option Explicit On
You may see a lot of errors, depending how explicit you write your programs. For best practices, you should have those options enabled.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
Hmm additionally you could (using Linq), remove the switch and parse the splits individually to each textbox using where, select, to, from. Your function needs a little more work. Also validate each of the 4 textboxes to make sure they each don't exceed the required length for each split. ;)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,250
Location
Sydney, Australia
Programming Experience
10+
You should also be writing with : Option Strict On AND Option Explicit On. To enable these options, put this at the top of your code file :
VB.NET:
Option Strict On
Option Explicit On
You may see a lot of errors, depending how explicit you write your programs. For best practices, you should have those options enabled.
Option Explicit is On by default, so pretty much no one should have to actually turn it On. Option Strict is Off by default, but really ought to be On. To that end, I would suggest that people turn it On in the project properties, so it is On for every file. I would also suggest turning it On in the IDE options, so that it will be On by default for future projects.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
Yea that's correct as per each doc states : explicit & strict, but Isn't it a pity that Microsoft didn't make Option Strict On a required option. I've always thought so. Being a fairly explicit programmer, I'd still ensure Option Explicit was in the file regardless and make sure it was also on too.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,250
Location
Sydney, Australia
Programming Experience
10+
Isn't it a pity that Microsoft didn't make Option Strict On a required option. I've always thought so.
I do agree but I think that it defaults to Off for two reasons. Firstly, they wanted VB6 code to have to change as little as possible when being migrated to VB.NET. Having to fix all the errors generated by Option Strict On when migrating from VB6 would have made the job that much more difficult. Of course, VB6 developers could just have turned it Off themselves but so many of them were such a bunch of whiners at the time anyway, Microsoft probably didn't want to piss them off any further. Secondly, VB is supposed to be an easier language to pick up and use that C# and the like and Option Strict Off helps with that. It means that beginners can write code that will actually do things without having to have a full understanding of data types and their differences. That may not necessarily be a good thing in the long run but it probably means that fewer people are put off so soon.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
Firstly, they wanted VB6 code to have to change as little as possible when being migrated to VB.NET.
I'd be shocked if people are still porting old programs still to this day. As its very seldom I see posts asking such a thing, so maybe they should make it a required option now, as there are very few reasons today not to make it a requirement in my opinion.
VB6 developers could just have turned it Off themselves but so many of them were such a bunch of whiners at the time anyway
Lol! They still are a bunch of whiners. But the same argument could be made for making it so VB6 users could turn it off. But instead, today; Microsoft could make it so Option Strict was on by default, and set that way for each new project and file created.
It means that beginners can write code that will actually do things without having to have a full understanding of data types and their differences. That may not necessarily be a good thing in the long run but it probably means that fewer people are put off so soon.
I find it kinda irritating that Microsoft bend over backwards to try and make simple things simpler for clueless people. I know that may sound a bit condescending. But If some people can't grasp the correct way of doing something explicitly and correctly, then I think they should further their education professionally so that they have that better understanding by being tutored. And I say that because I don't think it's a good thing even in the short term to learn to write poor code, let alone in the long term. ;)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,250
Location
Sydney, Australia
Programming Experience
10+
I find it kinda irritating that Microsoft bend over backwards to try and make simple things simpler for clueless people. I know that may sound a bit condescending. But If some people can't grasp the correct way of doing something explicitly and correctly, then I think they should further their education professionally so that they have that better understanding by being tutored. And I say that because I don't think it's a good thing even in the short term to learn to write poor code, let alone in the long term. ;)
I don't disagree but let's remember that Microsoft is a company that wants people to use their products. If they don't make VB easy to use, rather than further their education so they can use it, people will just use something else.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
You make a good point. But John, it's not like there is a lot of competition for Microsoft in terms of OS's to compete against, since Windows is one of the most used OS's around the world, nor should their be much worry about people using an alternative platform. Unless devs want to target only Linux, and mostly use C++C#, Or objective C for Mac? I don't think there is much to worry about.

I also think its fair to say that VB.Net was a good target for wanting to build on Winforms, and its not like Microsoft are doing alot to keep that going with their being so much drive gone behind WPF now anyway and Winforms reaching EOL. I think if ever there was a good time to make option strict required; it be now...
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
A proposed solution to this argument would be to allow a VB6 dev to paste non strict code into the IDE and then the IDE could also interpret any VB6 like code usage and just like with the bulb icon on VS which hints at suggestions, you would have recommendations for converting or casting between different data types etc. Option strict doesn't necessarily have to be on, but it could at least show a suggestion for what the equivalent of any VB6 like code would be in VB.Net. A feature like this, is typically only a mild extension of the IDE for VB.Net users and it would serve VB6 users and to not restrict code from being compiled regardless of the option strict setting, but it would also show the developer how best a function/method/property/struct etc should be formatted in VB.Net thus helping them with the conversion and learning process of writing strict code. Why hasn't this been done years ago? Eh Could you see something like this working?
 

DoJo

New member
Joined
Feb 8, 2020
Messages
2
Programming Experience
3-5
You are close. This will give you something to play with. Try this :
VB.NET:
    Public Function Split_Serial(serial As String) As String
        Dim serial_parts As String() = serial.Split(CType("-", Char())).ToArray
        For i = 0 To serial_parts.Length - 1
            Select Case i
                Case 0
                    TextBox2.Text = serial_parts(0)
                Case 1
                    TextBox3.Text = serial_parts(1)
                Case 2
                    TextBox4.Text = serial_parts(2)
                Case 3
                    TextBox5.Text = serial_parts(3)
            End Select
        Next
        Return serial
    End Function
    Dim serial_Collection As String() = {"E8V7-J3MK-K3D4-D255"}
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Split_Serial(TextBox1.Text).Equals(serial_Collection(0)) 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
Separately, you can also do something like this using John's idea with the TextChanged event :
VB.NET:
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text.Length = 19 AndAlso Split_Serial(TextBox1.Text).Equals(serial_Collection(0)) Then

            'Validate the serial complete

        End If

    End Sub
You should also be writing with : Option Strict On AND Option Explicit On. To enable these options, put this at the top of your code file :
VB.NET:
Option Strict On
Option Explicit On
You may see a lot of errors, depending how explicit you write your programs. For best practices, you should have those options enabled.
Thank you very much for everything guys, I'm a begginer using VB.NET and actually I couldn't understand what exactly you guys did at all...

And for some reason, when I copy the serial and paste inside the first TextBox, the Text still not filling all the other TextBoxes, also, the Application still saying that the serial is incorrect, like it wasn't checking all the TextBoxes.

Observation: There's no TextBox5 in my code.

@Edit

Using this on the top of my Code:

VB.NET:
Option Strict On
Option Explicit On

I got these two erros: BC30210, BC32013

Code:
BC30210 Visual Basic AND  VB.NET Option Strict On requires all Function, Property, and Operator declarations to have an 'As' clause.
BC32013 Visual Basic AND  VB.NET Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.
 
Last edited:

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
75
Location
UK
Programming Experience
10+
I don't know what your controls are. You never said, so I improvised. What I gave you was a working example.

Consider that if the switch has four parts, then you need four additional textboxes or string variables to equate to the four different parts of the serial :
VB.NET:
            Select Case i
                Case 0
                    TextBox2.Text = serial_parts(0)
                Case 1
                    TextBox3.Text = serial_parts(1)
                Case 2
                    TextBox4.Text = serial_parts(2)
                Case 3
                    TextBox5.Text = serial_parts(3)
            End Select
I got these two erros: BC30210, BC32013
The first one is because your functions on your code file likely look like this :
Public Function Split_Serial(serial As String)

They should look like this :
Public Function Split_Serial(serial As String) As String

At the end of a function, option strict requires you to specify explicitly what the function operates as.
And show the line of code you get this on :
BC32013 Visual Basic AND VB.NET Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.
Those errors are really self explanatory. Perhaps, @jmcilhinney will explain why these appear as errors after enabling those options?
 
Top Bottom