Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI,

In my Windows project i am using text box to disply names which are stored in the database, when i type the specific name it displays the name from database.and also dislpy name in listbox. But what i want is, when i type any letter i.e 'v'it should display all names starts with 'V'.For it i got solution as;
C#
char firstChar = 0;
this.listBox1.Items.Clear();

if (textBox1.Text != String.Empty)
{
  if (firstChar != textBox1.Text[0])
  {
    firstChar = textBox1.Text[0];

    for (int i = 0; i < TempArrayList.Count; i++)
    {
      if ( temp.GetValue(i).ToString()[0] == firstChar)
        this.listBox1.Items.Add(temp.GetValue(i));
    }
  }
}

by using it as per type any name in textbox display on list.

But, my next question is that.When i type any letter i got list of names which starts by typing letter.Now i want to set this particular name in textbox.How to do it?
i.e i type letter 'c'then display list of all names which stars by letter 'C',and also disply it in listbox simultaneously.upto till i got it.after it suppose i choose 'car'word then in listbox which automaticaly get selected and also want to show in textbox
Posted
Updated 22-May-12 2:07am
v2

1 solution

Hi Sneha,

You can use Linq to query your list of names.

Basic syntax on how you will do is explained below:

C#
IList<string> numbers = new List<string>()
            {
                "one","two","three","four","five","six","seven","eight","nine","ten"
            };

var filteredNumbers = numbers.Where(x => x.StartsWith("t")).Select(x => x.Length).ToList();
            filteredNumbers.ForEach(x => Console.WriteLine(x));



So, you just have to fetch the names from the database and store those names in any Enumerable object like IList etc and query them with Linq on text change or whatever way you want.

Hope this help you.

Happy Coding :)
 
Share this answer
 
Comments
BobJanova 22-May-12 10:51am    
Yes, a Linq expression as written on your second line is the right answer. However, since all you want to do is iterate over it, I wouldn't use ToList, I'd just do

listbox.Items.Clear();
for(string s in source.Where(s => s.StartsWith(input)))
listbox.Items.Add(s);
Sunny_Kumar_ 22-May-12 10:59am    
thanks, I really appreciate your opinion.
Member 11793580 1-Jul-15 2:39am    
Hi all,
Even i am facing the same problem.When i give full name in search text box it is displaying correctly but when i want to give part of name it is not displaying. I am using winform in c#.
private void btnSearch_Click(object sender, EventArgs e)
{

if (txtSearch.Text == "" && txtSearch1.Text == "")
MessageBox.Show("Please enter S ID");
else
{
DataTable dtSearch = VMR.MySqlHelper.GetDataTable("select * from table_name where S_ID = '" + txtSearch.Text + "' OR Fellow_name like '%da%'");
if (dtSearch.Rows.Count > 0)
{
textBox1.Text = dtSearch.Rows[0]["S_ID"].ToString();
txtFellowname.Text = dtSearch.Rows[0]["Fellow_name"].ToString();
txtPositionheld.Text = dtSearch.Rows[0]["Position_held"].ToString();

Can ne1 please give a solution to this?

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