Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Guys, I have another question, I hope I haven't over complicated it.

Brief Project Description:
I am trying to make a touchscreen application that consists of a ListBox (getting data from SQL via Linq) and 26 Buttons (one for each letter of the alphabet). There are three fields displayed in each ListBox item, "Name", "Address" and "Phone Number".

Question:
Is it possible to goto fields in the ListBox with a "Name" starting with the particular letter of the Button pressed?

I would like to keep all fields visible and jump to Names starting with "S" (for example).

More Information:
I have been looking around and TextSearch class keeps coming up but i am not really sure how to apply it to my situation.

I was thinking i could use the below code to eliminate the need for each button needing its own code.
private void SearchLetterButton(object sender, RoutedEventArgs args)
{
    Button btn = sender as Button;
    string buttondata = btn.Content;
    /// Listbox Search Code using "buttondata" as the search criteria
}

Thanks any help in advance,
Alex.
Posted
Updated 8-Feb-10 12:35pm
v2

Hi Guys, Sorry for the delay in getting back to you both,

Thanks Christian, I had a look at the ScrollViewer Control and it seems to only allow you to scroll to the first and last items automatically. There is ScrollViewer.ScrollToHorizontalOffset but you need to know the SelectedIndex of the item that you want to scroll to and knowing the SelectedIndex is half the problem.

I looked more into the TextSearch class and it seems a little promising. If you enable TextSearch and set the TextPath on the field you want to search, when you press a letter on the keyboard it selects the first item in the list with that letter. All that is needed now is to substitute the keyboard input with a button click.

TextSearch Example Code:
<Window.Resource>
    <DataTemplate x:Key="listboxTemplate">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
</Window.Resources>

<Grid>
    <ListBox x:Name="listbox" ItemTemplate="{StaticResource listboxTemplate}"
    IsTextSearchEnabled="True" TextSearch.TextPath="Name"/>
</Grid>
C#
var query = from s in dc.People
            orderby s.Name
            select s;
listbox.ItemsSource = query.ToList();


I know its kind of crude but i have found a couple of samples of virtual keyboard simulation that might be a means to an end. The code below works for TextBox's but i haven't quite worked out how to adapt it to work with a ListBox.
C#
void keyboardButton_Click(object sender, RoutedEventArgs e)
{
    if (System.Windows.Input.Keyboard.FocusedElement.GetType() == typeof(TextBox))
    {
        TextBox box = ((TextBox)System.Windows.Input.Keyboard.FocusedElement);
        int newIndex = box.CaretIndex + 1;
        box.Text = box.Text.Insert(box.CaretIndex, ((Button)sender).Content.ToString());
        box.CaretIndex = newIndex;
    }
}


Thanks again for the replies, Ill yet you know if i make any progress,
Alex
 
Share this answer
 
This[^] looks like a good place to start. Just remember the item that is the first item with each letter, so you can scroll to it.
 
Share this answer
 
Dude even i m trying to find it out.... :-D Whenever i finished i will definitely tell u... :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900