Anyone know how to programmatically pop open a combobox's dropdown list. Ideally i want it perminantly open.
Printable View
Anyone know how to programmatically pop open a combobox's dropdown list. Ideally i want it perminantly open.
You should set the DropDownStyle to Simple at design time if you want the list permanently visible. You can set the DroppedDown property to True to drop the list programmatically but it will disappear again as soon as the user clicks somewhere.
If you want it open permanently, why not use a Listbox instead of a combo?
(Congrats on 10K jmcilhinney :thumb: )
The advantage of a Simple ComboBox is that you can type in anything that you want, or select from the list, just like a DropDown ComboBox. A ListBox is like a DropDownList ComboBox, where you can only choose from the items in the list. If that's what you want then you should use a ListBox as Hack suggests.Quote:
Originally Posted by Hack
I changed the style to 'simple' and changing the member variable as you said didn't work. Changing the styly to 'dropdown' did though, so thanks for that. :)
I've got another problem now though. All i did was add the line 'cboRunThis.DroppedDown = true;' in my text changed event and now the mouse disappears when it's over my program.
I guess the problem is that i'm updating the list in a seperate thread, but i'm doing that ok i think (by calling invoke if required, using code from MSDN). It doesn't make the cursor disappear if i manually click the combobox to show the list, plus the list is updated as appropriate then.
I'm just not sure why it should be a problem when i'm updating the list in another thread.
Any thoughts on this would be appreciated.
EDIT:
Now as soon as the list had items then has none i get the following error...
I have no idea what could be wrong with my code, since that isn't my code.Code:System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
at System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)
at System.Windows.Forms.ComboBox.get_Text()
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I've tried removing every line where i dropdown the list, so i'm really stumped as to what's going wrong. I guess it's to do with the way i'm updating the list, so here's the code my thread is using to update the list.
and here's the code i use in the textchanged event that clears the combobox's list, and aborts the worker thread (and creates a new worker thread).Code:public void AddItem(string Item, ComboBox DisplayBox)
{
if (DisplayBox.InvokeRequired)
{
AddItemCallback CallBack = new AddItemCallback(AddItem);
this.Invoke(CallBack, new object[] { Item, DisplayBox });
}
else
{
int CursorPos = DisplayBox.SelectionStart;
DisplayBox.Items.Add(Item);
DisplayBox.SelectionStart = CursorPos;
}
}
Code:private void cboRunThis_TextChanged(object sender, EventArgs e)
{
WorkerParameters Params = new WorkerParameters();
if (WorkerThread.IsAlive)
{
WorkerThread.Abort();
WorkerThread.Join();
}
if (cboRunThis.Text.Length == 0)
return;
FoundItemsQueue.Clear();
int CursorPos = cboRunThis.SelectionStart;
cboRunThis.Items.Clear();
cboRunThis.SelectionStart = CursorPos;
if (cboRunThis.Text.Contains("/"))
return;
WorkerThread = new Thread(Job);
Params.SearchString = cboRunThis.Text;
Params.DisplayBox = cboRunThis;
WorkerThread.Start(Params);
}
I think you're going about this all wrong. I think what you probably should be doing is using the autocomplete features of WinForms 2.0. Take a look at the properties of the TextBox and ComboBox that start with "AutoComplete".
Also, that call to Join looks a bit dodgy. If you Join the worker thread then your UI thread stops executing until the worker thread completes. That's probably why you're losing your cursor.
I can't really use auto complete since the items in the list done always begin with what the user types in (plus it seems that if i change the list on the fly, changing the autocompletecustom string collection it crashes if it's being displayed).
The second point, the call do join is fine since i'm aborting the thread just before i join it (so basically i trigger the thread to be terminated , then wait for it to end).
The cursor problem was happening before i added the join command (in fact i added it hoping that was the causing the wierd crashing!).
Thanks for your input though, it's greatly appreciated!