Step 3 of 4: Adding Code to the Application
In This Topic
In the previous steps you set up the application's user interface and added C1NumberBox, TextBlock, and Button controls to your application. In this step you'll add code to your application to finalize it.
Complete the following steps:
- Select View | Code to switch to Code view.
- Add the following imports statements to the top of the page:
Visual Basic |
Copy Code
|
Imports Windows.UI.Xaml.Media
Imports Windows.UI.Xaml.Navigation
Imports Windows.UI
Imports C1.Xaml
|
C# |
Copy Code
|
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI;
using C1.Xaml;
|
- Initialize the following global variables just inside the MainPage class:
Visual Basic |
Copy Code
|
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# |
Copy Code
|
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.
- Add code to the Button1_Click event handler so that it appears like the following:
Visual Basic |
Copy Code
|
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn1.Click
DefaultLaunch()
End Sub
|
C# |
Copy Code
|
private void btn1_Click(object sender, RoutedEventArgs e)
{
DefaultLaunch();
}
|
- Add the following code just below the Button1_Click event handler:
Visual Basic |
Copy Code
|
async Sub DefaultLaunch()
' The URI to launch
Dim uri As New Uri("https://developer.mescius.com")
' Launch the URI
Dim success = await Windows.System.Launcher.LaunchUriAsync(uri)
If success Then
' URI launched
Else
' URI launch failed
End If
End Sub
|
C# |
Copy Code
|
async void DefaultLaunch()
{
// The URI to launch
string uriToLaunch = @"https://developer.mescius.com";
var uri = new Uri(uriToLaunch);
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
|
When the button is pressed at run time it will open the ComponentOne Web site.
- Next add the following custom NBValidation event to your code:
Visual Basic |
Copy 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 = Visibility.Visible
End If
End Sub
|
C# |
Copy Code
|
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 = 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.
- Add C1NumericBox_ValueChanged event handlers to initialize NBValidation. The code will look like the following:
Visual Basic |
Copy Code
|
Private Sub c1nb1_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Xaml.PropertyChangedEventArgs(Of System.Double)) Handles c1nb1.ValueChanged
NBValidation()
End Sub
Private Sub c1nb2_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Xaml.PropertyChangedEventArgs(Of System.Double)) Handles c1nb2.ValueChanged
NBValidation()
End Sub
Private Sub c1nb3_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Xaml.PropertyChangedEventArgs(Of System.Double)) Handles c1nb3.ValueChanged
NBValidation()
End Sub
Private Sub c1nb4_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Xaml.PropertyChangedEventArgs(Of System.Double)) Handles c1nb4.ValueChanged
NBValidation()
End Sub
Private Sub c1nb5_ValueChanged(ByVal sender As System.Object, ByVal e As C1.Xaml.PropertyChangedEventArgs(Of System.Double)) Handles c1nb5.ValueChanged
NBValidation()
End Sub
|
C# |
Copy Code
|
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.
See Also