C#代码从后端按字母顺序在文本框中显示名称 [英] C# code to display names in text box from backend alphabetically

查看:125
本文介绍了C#代码从后端按字母顺序在文本框中显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

在我的Windows项目中,我使用文本框显示存储在数据库中的名称,当我键入特定名称时,它会显示来自数据库的名称,并且还会在列表框中显示名称.但是我想要的是,当我输入任何字母即``v''时,它应该显示所有以``V''开头的名称.为此,我得到了解决方案;



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;

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));
    }
  }
}


通过按类型使用它,列表中显示的文本框中的任何名称.

但是,我的下一个问题是.当我输入任何字母时,我会得到以字母开头的名称列表.现在我想在文本框中设置此特定名称.
例如,我键入字母"c",然后显示所有以字母"C"开头的名称的列表,并同时将其显示在列表框中.直到得到它为止.在此之后,我选择了"car"一词然后在自动选择的列表框中,并且还要在文本框中显示


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

推荐答案

Sneha,

您可以使用Linq查询名称列表.

有关如何操作的基本语法说明如下:

Hi Sneha,

You can use Linq to query your list of names.

Basic syntax on how you will do is explained below:

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));




因此,您只需要从数据库中获取名称,然后将这些名称存储在任何Enumerable对象(如IList等)中,并在文本更改或您想要的任何方式下使用Linq查询它们即可.

希望对您有帮助.

快乐编码:)




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 :)


这篇关于C#代码从后端按字母顺序在文本框中显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆