Windows Phone Development For Beginner - Part 4

Datetime:2016-08-23 03:48:38          Topic: WPhone Development           Share

Before reading this article, I highly recommend reading my previous articles:

In this part, I will explain how to write an application and access a TextBox value in Windows Phone.

Let's start to write the application.

Write first Application

Select New project from the File menu and select Windows Phone applicaton. Add three TextBoxes to the grid panel and add one button and two textboxes for entering values and a third TextBox for the result.

MainPage.xaml

  1.     
  2. <!--LayoutRoot is the root grid where all page content is placed-->   
  3. < Grid   x:Name = "LayoutRoot"   Background = "Transparent" >   
  4.      < Grid.RowDefinitions >   
  5.          < RowDefinition   Height = "Auto" />   
  6.          < RowDefinition   Height = "*" />   
  7.      </ Grid.RowDefinitions >   
  8.      <!--TitlePanel contains the name of the application and page title-->   
  9.      < StackPanel   x:Name = "TitlePanel"   Grid.Row = "0"   Margin = "12,17,0,28" >   
  10.          < TextBlock   x:Name = "ApplicationTitle"   Text = "MY APPLICATION"   Style = "{StaticResource PhoneTextNormalStyle}" />   
  11.          < TextBlock   x:Name = "PageTitle"   Text = "1st Application"   Margin = "9,-7,0,0"   Style = "{StaticResource PhoneTextTitle1Style}" />   
  12.      </ StackPanel >   
  13.      <!--ContentPanel - place additional content here-->   
  14.      < Grid   x:Name = "ContentPanel"   Grid.Row = "1"   Margin = "12,0,12,0" >   
  15.          < TextBlock   x:Name = "lblfirst"   Text = "Enter 1st Number"   Margin = "25,38,241,527" > </ TextBlock >   
  16.          < TextBlock   x:Name = "lblsecond"   Text = "Enter 2nd Number"   Margin = "25,98,241,467" > </ TextBlock >   
  17.          < TextBlock   x:Name = "lblresult"   Text = "Result"   Margin = "25,161,241,404" > </ TextBlock >   
  18.          < TextBox   x:Name = "txtfirst"   Margin = "225,24,6,508" > </ TextBox >   
  19.          < TextBox   x:Name = "txtsecond"   Margin = "225,84,6,450" > </ TextBox >   
  20.          < TextBox   x:Name = "txtresult"    Margin = "225,147,6,381" > </ TextBox >   
  21.          < Button   x:Name = "btnadd"   Height = "95"   Content = "Addition"   Margin = "0,232,0,280"   Click = "btnadd_Click" > </ Button >   
  22.      </ Grid >   
  23. </ Grid   

                           Figure 1

Let's write the code for the button click event. The first and second TextBoxes accept the user input data and the third TextBox shows the addition of the two numbers.

MainPage.xaml.cs

  1. using  System;  
  2. using  System.Collections.Generic;  
  3. using  System.Linq;  
  4. using  System.Net;  
  5. using  System.Windows;  
  6. using  System.Windows.Controls;  
  7. using  System.Windows.Documents;  
  8. using  System.Windows.Input;  
  9. using  System.Windows.Media;  
  10. using  System.Windows.Media.Animation;  
  11. using  System.Windows.Shapes;  
  12. using  Microsoft.Phone.Controls;  
  13.   
  14. namespace  WriteFirstApplication  
  15. {  
  16.      public  partial  class  MainPage : PhoneApplicationPage  
  17.     {  
  18.          // Constructor   
  19.          public  MainPage()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.          private   void  btnadd_Click( object  sender, RoutedEventArgs e)  
  25.         {  
  26.              
  27.              int  result = Convert.ToInt32(txtfirst.Text)+Convert.ToInt32(txtsecond.Text);  
  28.             txtresult.Text = result.ToString();  
  29.   
  30.               
  31.         }  
  32.     }  
  33. }  

After doing everything run your application.

                                 Figure 2

Now write one more code for showing the result in the messagebox. You can add only one line for the message box.

MessageBox.Show(“YourContent”)

MainPage.xaml.cs

  1. using  System;  
  2. using  System.Collections.Generic;  
  3. using  System.Linq;  
  4. using  System.Net;  
  5. using  System.Windows;  
  6. using  System.Windows.Controls;  
  7. using  System.Windows.Documents;  
  8. using  System.Windows.Input;  
  9. using  System.Windows.Media;  
  10. using  System.Windows.Media.Animation;  
  11. using  System.Windows.Shapes;  
  12. using  Microsoft.Phone.Controls;  
  13.   
  14. namespace  WriteFirstApplication  
  15. {  
  16.      public  partial  class  MainPage : PhoneApplicationPage  
  17.     {  
  18.          // Constructor   
  19.          public  MainPage()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.          private   void  btnadd_Click( object  sender, RoutedEventArgs e)  
  25.         {  
  26.              
  27.              int  result = Convert.ToInt32(txtfirst.Text)+Convert.ToInt32(txtsecond.Text);  
  28. MessageBox.Show( "Addition of " + txtfirst.Text + "&" + txtsecond.Text + "="  +result.ToString());  
  29.   
  30.   
  31.              
  32.         }  
  33.     }  
  34. }  
Figure 3




About List