Trial PeriodHow to add a two week trial to your applicationRate Topic:

This tutorial will teach you how to add a trial period to your program, allowing you to offer potential customers a chance to test out your application before buying it. Although this sounds very complicated, it is actually very simple. For the purposes of this tutorial, I will be using Visual Basic 2008 Express Edition, but there is no reason why this will not work in a 2005 version of Visual Studio.

First of all, you will need to declare the required variables. For these you will need the following:

2 Date type variables - I named them dteStartDate and dteLastStart. dteStartDate is when the trial program is first installed and ran, dteLastStart is the date the program was last ran.

2 Boolean type variables - blnEnabled and blnFirstTime. blnEnabled keeps track of if the trial is still active or should be disabled. blnFirstTime will come into effect into part 2 of the tutorial (Adding a registry key to stop the trial being ran more than once), but I need more study before I can do that part.

1 Long type variable - lngTimeLeft. Keeps track of how many days remain in the trial.

1 Integer type variable - intTime. Keeps an eye out for people making changes to the system clock to extend the trial.

At this point, you should also set intTime to '1', blnEnabled to 'True', and make sure dteStartDate is assigned the date the program is first ran. Then add a statement telling your application to save it's settings when the program is closed.

Our next step is to add a few lines of code designed to stop people changing the system clock to extend the trial. Eventually, this will be coupled with a registry entry to disallow multiple installs of the demo, but as I have already mentioned, I need to look into that a bit further before I put anything on here.

If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
'First clock change
If intTime = 1 Then
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
intTime = 2
ElseIf intTime = 2 Then
'Second clock change
blnEnabled = False
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
End If
'disables app
If blnEnabled = False Then
If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If

So, first of all, check the number of days between the date the application was last ran, and today's date according to the system clock. If today's date is before the date the application was last ran, then the program will issue a warning, basically saying 'We know you changed your clock to an earlier date. Because this is the first time you've done this, we'll be nice and give you a warning. Don't do it again". Then it changes the intTime variable to 2 in case they try this trick a second time. This code can easily be changed to give more or fewer warnings before disabling the trial. All you will need to do is move the line 'blnEnabled = False' to another If statement, and remember to keep adding 1 to intTime for every warning you wish to give the user.

For this example, we only give the 1 warning, so if the user tries changing the system clock again, it will disable the application and givbe a message basically saying 'We warned you". Then it offers a second messagebox telling the user it has been disabled, then closes any open forms.

Finally, the last piece of code we need is that which disables the application at the end of the trial. This part can be modified to change the length of the trial to whatever you see fit. Just change the '14' (all 3 of them in this piece of code) to however many days you want the user to trial your application.

If DateDiff(DateInterval.Day, dteStartDate, Now) > 14 Then
blnEnabled = False
If blnEnabled = False Then
If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
'Close all open forms
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
dteLastStart = Now
If blnFirstTime = True Then
blnFirstTime = False
End If
'Saves variable settings
My.Settings.Save()
lngTimeLeft = 14 - (DateDiff(DateInterval.Day, dteStartDate, Now))
MsgBox("This is a 14-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")

The very end records the date the program is ran, saves the settings, and gives a messagebox telling the user how many days are left.

If you have any questions or comments about this, please post them here.

At the moment, they could do that. I'm currently working on another two tutorials to help prevent this and generally make it more secure. The first is about encryption, and the second is how to add, edit, and remove registry entries, so between them, they should be able to make a trial version of a product fairly safe from tampering.

I have been looking at encryption over the last few days, and it is actually surprisingly simple. I will put a link to it from here when it is ready.

Also, having the registry entries means that when someone with the trial version of a program buys the full version, we can simply add a few lines of code to the full version which will edit (or remove) the registry entry.

Just an FYI.. the my.settings data is saved not in the registry per say but at this path:
C:\Documents and Settings\<user name>\Local Settings\Apps and down from there. We use it all the time at work and if something gets munged up we have users delete the data below that path for what ever project they are calling in on. Essentially it would short circuit your logic there and treat it as if it's the first time using it.

No big whoop - you average user wouldn't know about it.. but it's there.

Just an FYI.. the my.settings data is saved not in the registry per say but at this path:
C:\Documents and Settings\<user name>\Local Settings\Apps and down from there. We use it all the time at work and if something gets munged up we have users delete the data below that path for what ever project they are calling in on. Essentially it would short circuit your logic there and treat it as if it's the first time using it.

No big whoop - you average user wouldn't know about it.. but it's there.

This tutorial will teach you how to add a trial period to your program, allowing you to offer potential customers a chance to test out your application before buying it. Although this sounds very complicated, it is actually very simple. For the purposes of this tutorial, I will be using Visual Basic 2008 Express Edition, but there is no reason why this will not work in a 2005 version of Visual Studio.

First of all, you will need to declare the required variables. For these you will need the following:

2 Date type variables - I named them dteStartDate and dteLastStart. dteStartDate is when the trial program is first installed and ran, dteLastStart is the date the program was last ran.

2 Boolean type variables - blnEnabled and blnFirstTime. blnEnabled keeps track of if the trial is still active or should be disabled. blnFirstTime will come into effect into part 2 of the tutorial (Adding a registry key to stop the trial being ran more than once), but I need more study before I can do that part.

1 Long type variable - lngTimeLeft. Keeps track of how many days remain in the trial.

1 Integer type variable - intTime. Keeps an eye out for people making changes to the system clock to extend the trial.

At this point, you should also set intTime to '1', blnEnabled to 'True', and make sure dteStartDate is assigned the date the program is first ran. Then add a statement telling your application to save it's settings when the program is closed.

Our next step is to add a few lines of code designed to stop people changing the system clock to extend the trial. Eventually, this will be coupled with a registry entry to disallow multiple installs of the demo, but as I have already mentioned, I need to look into that a bit further before I put anything on here.

If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
'First clock change
If intTime = 1 Then
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
intTime = 2
ElseIf intTime = 2 Then
'Second clock change
blnEnabled = False
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
End If
'disables app
If blnEnabled = False Then
If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If

So, first of all, check the number of days between the date the application was last ran, and today's date according to the system clock. If today's date is before the date the application was last ran, then the program will issue a warning, basically saying 'We know you changed your clock to an earlier date. Because this is the first time you've done this, we'll be nice and give you a warning. Don't do it again". Then it changes the intTime variable to 2 in case they try this trick a second time. This code can easily be changed to give more or fewer warnings before disabling the trial. All you will need to do is move the line 'blnEnabled = False' to another If statement, and remember to keep adding 1 to intTime for every warning you wish to give the user.

For this example, we only give the 1 warning, so if the user tries changing the system clock again, it will disable the application and givbe a message basically saying 'We warned you". Then it offers a second messagebox telling the user it has been disabled, then closes any open forms.

Finally, the last piece of code we need is that which disables the application at the end of the trial. This part can be modified to change the length of the trial to whatever you see fit. Just change the '14' (all 3 of them in this piece of code) to however many days you want the user to trial your application.

If DateDiff(DateInterval.Day, dteStartDate, Now) > 14 Then
blnEnabled = False
If blnEnabled = False Then
If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
'Close all open forms
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
dteLastStart = Now
If blnFirstTime = True Then
blnFirstTime = False
End If
'Saves variable settings
My.Settings.Save()
lngTimeLeft = 14 - (DateDiff(DateInterval.Day, dteStartDate, Now))
MsgBox("This is a 14-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")

The very end records the date the program is ran, saves the settings, and gives a messagebox telling the user how many days are left.

If you have any questions or comments about this, please post them here.

This tutorial will teach you how to add a trial period to your program, allowing you to offer potential customers a chance to test out your application before buying it. Although this sounds very complicated, it is actually very simple. For the purposes of this tutorial, I will be using Visual Basic 2008 Express Edition, but there is no reason why this will not work in a 2005 version of Visual Studio.

First of all, you will need to declare the required variables. For these you will need the following:

2 Date type variables - I named them dteStartDate and dteLastStart. dteStartDate is when the trial program is first installed and ran, dteLastStart is the date the program was last ran.

2 Boolean type variables - blnEnabled and blnFirstTime. blnEnabled keeps track of if the trial is still active or should be disabled. blnFirstTime will come into effect into part 2 of the tutorial (Adding a registry key to stop the trial being ran more than once), but I need more study before I can do that part.

1 Long type variable - lngTimeLeft. Keeps track of how many days remain in the trial.

1 Integer type variable - intTime. Keeps an eye out for people making changes to the system clock to extend the trial.

At this point, you should also set intTime to '1', blnEnabled to 'True', and make sure dteStartDate is assigned the date the program is first ran. Then add a statement telling your application to save it's settings when the program is closed.

Our next step is to add a few lines of code designed to stop people changing the system clock to extend the trial. Eventually, this will be coupled with a registry entry to disallow multiple installs of the demo, but as I have already mentioned, I need to look into that a bit further before I put anything on here.

If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
'First clock change
If intTime = 1 Then
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
intTime = 2
ElseIf intTime = 2 Then
'Second clock change
blnEnabled = False
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
End If
'disables app
If blnEnabled = False Then
If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If

So, first of all, check the number of days between the date the application was last ran, and today's date according to the system clock. If today's date is before the date the application was last ran, then the program will issue a warning, basically saying 'We know you changed your clock to an earlier date. Because this is the first time you've done this, we'll be nice and give you a warning. Don't do it again". Then it changes the intTime variable to 2 in case they try this trick a second time. This code can easily be changed to give more or fewer warnings before disabling the trial. All you will need to do is move the line 'blnEnabled = False' to another If statement, and remember to keep adding 1 to intTime for every warning you wish to give the user.

For this example, we only give the 1 warning, so if the user tries changing the system clock again, it will disable the application and givbe a message basically saying 'We warned you". Then it offers a second messagebox telling the user it has been disabled, then closes any open forms.

Finally, the last piece of code we need is that which disables the application at the end of the trial. This part can be modified to change the length of the trial to whatever you see fit. Just change the '14' (all 3 of them in this piece of code) to however many days you want the user to trial your application.

If DateDiff(DateInterval.Day, dteStartDate, Now) > 14 Then
blnEnabled = False
If blnEnabled = False Then
If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
'Close all open forms
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
dteLastStart = Now
If blnFirstTime = True Then
blnFirstTime = False
End If
'Saves variable settings
My.Settings.Save()
lngTimeLeft = 14 - (DateDiff(DateInterval.Day, dteStartDate, Now))
MsgBox("This is a 14-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")

The very end records the date the program is ran, saves the settings, and gives a messagebox telling the user how many days are left.

If you have any questions or comments about this, please post them here.

Happy coding,
Bort

[quote]
Hello Bort, how you doing? I like this tutorial and is very handy whenever I create an application that I know I can sell I can also use the trial period code. This is cool. That is why I like programming it is fun. Well easy, I really wouldn't its easy but challenging, my opinion. LOL
However, I like this site I can learn a lot from here. I am currently in college and my last college professor really didn't teach me anything in the advanced class. I study on my own and practice the coding so I can be more fluent and proficient by the time I graduate. I am 50 years old and been a carpenter for over 20 years and due to the construction industry slowing down I decided to change my career to IT/Programming. I like Visual Basic and the making of software windows apps. Thanks for your time making a tutorial I will learn from it. I was looking at the DateDiff which that is what I had to use for the Sql server class for a video store cool snippets to add. Thanks again, Bort. sorry for he long speech.

This tutorial will teach you how to add a trial period to your program, allowing you to offer potential customers a chance to test out your application before buying it. Although this sounds very complicated, it is actually very simple. For the purposes of this tutorial, I will be using Visual Basic 2008 Express Edition, but there is no reason why this will not work in a 2005 version of Visual Studio.

First of all, you will need to declare the required variables. For these you will need the following:

2 Date type variables - I named them dteStartDate and dteLastStart. dteStartDate is when the trial program is first installed and ran, dteLastStart is the date the program was last ran.

2 Boolean type variables - blnEnabled and blnFirstTime. blnEnabled keeps track of if the trial is still active or should be disabled. blnFirstTime will come into effect into part 2 of the tutorial (Adding a registry key to stop the trial being ran more than once), but I need more study before I can do that part.

1 Long type variable - lngTimeLeft. Keeps track of how many days remain in the trial.

1 Integer type variable - intTime. Keeps an eye out for people making changes to the system clock to extend the trial.

At this point, you should also set intTime to '1', blnEnabled to 'True', and make sure dteStartDate is assigned the date the program is first ran. Then add a statement telling your application to save it's settings when the program is closed.

Our next step is to add a few lines of code designed to stop people changing the system clock to extend the trial. Eventually, this will be coupled with a registry entry to disallow multiple installs of the demo, but as I have already mentioned, I need to look into that a bit further before I put anything on here.

If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
'First clock change
If intTime = 1 Then
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
intTime = 2
ElseIf intTime = 2 Then
'Second clock change
blnEnabled = False
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
End If
'disables app
If blnEnabled = False Then
If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If

So, first of all, check the number of days between the date the application was last ran, and today's date according to the system clock. If today's date is before the date the application was last ran, then the program will issue a warning, basically saying 'We know you changed your clock to an earlier date. Because this is the first time you've done this, we'll be nice and give you a warning. Don't do it again". Then it changes the intTime variable to 2 in case they try this trick a second time. This code can easily be changed to give more or fewer warnings before disabling the trial. All you will need to do is move the line 'blnEnabled = False' to another If statement, and remember to keep adding 1 to intTime for every warning you wish to give the user.

For this example, we only give the 1 warning, so if the user tries changing the system clock again, it will disable the application and givbe a message basically saying 'We warned you". Then it offers a second messagebox telling the user it has been disabled, then closes any open forms.

Finally, the last piece of code we need is that which disables the application at the end of the trial. This part can be modified to change the length of the trial to whatever you see fit. Just change the '14' (all 3 of them in this piece of code) to however many days you want the user to trial your application.

If DateDiff(DateInterval.Day, dteStartDate, Now) > 14 Then
blnEnabled = False
If blnEnabled = False Then
If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
'Close all open forms
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
dteLastStart = Now
If blnFirstTime = True Then
blnFirstTime = False
End If
'Saves variable settings
My.Settings.Save()
lngTimeLeft = 14 - (DateDiff(DateInterval.Day, dteStartDate, Now))
MsgBox("This is a 14-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")

The very end records the date the program is ran, saves the settings, and gives a messagebox telling the user how many days are left.

If you have any questions or comments about this, please post them here.