In the previous steps you set up the application's user interface and added C1NumberBox, Label, and Button controls to your application. In this step you'll add code to your application to finalize it.
Complete the following steps:
1. Double-click Button1 to switch to Code view and create the Button1_Click event handler.
2. Add the following imports statements to the top of the page:
Imports C1.WPF
Imports System.Windows.Media
Imports System.Diagnostics
•C#
using C1.WPF;
using System.Windows.Media;
using System.Diagnostics;
3. Initialize the following global variables just inside class Window1:
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 code to the Button1_Click event handler so that it appears like the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Process.Start("http://www.componentone.com")
End Sub
•C#
private void button1_Click(object sender, RoutedEventArgs e)
{
Process.Start("http://www.componentone.com");
}
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.C1NumericBox1.Value = nb1 And Me.C1NumericBox2.Value = nb2 And Me.C1NumericBox3.Value = nb3 And Me.C1NumericBox4.Value = nb4 And Me.C1NumericBox5.Value = nb5 Then
Me.Label1.Foreground = Brushes.Green
Me.Label1.Content = "Combination Valid"
Me.C1NumericBox1.IsReadOnly = True
Me.C1NumericBox2.IsReadOnly = True
Me.C1NumericBox3.IsReadOnly = True
Me.C1NumericBox4.IsReadOnly = True
Me.C1NumericBox5.IsReadOnly = True
Me.Button1.Visibility = Windows.Visibility.Visible
End If
End Sub
•C#
private void NBValidation()
{
if (this.c1NumericBox1.Value == nb1 & this.c1NumericBox2.Value == nb2 & this.c1NumericBox3.Value == nb3 & this.c1NumericBox4.Value == nb4 & this.c1NumericBox5.Value == nb5)
{
this.label2.Foreground = Brushes.Green;
this.label2.Content = "Combination Valid";
this.c1NumericBox1.IsReadOnly = true;
this.c1NumericBox2.IsReadOnly = true;
this.c1NumericBox3.IsReadOnly = true;
this.c1NumericBox4.IsReadOnly = true;
this.c1NumericBox5.IsReadOnly = true;
this.button1.Visibility = 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. Choose View | Designer to return to Design view.
7. Click C1NumericBox1 to select it, and navigate to the Properties window.
8. Click the Events (lightning bolt) button on the Properties window to view events.
9. Double-click the box next to the ValueChanged event. This will switch to Code view and create the C1NumericBox1_ValueChanged event handler.
10. Enter the code in the C1NumericBox1_ValueChanged event handler to initialize NBValidation. It will look like the following:
Private Sub C1NumericBox1_ValueChanged(ByVal sender As System.Object, ByVal e As C1.WPF.PropertyChangedEventArgs(Of System.Double)) Handles C1NumericBox1.ValueChanged
NBValidation()
End Sub
•C#
private void c1NumericBox1_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
{
NBValidation();
}
11. Repeat steps 6 to 9 for each additional C1NumericBox control so that NBValidation is initialized in all five.
In this step you completed adding code to your application. In the next step you'll run the application and observe run-time interactions.