Working With ListBox In Windows Phone (Send SMS, Email and Make a Call) : Part 3

Windows Phone ListBox previous articles:

Introduction 

 
Here we can make a call to the Student, send an SMS to that student and send an Email to that student. For that use this procedure.
 
Step 1
 
Here we continue the previous article in which we edit the data in the ListBox like this.
 
ListBoxInWP7-1.png
 
Step 2
 
First, we use three buttons (For Call, Send SMS, Send Email) and three TextBlocks like this.
 
ListBoxInWP7-2.png
 
  1. <Button Height="65" HorizontalAlignment="Left" Margin="33,532,0,0" Name="btnCall" VerticalAlignment="Top" Width="85" FontSize="18" Click="btnCall_Click" IsEnabled="True"    
  2. Visibility="Collapsed" Grid.ColumnSpan="2">    
  3.                     <Button.Background>    
  4.                         <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/Dialer.png" />    
  5.                     </Button.Background>    
  6.                     <Button.BorderBrush>    
  7.                         <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">    
  8.                             <GradientStop Color="Black" Offset="0" />    
  9.                             <GradientStop Color="#FF905E35" Offset="1" />    
  10.                             <GradientStop Color="#FF3E352F" Offset="0.082" />    
  11.                         </LinearGradientBrush>    
  12.                     </Button.BorderBrush>    
  13.                 </Button>    
  14.                 <Button Height="65" HorizontalAlignment="Right" Margin="0,532,178,0" Name="btnSMS" VerticalAlignment="Top" Width="79" FontSize="18" IsEnabled="True"    
  15. Visibility="Collapsed" Click="btnSMS_Click" Grid.Column="1">    
  16.                     <Button.BorderBrush>    
  17.                         <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">    
  18.                             <GradientStop Color="Black" Offset="0" />    
  19.                             <GradientStop Color="#FF7C674E" Offset="1" />    
  20.                         </LinearGradientBrush>    
  21.                     </Button.BorderBrush>    
  22.                     <Button.Background>    
  23.                         <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/Message1.png" />    
  24.                     </Button.Background>    
  25.                 </Button>    
  26.                 <Button Height="65" HorizontalAlignment="Left" Margin="302,528,0,0" Name="btnEmail" VerticalAlignment="Top" Width="79" FontSize="18" IsEnabled="True"    
  27. Visibility="Collapsed" Click="btnEmail_Click" Grid.Column="1">    
  28.                     <Button.Background>    
  29.                         <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/Send%20Email1.png" />    
  30.                     </Button.Background>    
  31.                     <Button.BorderBrush>    
  32.                         <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">    
  33.                             <GradientStop Color="Black" Offset="0" />    
  34.                             <GradientStop Color="#FF80644A" Offset="1" />    
  35.                         </LinearGradientBrush>    
  36.                     </Button.BorderBrush>    
  37.                 </Button>                  
  38.                 <TextBlock FontFamily="Calibri" FontSize="20" FontStyle="Normal" FontWeight="Bold" Height="32" HorizontalAlignment="Left" Margin="18,516,0,0" Name="txtBlkCall" Text="Call" TextAlignment="Center" VerticalAlignment="Top" Width="109" Visibility="Collapsed" Grid.ColumnSpan="2" />    
  39.                 <TextBlock FontFamily="Calibri" FontSize="20" FontStyle="Normal" FontWeight="Bold" Height="32" HorizontalAlignment="Left" Margin="112,515,0,0" Name="txtBlkSMS" Text="Send    
  40. SMS" TextAlignment="Center" VerticalAlignment="Top" Width="109" Grid.Column="1" Visibility="Collapsed" />    
  41.                 <TextBlock FontFamily="Calibri" FontSize="20" FontStyle="Normal" FontWeight="Bold" Height="32" HorizontalAlignment="Left" Margin="265,514,0,0" Name="txtBlkSendEmail"    
  42. Text="Send Email" TextAlignment="Center" VerticalAlignment="Top" Width="127" Grid.Column="1" Visibility="Collapsed" />     
Note
 
Here we set the Visibility=Collapsed. In this example, the buttons will be visible when we tap a single item in the ListBox For this we create a Tap event of our ListBox and we write the following code in it.
  1. private void lstStudentname_Tap(object sender, System.Windows.Input.GestureEventArgs e)  
  2.     {             
  3.          btnCall.Visibility = Visibility.Visible;  
  4.          btnSMS.Visibility = Visibility.Visible;  
  5.          btnEmail.Visibility = Visibility.Visible;            
  6.          txtBlkCall.Visibility = Visibility.Visible;  
  7.          txtBlkSendEmail.Visibility = Visibility.Visible;  
  8.          txtBlkSMS.Visibility = Visibility.Visible;  
  9. }   
Step 3
 
Now we will write the code for the Call Button:
  1. private void btnCall_Click(object sender, RoutedEventArgs e)  
  2.     {  
  3.            var listBoxItem = lstStudentname.ItemContainerGenerator.ContainerFromIndex(lstStudentname.SelectedIndex) as ListBoxItem;  
  4.            var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtBlkExtra");   
  5.            a = txtBlk.Text;   
  6.            using (StudentdataContext StudentDB = new StudentdataContext(strConnectionString))  
  7.            {  
  8.                  var c = from b in StudentDB.GetTable<StudentInfo>() where b.Extra == a.ToString() select b;  
  9.                  foreach (var x in c)  
  10.                  {  
  11.                       if (x.Mobile == "")  
  12.                       {  
  13.                            MessageBox.Show("Please Add the Mobile Number");  
  14.                        }  
  15.                       else  
  16.                       {  
  17.                            PhoneCallTask pct = new PhoneCallTask();  
  18.                            pct.PhoneNumber = x.Mobile;  
  19.                            pct.Show();  
  20.                       }  
  21.                  }  
  22.             }   
  23.       }   
Output
 
If we cannot enter the Mobile Number then the following message will appear.
 
ListBoxInWP7-3.png
 
To enter a Mobile Number we should first edit the data. To do that we double-tap on the ListBox like this.
 
ListBoxInWP7-4.png
 
The following window will be shown, here we enter a Mobile Number and edit the Student Details.
 
ListBoxInWP7-5.png
 
Now we click on the Call Button.
 
ListBoxInWP7-6.png
 
Step 4
 
Now we will write the code to send the SMS.
 
First, we create a page (SendSMS.xaml) to add controls like this.
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.             <Grid.Background>  
  3.                 <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/wooden-background.jpg" />  
  4.             </Grid.Background>  
  5.             <TextBlock Height="33" HorizontalAlignment="Left" Margin="32,20,0,0" Name="txtBlkTo" Text="To" VerticalAlignment="Top" FontWeight="Bold" Width="43" FontSize="22"  
  6. FontFamily="Portable User Interface" />  
  7.             <TextBox Height="72" HorizontalAlignment="Left" Margin="107,0,0,0" Name="txtTo" Text="" VerticalAlignment="Top" Width="343" FontSize="18" />  
  8.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="32,81,0,0" Name="txtBlkCC" Text="CC" VerticalAlignment="Top" Width="43"  
  9. FontFamily="Portable User Interface" />  
  10.             <TextBox FontSize="18" Height="72" HorizontalAlignment="Left" Margin="107,63,0,0" Name="txtCC" Text="" VerticalAlignment="Top" Width="343" />  
  11.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="14,163,0,0" Name="textBlkSubject" Text="Subject" VerticalAlignment="Top" Width="97" FontFamily="Portable User Interface" />  
  12.             <TextBox FontSize="18" Height="72" HorizontalAlignment="Left" Margin="107,138,0,0" Name="txtSubject" Text="" VerticalAlignment="Top" Width="343" />  
  13.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="22,0,0,287" Name="textBlkBody" Text="Body" VerticalAlignment="Bottom" Width="97"  
  14. FontFamily="Portable User Interface" />  
  15.             <TextBox FontSize="18" Height="206" HorizontalAlignment="Left" Margin="107,216,0,0" Name="txtBody" Text="" VerticalAlignment="Top" Width="343" />   
  16.             <Button Content="Send" Height="72" HorizontalAlignment="Left" Margin="280,450,0,0" Name="btnSend" VerticalAlignment="Top" Width="160" Click="btnSend_Click"  
  17. FontWeight="Bold">  
  18.                 <Button.Background>  
  19.                     <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/Dark%20Wooden%20Background.jpg" />  
  20.                 </Button.Background>  
  21.             </Button>  
  22.         </Grid>  
  23.     </Grid>  
Step 5
 
So when we click on the "Send SMS" button in MainPage we pass the following parameter (Mobile Number).
  1. NavigationService.Navigate(new Uri(string.Format("/SendSMS.xaml?parameter={0}", x.Mobile), UriKind.Relative));   
After that we write the following code to fetch the data in the SendSMS.xaml.cs page:
  1. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  2.     {  
  3.          base.OnNavigatedTo(e);  
  4.          parameterValue = NavigationContext.QueryString["parameter"];  
  5.          txtMobileNumber.Text = parameterValue.ToString();  
  6.          object sample;  
  7.          if (PS.State.TryGetValue("numbertext", out sample))  
  8.              txtMobileNumber.Text = sample as string;  
  9.          if (PS.State.TryGetValue("messagetext", out sample))  
  10.              txtMessage.Text = sample as string;  
  11.          base.OnNavigatedTo(e);  
Output
 
ListBoxInWP7-7.png
 
Now we will write the code for the "Send SMS" button.
  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2.     {  
  3.          if (txtMobileNumber.Text == "" || txtMessage.Text == "")  
  4.          {  
  5.              MessageBox.Show("Please Enter the Correct Data");                 
  6.          }  
  7.          else  
  8.          {  
  9.              CT.To = txtMobileNumber.Text;  
  10.              CT.Body = txtMessage.Text;   
  11.              CT.Show();  
  12.              this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));   
  13.          }  
  14. }  
Here we send the message to the following number.
 
Step 6
 
Now we will write the code for sending an email.
 
First, we create a page (SendMail.xaml) and add controls like this.
 
ListBoxInWP7-8.png
 
  1.     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.             <Grid.Background>  
  3.                 <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/wooden-background.jpg" />  
  4.             </Grid.Background>  
  5.             <TextBlock Height="33" HorizontalAlignment="Left" Margin="32,20,0,0" Name="txtBlkTo" Text="To" VerticalAlignment="Top" FontWeight="Bold" Width="43" FontSize="22"  
  6. FontFamily="Portable User Interface" />  
  7.             <TextBox Height="72" HorizontalAlignment="Left" Margin="107,0,0,0" Name="txtTo" Text="" VerticalAlignment="Top" Width="343" FontSize="18" />  
  8.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="32,81,0,0" Name="txtBlkCC" Text="CC" VerticalAlignment="Top" Width="43"  
  9. FontFamily="Portable User Interface" />  
  10.             <TextBox FontSize="18" Height="72" HorizontalAlignment="Left" Margin="107,63,0,0" Name="txtCC" Text="" VerticalAlignment="Top" Width="343" />  
  11.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="14,163,0,0" Name="textBlkSubject" Text="Subject" VerticalAlignment="Top" Width="97"  
  12. FontFamily="Portable User Interface" />  
  13.             <TextBox FontSize="18" Height="72" HorizontalAlignment="Left" Margin="107,138,0,0" Name="txtSubject" Text="" VerticalAlignment="Top" Width="343" />  
  14.             <TextBlock FontSize="22" FontWeight="Bold" Height="33" HorizontalAlignment="Left" Margin="22,0,0,287" Name="textBlkBody" Text="Body" VerticalAlignment="Bottom" Width="97"  
  15. FontFamily="Portable User Interface" />  
  16.             <TextBox FontSize="18" Height="206" HorizontalAlignment="Left" Margin="107,216,0,0" Name="txtBody" Text="" VerticalAlignment="Top" Width="343" />  
  17.             <Button Content="Send" Height="72" HorizontalAlignment="Left" Margin="280,450,0,0" Name="btnSend" VerticalAlignment="Top" Width="160" Click="btnSend_Click"  
  18. FontWeight="Bold">  
  19.                 <Button.Background>  
  20.                     <ImageBrush ImageSource="/WorkingWithListBoxInWP7;component/Image/Dark%20Wooden%20Background.jpg" />  
  21.                 </Button.Background>  
  22.             </Button>  
  23.         </Grid>  
  24.     </Grid>   
So when we click on the "Send Email" button in the MainPage we pass the following parameter (Email Id).
  1. using (StudentdataContext StudentDB = new StudentdataContext(strConnectionString))   
  2. {  
  3.       var email = from b in StudentDB.GetTable<StudentInfo>() where b.Extra == a.ToString() select b;  
  4.       foreach (var x in email)  
  5.       {  
  6.            if (x.Email == "")  
  7.            {  
  8.                  MessageBox.Show("Please Add the Email Address");  
  9.            }  
  10.            else  
  11.            {  
  12.                  NavigationService.Navigate(new Uri(string.Format("/SendMail.xaml?parameter={0}", x.Email), UriKind.Relative));  
  13.            }  
  14.       }  
  15.  
After that we write the following code to fetch the data in the SendMail.xaml.cs page.
  1. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  2. {  
  3.       base.OnNavigatedTo(e);  
  4.       parameterValue = NavigationContext.QueryString["parameter"];  
  5.       txtTo.Text = parameterValue.ToString();   
Now we will write the code to send an email.
  1. private void btnSend_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.        EmailComposeTask MYEmailTask = new EmailComposeTask();  
  4.        MYEmailTask.To = txtTo.Text;  
  5.        MYEmailTask.Cc = txtCC.Text;  
  6.        MYEmailTask.Subject = txtSubject.Text;  
  7.        MYEmailTask.Body = txtBody.Text;  
  8.        MYEmailTask.Show();   
  9. }   
Using this example we can make a call, send SMS and send an email to that Student.
 

Summary

 
In this article, we learned about Working With ListBox In Windows Phone (Send SMS, Email and Make a Call) : Part 3.