Windows Phone Controls: Part 2

Introduction

Welcome again! I hope you are learning with us, give us feedback, let us know about your opinion. In Windows Phone Controls – Part 1 we saw seven essential controls of Windows Phone. Today we'll learn about more common controls like Input controls. We'll mostly talk about TextBox, Date and Time Picker controls. So let's get started.

Working with TextBox Control

First of all, today I'm not going to explain how to open a project from the start. We've done this before. So, I'm moving up to the content right here. Use two TextBlocks and change the Text content to “First Name” & “Last Name”. Then take two TextBoxs, named “firstNameTextBox” & “lastNameTextBox” and take another two TextBlocks and name them “welcomeTextBlock” & “nameTextBlock”. Arrange them as in the picture below.

textbox control
                                             Figure 1

The design of the code is also given here.

  1. <TextBlock Text="First Name: " FontSize="24"/>  
  2. <TextBox x:Name="firstNameTextBox" Grid.Column="1" />  
  3.   
  4. <TextBlock Text="Last Name: " Grid.Row="1" FontSize="24"/>  
  5. <TextBox x:Name="lastNameTextBox" Grid.Row="1" Grid.Column="1" />  
  6.   
  7. <Button x:Name="GoButton" Grid.Column="1" Grid.Row="2" Content="Go"   
  8.         VerticalAlignment="Bottom" Width="110" Click="GoButton_Click" />  
  9. <TextBlock x:Name="welcomeTextBlock" HorizontalAlignment="Left"   
  10.            Margin="10,9.5,0,0" Grid.Row="3" TextWrapping="Wrap"   
  11.            VerticalAlignment="Top" Height="55" Width="360" FontSize="24"   
  12.            Grid.ColumnSpan="2"/>  
  13. <TextBlock x:Name="nameTextBlock" HorizontalAlignment="Left"   
  14.            Margin="10,98.5,0,0" Grid.Row="3" TextWrapping="Wrap"   
  15.            Width="360" FontSize="24" Grid.ColumnSpan="2"   
  16.            Height="55" VerticalAlignment="Top"/>  
Listing 1

Since we need to handle the Input operation, we used a Button control in the code above and have a click event “GoButton_Click”. So our C# code will look like this.
  1. private void GoButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (lastNameTextBox.Text != string.Empty)  
  4.     {  
  5.         if (firstNameTextBox.Text != string.Empty)  
  6.         {  
  7.             welcomeTextBlock.Text = "Hello,";  
  8.             nameTextBlock.Text = firstNameTextBox.Text + " " + lastNameTextBox.Text;  
  9.         }  
  10.         else  
  11.         {  
  12.             welcomeTextBlock.Text = "Hello,";  
  13.             nameTextBlock.Text = lastNameTextBox.Text;  
  14.         }  
  15.     }  
  16.     else  
  17.     {  
  18.         welcomeTextBlock.Text = "Sorry,";  
  19.         nameTextBlock.Text = "Last name can't be empty!";  
  20.     }  
  21. }  
Listing 2

Now, what I actually did is, checked whether or not the text of “lastNameTextBox” is empty. If it's not empty, it'll be good to go. Then we're checking the text of “firstNameTextBox” and if it's not empty then we'll do the operation below the second if decision statement. So, in this case we make a welcome text “Hello” and put the first and last name in the “nameTextBlock” or we'll just put the last name in this field.

Otherwise, we'll give an error message if the last name field is empty, because the last name can't be empty.

Working with Date & Time Picker Control

Now, we'll talk about the Date and Time Picker controls. Drag and drop or just write your own customized XAML. I like to write my preferable customized XAML. I've taken one Textblock as the header to show some text, one DatePicker and one TimePicker and a Button. On the right side, I've also taken a TextBlock as a header field and two other TextBlocks to show the date and time you'll pick. The design is given here.

date and time picker
                                               Figure 2

The design code is given below.
  1. <TextBlock HorizontalAlignment="Left" Margin="10,10.167,0,0"   
  2.            Grid.Row="4" TextWrapping="Wrap" Text="Pick a Date and Time"   
  3.            FontSize="20" VerticalAlignment="Top" Height="47" Width="170"/>  
  4. <DatePicker x:Name="datePicker" HorizontalAlignment="Left"   
  5.             Margin="10,74.167,0,0" Grid.Row="4"   
  6.             VerticalAlignment="Top" Width="170"/>  
  7. <TimePicker x:Name="timePicker" HorizontalAlignment="Left"   
  8.             Margin="10,146.167,0,0" Grid.Row="4" VerticalAlignment="Top"   
  9.             Width="170"/>  
  10. <TextBlock HorizontalAlignment="Left" Margin="10,10.167,0,0"   
  11.            Grid.Row="4" TextWrapping="Wrap" Text="You have selected"   
  12.            FontSize="20" VerticalAlignment="Top" Height="47"   
  13.            Width="170" Grid.Column="1"/>  
  14. <TextBlock x:Name="dateTextBlock" HorizontalAlignment="Left"   
  15.            Margin="10,84.167,0,0" Grid.Row="4" TextWrapping="Wrap"   
  16.            FontSize="20" VerticalAlignment="Top" Height="47"   
  17.            Width="170" Grid.Column="1"/>  
  18. <TextBlock x:Name="timeTextBlock" HorizontalAlignment="Left"   
  19.            Margin="10,156.167,0,0" Grid.Row="4" TextWrapping="Wrap"   
  20.            FontSize="20" VerticalAlignment="Top" Height="47" Width="170"   
  21.            Grid.Column="1"/>  
  22. <Button x:Name="submitButton" Grid.Row="4" Content="Submit"   
  23.         VerticalAlignment="Bottom" Width="110"   
  24.         Click="submitButton_Click" Margin="10,0,0,1" />  
Listing 3

And in the code behind, the C# code will be like this.
  1. private void submitButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //dateTextBlock.Text = datePicker.Date.Day + " /" + datePicker.Date.Month + " /" + datePicker.Date.Year;  
  4.     dateTextBlock.Text = datePicker.Date.ToString("D");  
  5.     //timeTextBlock.Text = timePicker.Time.Hours + ":" + timePicker.Time.Minutes;  
  6.     //timePicker.ClockIdentifier = Windows.Globalization.ClockIdentifiers.TwelveHour;  
  7.     timeTextBlock.Text = timePicker.Time.ToString("T");  
  8. }  
Listing 4

Here, in the Button event handler, we've used some methods to show the date and time. The best and easy option is given here for both date and time. Others are commented out, you can try these if you want.

After you've set all these, your design will look like this:

design look
                                       Figure 3

And if you run the application, it'll work just like this.

windows phone app
                                          Figure 4

Summary

I hope, you can do that with me. See you soon with Part 3. Until then, good bye. Have a nice day. Happy coding.

Read the original article at: http://bit.ly/1FcnI0H

Up Next
    Ebook Download
    View all
    Learn
    View all