选择动态listboxs在C#创建项目 [英] Selecting dynamically created listboxs items in C#

查看:154
本文介绍了选择动态listboxs在C#创建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建一个列表框并与一些物品装满它,一旦在文本框我想输入一个点显示列表框,以便用户可以通过使用箭头键选择任何项目。结果
我做的一切了这一点。当用户类型的文本框点,该列表框被显示,但箭头键不会选择任何项目!

I dynamically created a Listbox and filled it with some items, Upon typing a dot in a Textbox i want to show the Listbox so that the user can select any item by using arrow keys .
I did everything up to this point. When the user types a dot in the Textbox, The Listbox gets shown, But the arrow keys wont select any items!

private void txtResults_KeyDown(object sender, KeyEventArgs e)
    {
       string[] words= ((TextBox)sender).Text.Split(' ');
       string s = sampleWord.Text = words[words.Length - 1];

        if (e.KeyCode == Keys.OemPeriod)
        {
            ShowPopUpList(s);
        }
        else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
        {
            lst.Focus();//doesnt work :-/
        }
        else
        {
            lst.Hide();
            txtResults.Focus();
        }
    }

这是对FormLoad创建列表框的代码( )

This is the code for creating the listbox on FormLoad()

private void CreateListBox()
{

    lst = new ListBox();
    lst.Size = new Size(70, 130);
    lst.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
    lst.KeyDown += lst_KeyDown;                   
    lst.DoubleClick += lst_DoubleClick;    

    //adding some test input
    lst.Items.Add("بسم");
    lst.Items.Add("الله ");
    lst.Items.Add("الرحمن ");
    lst.Items.Add("الرحیم ");
    lst.Items.Add("بنام ");
    lst.Items.Add("خداوند ");
    lst.Items.Add("بخشنده ");
    lst.Items.Add("مهربان ");
    lst.Items.Add("الهی شکرت ");



}

}

private void ShowListbox()
{

     txtResults.SelectionStart = txtResults.Text.Length;
     txtResults.SelectionLength = 0;
     Point index = txtResults.GetPositionFromCharIndex(txtResults.SelectionStart-1);
     lst.Location = new Point (index.X-50, index.Y+70);
     this.Controls.Add(lst);
     lst.BringToFront();
     lst.Show();
 }

ShowPopUpList(S) ShowListbox()方法被调用。没有什么幻想一下吧!

In ShowPopUpList(s) the ShowListbox() method is called. nothing fancy about it!

请注意:

我只需要在列表框中当我使用向上或向下箭头键来获得焦点明确地选择项目。除非当时我需要能够自由地继续打字,不要失去聚焦到列表框中。结果
请告诉我周围的方式做这件事?

I only need the list box to get focus when i use UP or DOWN arrow keys to explicitly select an item. unless then i need to be able to freely continue typing and dont lose focus to listbox.
Whats the way around doing it ?

推荐答案

从文本框的keydown的处理程序移除焦点,并在这里把它:

Remove focus from the textbox keydown handler and place it here:

        private void ShowPopUpList(string s)
        {
            //your initialization of the
            //listbox  here and after..
            listBox1.Focus();
        }



另一种方式:

Another way:

if (e.KeyCode == Keys.OemPeriod)
{
     ShowPopUpList(s);
     listBox1.Focus();
}



真正的最大的区别是它得到集中rightaway.with您的旧代码将第一。检查KeyDown和第二打它应该已经包含焦点

The real big difference is it gets focus rightaway.with your old code would first check the keydown and on the second hit it would already contain the focus.

最后编辑:

如果列表框需要得到重点向上/向下箭头键(只有那些键):

If Listbox needs to get the focus on up/down arrow keys(and only with those keys):

if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
{
       ShowPopUpList();
       listBox1.Focus();
       listBox1.SelectedIndex = 0;
}

这篇关于选择动态listboxs在C#创建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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