Thanattha Bussapak

Thanattha Bussapak

  • NA
  • 20
  • 18.8k

How to create autocomplete box on windows phone 8

Nov 28 2013 12:27 PM
I am new programmer. I want to create autocomplete box on windows phone 8 form database (MySQL) list. Now I can create autocomplete box by List<string> source = new List<string>();
            source.Add("Phaya Thai");

But I don't know how I create list form MySQL

<phone:PhoneApplicationPage
    x:Class="Train_Guide_Thailand.SearchPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="#FF71BAE8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Margin="10,40,2,10">
            <TextBlock  Text="Treno" Margin="16,-40,10,0" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Comic Sans MS" Foreground="#FF494943" FontSize="20" Height="23" FontWeight="Bold"/>
            <TextBlock TextWrapping="Wrap" Text="Search" FontFamily="Comic Sans MS" Foreground="#FF494943" FontSize="26" Height="48" Margin="16,0,0,0"/>
        </StackPanel>


        <!--ContentPanel - place additional content here-->
        <Grid Background="LightSkyBlue" x:Name="ContentPanel" CacheMode="BitmapCache" Margin="0,98,0,0" Grid.RowSpan="2">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="55*"/>
                <ColumnDefinition Width="178*"/>
                <ColumnDefinition Width="7*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="99*"/>
                <RowDefinition Height="93*"/>
                <RowDefinition Height="94*"/>
                <RowDefinition Height="282*"/>
            </Grid.RowDefinitions>


            <TextBlock FontFamily="Comic Sans MS" Foreground="#FF3C3C35" HorizontalAlignment="Left" TextWrapping="Wrap" Text="From" VerticalAlignment="Top" Margin="20,46,0,0" Height="34" Width="63"/>
            <toolkit:AutoCompleteBox  x:Name="SourceStation"  ItemsSource="{Binding}" Margin="0,22,3,10" Grid.Column="1" />
            <Button  HorizontalAlignment="Right" FontFamily="Comic Sans MS"  Click="deleteButton1_Click" Content="X" BorderBrush="Red" Foreground="{StaticResource PhoneBackgroundBrush}" VerticalAlignment="Top" BorderThickness="0" Width="50" Padding="0,0,0,0" Grid.Column="1" Margin="0,35,3,0" FontSize="18.667" RenderTransformOrigin="0.555,0.335" Height="45">
                <Button.Background>
                    <SolidColorBrush Color="{StaticResource PhoneProgressBarBackgroundColor}"/>
                </Button.Background>
            </Button>
            <TextBlock FontFamily="Comic Sans MS" Foreground="#FF3C3C35" HorizontalAlignment="Left" TextWrapping="Wrap" Text="To" VerticalAlignment="Top"  Height="32" Width="58" Grid.Row="2" RenderTransformOrigin="0.625,0.625" Margin="20,32,0,0"/>
            <Button Margin="126,0,0,0" Grid.Column="1" HorizontalAlignment="Left" Width="96" Grid.Row="1" Click="form_to_Button_Click" />
            <toolkit:AutoCompleteBox x:Name="DestStation"  ItemsSource="{Binding}" Margin="0,10,3,18" Grid.Column="1" Grid.Row="2"/>
            <Button  HorizontalAlignment="Right" FontFamily="Comic Sans MS"  Click="deleteButton2_Click" Content="X" BorderBrush="Red" Foreground="{StaticResource PhoneBackgroundBrush}" VerticalAlignment="Top" BorderThickness="0" Width="50" Padding="0,0,0,0" Grid.Column="1" Margin="0,21,3,0" Grid.Row="2" FontSize="18.667" RenderTransformOrigin="0.525,0.824" Height="49">
                <Button.Background>
                    <SolidColorBrush Color="{StaticResource PhoneProgressBarBackgroundColor}"/>
                </Button.Background>
            </Button>
            <TextBlock FontFamily="Comic Sans MS" Foreground="#FF3C3C35" Margin="20,23,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Date" VerticalAlignment="Top"  Width="63" Height="34" Grid.Row="3"/>
            <toolkit:DatePicker x:Name="SearchTime" Margin="0,94,134,210" Grid.Row="2" Grid.Column="1" Grid.RowSpan="2"/>
           
        </Grid>


    </Grid>


    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Image/searchButton2.png"  Text="Search" Click="ApplicationBarSearchButton_Click"/>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>



</phone:PhoneApplicationPage>





using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


namespace Train_Guide_Thailand
{
    public partial class SearchPage : PhoneApplicationPage
    {
        public SearchPage()
        {
            InitializeComponent();
       
            //--SourceStation--
            List<string> source = new List<string>();
            source.Add("Phaya Thai");
            source.Add("Ladkrabang");
            source.Add("Pra Chom Klao");
            source.Add("aaaa");
            source.Add("bbbb");
            SourceStation.ItemsSource = source;  // destinations;

            //--destinationsStation--
            List<string> destinations = new List<string>();
            destinations.Add("Phaya Thai");
            destinations.Add("Ladkrabang");
            destinations.Add("Pra Chom Klao");
            destinations.Add("aaaa");
            destinations.Add("bbbb");
            DestStation.ItemsSource = destinations;

            //--SourceStation
            SourceStation.DropDownOpening += new RoutedPropertyChangingEventHandler<bool>(SourceStation_DropDownOpening);
            SourceStation.GotFocus += new RoutedEventHandler(SourceStation_GotFocus);
            SourceStation.LostFocus += new RoutedEventHandler(SourceStation_LostFocus);

            SourceStation.TextFilter += SearchStation;
            SourceStation.TextChanged += new RoutedEventHandler(SourceStation_TextChanged);
            SourceStation.SelectionChanged += new SelectionChangedEventHandler(SourceStation_SelectionChanged);

            SourceStation.Text = "";

            //--DestStation
            SourceStation.DropDownOpening += new RoutedPropertyChangingEventHandler<bool>(SourceStation_DropDownOpening);
            SourceStation.GotFocus += new RoutedEventHandler(SourceStation_GotFocus);
            SourceStation.LostFocus += new RoutedEventHandler(SourceStation_LostFocus);

            SourceStation.TextFilter += SearchStation;
            SourceStation.TextChanged += new RoutedEventHandler(SourceStation_TextChanged);
            SourceStation.SelectionChanged += new SelectionChangedEventHandler(SourceStation_SelectionChanged);

            SourceStation.Text = "";
        }

        bool SearchStation(string search, string value) // value is the possible match
        {
            if (!string.IsNullOrEmpty(search))
            {
                string currentText = search;
                string filterText = currentText;
                if (currentText.Contains(','))
                {
                    int pos = currentText.LastIndexOf(',');
                    // get only what we have after comma
                    filterText = currentText.Substring(pos + 1);
                }
                // Look for a match in the last letter.
                // do not allow to add twice the same tag from suggestion
                if (value.ToLower().StartsWith(filterText) && !previouslyTypedCities.Contains(value))
                {
                    return true; //match
                }
            }
            // If no match
            return false;
        }

        string previouslyTypedCities = "";

        void SourceStation_TextChanged(object sender, RoutedEventArgs e)
        {
            try
            {

                if (SourceStation.Text.Contains(','))
                {
                    int pos = SourceStation.Text.LastIndexOf(',');
                    if (pos > 0) previouslyTypedCities = SourceStation.Text.Substring(0, pos + 1); // get the comma as well
                }
            }
            catch (Exception ex)
            {

            }
        }

        void SourceStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.RemovedItems.Count > 0) return;
                if (e.AddedItems.Count > 0)
                {

                    if (!previouslyTypedCities.Contains(SourceStation.Text))
                    {
                        // add the new selection
                        SourceStation.Text = previouslyTypedCities + SourceStation.Text;
                        // set new value
                        SourceStation.SelectedItem = SourceStation.Text;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private bool SourceStationFocused = false;

        void SourceStation_LostFocus(object sender, RoutedEventArgs e)
        {
            SourceStationFocused = false;
        }

        void SourceStation_GotFocus(object sender, RoutedEventArgs e)
        {
            SourceStationFocused = true;
        }

        void SourceStation_DropDownOpening(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            // AutoCompleteBox is not focused, do NOT open drop-down
            if (!SourceStationFocused)
                e.Cancel = true;
        }




        //------------------------------------------------------------------------------------------
        //Search_Click

        ProgressIndicator prog;

        private void ApplicationBarSearchButton_Click(object sender, EventArgs e)
        {

            //Source station destination do not duplicate.
            if ((SourceStation.Text != "" && DestStation.Text != "") && SourceStation.Text != DestStation.Text )
            {

                string url = "http://169.254.80.80/checkSearch.php";
                Uri uri = new Uri(url, UriKind.Absolute);

                StringBuilder postData = new StringBuilder();
                postData.AppendFormat("{0}={1}", "sStationName", HttpUtility.UrlEncode(this.SourceStation.Text));
                postData.AppendFormat("&{0}={1}", "sStationName", HttpUtility.UrlEncode(this.DestStation.Text));


                WebClient client = default(WebClient);
                client = new WebClient();
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();

                client.UploadStringCompleted += client_UploadStringCompleted;
                client.UploadProgressChanged += client_UploadProgressChanged;

                client.UploadStringAsync(uri, "POST", postData.ToString());


                prog = new ProgressIndicator();
                prog.IsIndeterminate = true;
                prog.IsVisible = true;
                prog.Text = "Loading....";
                SystemTray.SetProgressIndicator(this, prog);

            }

Answers (2)