CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    2

    How to create a "Search" textbox, with instant result show in listbox? Int value

    Hi. I'm new to WPF (just 1 week before i have no ideas what it is)

    I have a listbox, with data taken from a column (busId: int) in table "INFO" from SQL Server.

    I want to create a Search textbox. When user enter some number, the listbox above will automatically filter the data to show only row that contain the number the user entered in textbox.

    I've searched for this for days. Non of the solutions work.

    Here my code

    XAML

    <Window x:Class="Test2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Select Bus" Height="250" Width="250" Loaded="Window_Loaded">
    <Grid Height="200" Width="200">
    <!--<ListView Name="BusInformation" Margin="12" ItemsSource="{Binding Tables[INFO]}">
    <ListView.Background>
    <LinearGradientBrush>
    <GradientStop Color="Gray" Offset="0"/>
    </LinearGradientBrush>
    </ListView.Background>
    <ListView.View>
    <GridView>
    <GridViewColumn Header="Bus ID:" DisplayMemberBinding="{Binding Path=busId}"/>
    <GridViewColumn Header="Bus Name:" DisplayMemberBinding="{Binding Path=name}"/>
    </GridView>
    </ListView.View>
    </ListView>-->
    <ListBox Name="BusInformation"
    ItemsSource="{Binding Tables[INFO]}"
    DisplayMemberPath="busId" Margin="0,31,124,0">
    </ListBox>
    <TextBlock Height="33" HorizontalAlignment="Left" Name="textBlock1" Width="188" VerticalAlignment="Top" FontWeight="Bold" TextWrapping="Wrap"
    Text="Select a Bus ID, then click Show"/>
    <Button Content="Xem thông tin" Height="28" HorizontalAlignment="Left" Margin="104,60,0,0" Name="buttonInfo" VerticalAlignment="Top" Width="84" Click="buttonInfo_Click"/>
    <TextBox Height="24" HorizontalAlignment="Left" Margin="104,129,0,0" Name="textBoxSearch" VerticalAlignment="Top" Width="84" TextChanged="textBoxSearch_TextChanged" />
    <Label Content="Search" Height="21" HorizontalAlignment="Left" Margin="102,102,0,0" Name="label1" VerticalAlignment="Top" Width="86" />
    </Grid>
    </Window>


    C# Code-behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data;
    using System.Data.SqlClient;
    using System.ComponentModel;
    using System.Windows.Threading;

    namespace Test2
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

    private DataSet GetADOData()
    {
    DataSet dsSet = new DataSet();

    string connectionString = @"Data Source=TRAN-D1652CEBAF\SQEXPRESS2;Initial Catalog=bus_test2;Integrated Security=True";
    string sql="SELECT * FROM INFO";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    SqlCommand command=new SqlCommand(sql, connection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    connection.Open();
    adapter.SelectCommand = command;
    adapter.Fill(dsSet, "INFO");
    }

    return dsSet;
    }

    public MainWindow()
    {
    InitializeComponent();
    BusInformation.DataContext = GetADOData();
    }

    private void buttonInfo_Click(object sender, RoutedEventArgs e)
    {
    var MainWindow = new Details();
    MainWindow.Show();
    DataRowView drv = (DataRowView)BusInformation.SelectedItem;
    MainWindow.textBlock1.Text = drv.Row["BusId"].ToString();
    MainWindow.textBlockBusName.Text = drv.Row["name"].ToString();
    MainWindow.textBoxGo.Text = drv.Row["go"].ToString();
    MainWindow.textBoxBack.Text = drv.Row["back"].ToString();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void textBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    }
    }



    Thanks

  2. #2
    Join Date
    Nov 2011
    Posts
    2

    Re: How to create a "Search" textbox, with instant result show in listbox? Int value

    Thank you for your help. I'm found a solution for my problem. Just another "GetADOData" will work, with condition in SQL command"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured