.NET Components for Mobility

Peter Foot

Microsoft Device Application Development MVP

Implement FindString for a ComboBox

The .NETCF ComboBox control doesn't have the FindString method which is present on the desktop however this is just a wrapper for a windows message supported by the native control so it is possible to wrap it quite easily in .NETCF 2.0 and above. First you need to define the windows message constant:-

internal const int CB_FINDSTRING = 0x14c;

Then you can P/Invoke SendMessage:-

[DllImport("coredll.dll")]
internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

With a little more work you can utilize the existing SendMessage method in Microsoft.WindowsCE.Forms but taking this approach allows us to define the argument types specifically for this purpose.

To use this method pass in the handle to the ComboBox (e.g. comboBox1.Handle), the message constant, the starting index to search from (e.g. 0) and finally the string to match terminated with a null character e.g.

int index = SendMessage(comboBox1.Handle, CB_FINDSTRING, 0, "Cheese\0");

The method will return either the item index which first matches this value (0 to the number of items -1) or -1 if not found.

To use this on Smartphone you need to use an alternative message since the managed ComboBox control wraps a native listbox:-

internal const int LB_FINDSTRING = 0x018F;

Comments

No Comments

About PeterFoot

Peter Foot is co-author of the Microsoft Mobile Development Handbook published by Microsoft Press. Peter has been awarded the Microsoft Most Valuable Professional (MVP) accolade since 2003 for his involvement in the Microsoft .NET Compact Framework developer community. Alongside an active presence in several online forums and communities, attendance at developer conferences and involvement in shared-source projects, Peter has also written a number of technical articles and maintains an active technical blog.
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.