开发者

Autocomplete Textbox using separator character in a row

How can i use a text box with distinctive characters in row with auto complete and drop down be active ?

when in text box , i enter the first distinctive character , I have both auto complete and drop down menu , i mean both of them are active . But , as soon as i want to add another distinctive character in my text box , none of my auto complete and my drop down menu are active . i mean they don't show themselves. just in the first using show . With which code , i can make auto complete and drop d开发者_开发知识库own active in each stage after distinctive character?

i use platform win form.


If you need to select multiple months, then you can use multi-selection ListBox. The ListBox will contain the list of months, and users will be able to select multiple months by holding Ctrl key and clicking. However, multiple selection ListBoxes are not considered as a good user experience, because users may have difficulty with it.

If the order of selection is important, you can use the approach described by mj82. In this case, your ListBox should be in single-selection mode. Users select a month in ListBox, and then click Add button to add the month to a TextBox.

Yet in the latter case, I would suggest using two ListBoxes. The one on the left has the full list of months, users move the selected months from the list on the left to the list on the right. If the order of the selected months is important, then you should also add Move Up and Move Down buttons so that users could change the order in the selected list.

You dialog may look similar to this:

Autocomplete Textbox using separator character in a row


For a very simple example, without complicated logic, drag 2x listbox + 2x button on your form. Then attach following code. As you see, there is a simple method for moving ONE selected item from one listbox to another. It's connected to button's click and to listbox double click events.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.AddRange(new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" });

    }

    //ADD button
    private void button1_Click(object sender, EventArgs e)
    {
        MoveItem(listBox1, listBox2);
    }

    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        MoveItem(listBox1, listBox2);
    }


    //REMOVE button
    private void button2_Click(object sender, EventArgs e)
    {
        MoveItem(listBox2, listBox1);
    }

    private void listBox2_DoubleClick(object sender, EventArgs e)
    {
        MoveItem(listBox2, listBox1);
    }

    /// <summary>
    /// Moves item from one listbox to another
    /// </summary>
    /// <param name="listBoxFrom">Origin listbox</param>
    /// <param name="listBoxTo">Destination listbox</param>
    private void MoveItem(ListBox listBoxFrom, ListBox listBoxTo)
    {
        if (listBoxFrom.SelectedItems.Count == 1)
        {
            listBoxTo.Items.Add(listBoxFrom.SelectedItem);
            listBoxFrom.Items.Remove(listBoxFrom.SelectedItem);
        }
    }
} 

You may then consider additional actions for disabling buttons where items are not selected, add UP / DOWN button or sorting item's list (for a list of moth's names however, you cannot use simply lexical sorting - you would have to use and sort a dictionary or your own class/struct object).

At the end, to retrieve selected items, use code similar to this:

System.Text.StringBuilder sb = new StringBuilder();
foreach (object obj in listBox2.Items)
   sb.Append(obj).Append(";");

MessageBox.Show("Your selection is: " + sb.ToString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜