2013/05/23

How to create process Loader in Window Application



1.       Create  ModelLodingUI  Window Form and set the property : FromBoderStyle- None
2.       Drag & Drop Picture Box and set Loading Image.
3.       Paste following code:
4.  namespace LoadingProcess
5.  {
6.      public partial class ModalLoadingUI : Form
7.      {
8.          #region Constants
9.          public bool CloseMe = false;
10.        #endregion
11. 
12.        public ModalLoadingUI()
13.        {
14.            InitializeComponent();
15.        }
16.        private void ModalLoadingUI_Load(object sender, EventArgs e)
17.        {
18. 
19.        }
20.        public void LoadForm()
21.        {
22.            this.Opacity = 0.5;
23.            this.BackColor = Color.Gray;
24.            this.Show();
25.            while (true)
26.            {
27.                if (CloseMe)
28.                {
29.                    Application.DoEvents();
30.                    CloseMe = false;
31. 
32.                    this.Close();
33.                    break;
34.                }
35.                else
36.                {
37.                    Application.DoEvents();
38.                }
39.            }
40. 
41.        }
42. 
43.    }
44.   }
II. Create Static Class like Program.cs:
static class Program
    {
        static ModalLoadingUI FormLoader;           

        public static void StartLoder()
        {
            FormLoader = new ModalLoadingUI();
            ThreadStart TS = new ThreadStart(FormLoader.LoadForm);
            Thread T = new Thread(TS);
            T.Start();
            Thread.Sleep(5000);
        }

        public static void StoptLoder()
        {
            FormLoader.CloseMe = true;
        }
    }
III. Call the function on page where you want to show the process.
private void button1_Click(object sender, EventArgs e)
        {
Program.StartLoder();
            // -----rite you code like saveData()-------------
            Thread.Sleep(5000);
            //-------------------
            Program.StoptLoder();        }


How To Create Google Map In WPF



Gmaap.xaml
--------------

namespace Test
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Googlemaps : Window
    {
        public Googlemaps(string from , string to)
        {
            InitializeComponent();
            txt_From.Text = from;
            txt_To.Text = to;
            btn_Search_Click(null, null);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Keyboard.Focus(txt_From ); 
        }

        private void SetWebBrowser()
        {
            try
            {
                Uri uri = new Uri("http://maps.google.co.in/maps?saddr=[" + txt_From.Text.Trim() + "]&daddr=[" + txt_To.Text.Trim() + "]&hl=en&output=embed&avoid=tolls&avoid=highways");
                //Uri uriDir = new Uri("http://maps.google.com/maps/api/directions/xml?origin=" + txt_From.Text.Trim() + "&destination=" + txt_To.Text.Trim() + "&sensor=false&avoid=tolls&avoid=highways");
                //wb_GDir.Navigate(uriDir);           
                wb_GMap.Navigate(uri);
            }
            catch
            {
                MessageBox.Show("Try with combinations of City State Country (for e.g. Jaipur Rajasthan India)","Google Maps");
            }
        }

        private void GetXML()
        {
            try
            {
                string url = "http://maps.google.com/maps/api/directions/xml?origin=" + txt_From.Text.Trim() + "&destination=" + txt_To.Text.Trim() + "&sensor=false&avoid=tolls&avoid=highways";
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
                IWebProxy proxy = WebRequest.GetSystemWebProxy(); proxy.Credentials = CredentialCache.DefaultCredentials;
                httpRequest.Proxy = proxy;
                httpRequest.Method = "GET";
                //httpRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+.NET+CLR+1.1.4322)";
                httpRequest.Timeout = -1;
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                Stream resStream = httpResponse.GetResponseStream();

                System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(XmlDocument));
                XmlDocument doc = (XmlDocument)deserializer.Deserialize(resStream);

                string startAddr = doc.GetElementsByTagName("start_address")[0].InnerText;
                string endAddr = doc.GetElementsByTagName("end_address")[0].InnerText;
                XmlNodeList duration = doc.GetElementsByTagName("duration");
                XmlNodeList distance = doc.GetElementsByTagName("distance");
                XmlNodeList steps = doc.GetElementsByTagName("step");

                if (startAddr != null && endAddr != null)
                {
                    lbl_From.Visibility = Visibility.Visible;
                    lbl_To.Visibility = Visibility.Visible;
                    lbl_From.Content = "From : " + startAddr;
                    lbl_To.Content = "To : " + endAddr;
                }
                if (distance != null && distance.Count > 0)
                {
                    lbl_Distance.Visibility = Visibility.Visible;
                    lbl_Distance.Content = "Total Distance : " + distance[distance.Count - 1].SelectSingleNode("text").InnerText;
                }

                if (duration != null && duration.Count > 0)
                {
                    lbl_Duration.Visibility = Visibility.Visible;
                    lbl_Duration.Content = "Total Duration : " + duration[duration.Count - 1].SelectSingleNode("text").InnerText;
                }
                if (steps != null && steps.Count > 0)
                {
                    lbl_Steps.Visibility = Visibility.Visible;
                    lbl_StepsContent.Visibility = Visibility.Visible;
                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < steps.Count; i++)
                    {
                        string strTemp = steps[i].SelectSingleNode("html_instructions").InnerText;
                        strTemp = strTemp.Replace("<b>", "");
                        strTemp = strTemp.Replace("</b>", "");
                        strTemp = strTemp.Replace("</div>", " ");
                        strTemp = strTemp.Replace("&nbsp;", " ");
                        strTemp = strTemp.Replace("&amp;", " ");

                        while (strTemp.Contains("<div"))
                        {
                            int sIndex = strTemp.IndexOf("<div");
                            int eIndex = strTemp.IndexOf("\">", strTemp.IndexOf("<div"));
                            strTemp = strTemp.Remove(sIndex, eIndex - sIndex + 2);
                            strTemp = strTemp.Insert(eIndex - sIndex + 2, " ");
                        }
                        if (strTemp.Length > 25)
                        {
                            for (int j = 1; j <= (strTemp.Length) / 25; j++)
                            {
                                int index = strTemp.Substring(0, j * 25).LastIndexOf(" ");
                                strTemp = strTemp.Insert(index, "\n      ");
                            }
                        }
                        strBuilder.Append(i + 1);
                        strBuilder.Append(". ");
                        strBuilder.Append(strTemp);
                        strBuilder.Append("\n");
                    }
                    lbl_StepsContent.Content = "\n" + strBuilder.ToString();
                }
            }
            catch
            {
                MessageBox.Show("Try with combinations of City State Country (for e.g. Jaipur Rajasthan India)","Google Maps");
            }
        }

        private void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_From.Text.Trim()) && !string.IsNullOrEmpty(txt_To.Text.Trim()))
            {
                SetWebBrowser();
                GetXML();
            }
        }
    }
}
---------------------------------------------------------Google Map.Xaml Page---------------------------------------------------
<Window x:Class="Test.Googlemaps" KeyboardNavigation.TabNavigation="Cycle"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Google Maps" Height="600" Width="800" WindowStartupLocation="CenterScreen" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="SingleBorderWindow" MaxHeight="600" MaxWidth="800" Icon="/Test;component/Images/TEST_favicon.ico">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Content="Google Maps" FontSize="16" Grid.Column="0" Grid.Row="1"></Label>
        <Grid Grid.Column="1" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="200"></ColumnDefinition>
                <ColumnDefinition Width="30"></ColumnDefinition>
                <ColumnDefinition Width="200"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Content="From" FontSize="14" Grid.Column="0"></Label>
            <TextBox TabIndex="0" Name="txt_From" Width="190" Grid.Column="1"></TextBox>
            <Label Content="To" FontSize="14" Grid.Column="2"></Label>
            <TextBox TabIndex="1" Name="txt_To" Width="190" Grid.Column="3"></TextBox>
            <Button Name="btn_Search" TabIndex="2" Content="Search Maps" Click="btn_Search_Click" Width="80" Grid.Column="4"></Button>           
        </Grid>
        <Label Content="Searching Location by City State Country combinations e.g., Jaipur Rajasthan India" FontSize="10" Grid.Row="2" Grid.Column="1"></Label>
       <ScrollViewer Name="sv_GDir" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Column="0" Grid.Row="3" >
           <StackPanel Name="stkpnl_GDir" ScrollViewer.CanContentScroll="True">              
                <!--<WebBrowser Name="wb_GDir" Height="420" Width="170"></WebBrowser>-->
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>                       
                    </Grid.RowDefinitions>
                    <Label Content="Get Directions" Grid.Row="0"></Label>
                    <Label Name="lbl_From" FontWeight="Bold" Grid.Row="1" Visibility="Collapsed"></Label>
                    <Label Name="lbl_To" FontWeight="Bold" Grid.Row="2" Visibility="Collapsed"></Label>
                    <Label Name="lbl_Distance" Content="Distance : 0 KM" FontWeight="Bold" Visibility="Collapsed" Grid.Row="3"></Label>
                    <Label Name="lbl_Duration" Content="Duration : 0 Hrs" FontWeight="Bold" Visibility="Collapsed" Grid.Row="4"></Label>
                    <Label Name="lbl_Steps" Content="Steps : " Visibility="Collapsed" FontWeight="Bold" Grid.Row="5"></Label>
                    <Label Name="lbl_StepsContent" VerticalAlignment="Top" Visibility="Collapsed" Height="Auto" Grid.Row="6"></Label>                   
                </Grid>
            </StackPanel>
        </ScrollViewer>
        <ScrollViewer Name="sv_GMap" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Column="1" Grid.Row="3">
            <StackPanel Name="stkpnl_GMap" ScrollViewer.CanContentScroll="True">
                <WebBrowser Name="wb_GMap" Height="520" Width="590"></WebBrowser>
            </StackPanel>
        </ScrollViewer>
       
    </Grid>
</Window>

----------------------------------Call Function on another Page where you want to call Gmap------------------------
private void btn_ShowGmap_Click(object sender, RoutedEventArgs e)
        {
            if (ChkMapVal())
            {
                string VFrom, VTo;
                VFrom = acb_VehicleFrom.Text;
                VTo = acb_VehicleTo.Text;
                Googlemaps gm = new Googlemaps(VFrom, VTo);
                gm.Show();
            }
        }