views:

191

answers:

2

Here's the problem:

This is for a WPF app that uses C# and LINQ to SQL.

When a user wants to look at a list of customers, he/she begins entering the name in a textbox. The textchanged event uses the input text to define the where clause of a LINQ statement that filters the list.

I currently have two such text boxes that run essentially the same code, but I cannot reduce that code to a single function - I'll be using customer lists in a bunch more places.

Here's a bit of the code:

private void CustomerListFiller(object sender, TextChangedEventArgs e)

    {

        string SearchText;

        FrameworkElement feSource = e.Source as FrameworkElement;

        ***SearchText =  sender.Text;*** 

        var fillCustList = from c in dbC.Customers

                           where c.CustomerName.StartsWith(SearchText)

                           orderby c.CustomerName

                           select new

                           {

                               c.CustomerID,

                               c.CustomerName

                           };

The bold, italicized line is the problem. I can't figure out how get at the text value of the sender to use in the StartsWith function. The error message is:

Error 1 'object' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

+1  A: 

You have to cast the "sender" variable to TextBox:

SearchText =  (sender as TextBox).Text;
Henrik Söderlund
Thanks! that did it! and, man, do I feel stupid!
Jack McG
A: 

You need to cast sender to a TextBox:

var textBox = sender as TextBox;
SearchText = textBox.Text;

Hope it helps

klausbyskov
this works, too! thanks for your help!
Jack McG