In the previous steps you set up the application's user interface and added five C1NumericBox controls to the application. In this step you'll add code to your application to finalize the application.
Complete the following steps:
1. Select View | Code to switch to Code view.
2. Add the following imports statements to the top of the page:
Imports C1.Phone
Imports System.Windows.Media
Imports Microsoft.Phone.Tasks
•C#
using C1.Phone;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
3. Initialize the following global variables just inside the MainPage class:
Dim nb1 As Integer = 5
Dim nb2 As Integer = 2
Dim nb3 As Integer = 3
Dim nb4 As Integer = 7
Dim nb5 As Integer = 9
•C#
int nb1 = 5;
int nb2 = 2;
int nb3 = 3;
int nb4 = 7;
int nb5 = 9;
These numbers will be used as the correct 'code' in the application. When the user enters the correct combination of numbers at run time the button will appear.
4. Add the following Click event handler to the code:
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn1.Click
Dim task As New WebBrowserTask()
task.Uri = New Uri("http://www.componentone.com/")
task.Show()
End Sub
•C#
private void btn1_Click(object sender, RoutedEventArgs e)
{
WebBrowserTask task = new WebBrowserTask();
task.Uri = new Uri("http://www.componentone.com/");
task.Show();
}
When the button is pressed at run time it will open the ComponentOne Web site.
5. Next add the following custom NBValidation event to your code:
Private Sub NBValidation()
If Me.c1nb1.Value = nb1 And Me.c1nb2.Value = nb2 And Me.c1nb3.Value = nb3 And Me.c1nb4.Value = nb4 And Me.c1nb5.Value = nb5 Then
Me.tb2.Foreground = New SolidColorBrush(Colors.Green)
Me.tb2.Text = "Combination Valid"
Me.c1nb1.IsReadOnly = True
Me.c1nb2.IsReadOnly = True
Me.c1nb3.IsReadOnly = True
Me.c1nb4.IsReadOnly = True
Me.c1nb5.IsReadOnly = True
Me.btn1.Visibility = Windows.Visibility.Visible
End If
End Sub
•C#
private void NBValidation()
{
if (this.c1nb1.Value == nb1 & this.c1nb2.Value == nb2 & this.c1nb3.Value == nb3 & this.c1nb4.Value == nb4 & this.c1nb5.Value == nb5)
{
this.tb2.Foreground = new SolidColorBrush(Colors.Green);
this.tb2.Text = "Combination Valid";
this.c1nb1.IsReadOnly = true;
this.c1nb2.IsReadOnly = true;
this.c1nb3.IsReadOnly = true;
this.c1nb4.IsReadOnly = true;
this.c1nb5.IsReadOnly = true;
this.btn1.Visibility = Windows.Visibility.Visible;
}
}
When the user enters the correct numbers (as indicated in step 3 above) the C1NumericBox controls will be set to read only and will no longer be editable, the text of the label below the controls will change to indicate the correct code has been entered, and a button will appear allowing users to enter the ComponentOne Web site.
6. Add C1NumericBox_ValueChanged event handlers to initialize NBValidation. The code will look like the following:
Private Sub c1nb1_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Phone.PropertyChangedEventArgs(Of System.Double)) Handles c1nb1.ValueChanged
NBValidation()
End Sub
Private Sub c1nb2_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Phone.PropertyChangedEventArgs(Of System.Double)) Handles c1nb2.ValueChanged
NBValidation()
End Sub
Private Sub c1nb3_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Phone.PropertyChangedEventArgs(Of System.Double)) Handles c1nb3.ValueChanged
NBValidation()
End Sub
Private Sub c1nb4_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Phone.PropertyChangedEventArgs(Of System.Double)) Handles c1nb4.ValueChanged
NBValidation()
End Sub
Private Sub c1nb5_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Phone.PropertyChangedEventArgs(Of System.Double)) Handles c1nb5.ValueChanged
NBValidation()
End Sub
•C#
private void c1nb1_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
private void c1nb2_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
private void c1nb3_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
private void c1nb4_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
private void c1nb5_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
In this step you completed adding code to your application. In the next step you'll run the application and observe run-time interactions.