URLComboBox: A Window Forms Control for Displaying URLs






4.86/5 (13 votes)
A Window Forms control for displaying URLs similar to URL combo in Internet Explorer
Introduction
URL combobox
is an easy to use control for displaying URLs. I had to develop this when I wrote a rich client interface for a web service.
Here are some features of the control:
- Autocomplete support like that in Internet Explorer.
- Automatic management of MRU list. The control automatically fills the combo list from a specific registry key (specified as a property) and also adds any URL entered by the user which is not already in the registry.
Using the Control
It is very simple to use the control.
- Add it to the VS.NET tool box.
- Place it on any form.
- Set the
AutoComplete
options by specifying theFlags
property. This controls what is displayed in the autocomplete popup list. For example, in the image shown below, theFlags
property is set so that the URL history, most recently typed URLs and the file system are included in the autocomplete list. (The various flags are documented in the C# XML documentation and correspond to the flags that can be specified to SHAutoComplete function). - Optionally, the properties
MRUKeyHive
andMRUKey
can be specified.MRUKeyHive
can beMRUKeyHive.LocalMachine
orMRUKeyHive.CurrentUser
, this indicates the root registry key (HKEY_LOCAL_MACHINE
orHKEY_CURRENT_USER
) to use.MRUKey
is astring
value that specifies the path of sub key under this key. The control looks for value names of the form urlXXX under this key (where XXX is a number, e.g.,url1
,url2
) and uses the data from these registry values to fill the list box. This is the same way as Internet Explorer manages the list of MRU URLs. The control also updates the registry key based on any new URL typed.The default values of these properties are set to the registry key which Internet Explorer uses for storing its most recently used URLs.
That's all that needs to be done to use this control. In fact, the demo VB app which is an internet browser has only one line of user written code. Everything else like resizing and layout of controls is managed by the Window Forms framework.
Implementation
The implementation is pretty simple. The control's CreateHandle
function is overloaded. In this function, the handle of the edit control is obtained by calling unmanaged GetComboBoxInfo
function. The handle thus obtained is passed to SHAutoComplete
function again through PInvoke. The MRU list is managed by using the convenient Microsoft.Win32.Registry
and Microsoft.Win32.RegistryKey
classes. The code contains the C# doc comments wherever necessary.
History
- 26th February, 2002: Initial version