Professional Documents
Culture Documents
fan of all things mobile, father of small girls, IBM code monkey, youth charity trustee…
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
beginning with ‘T’, you need to press T fourteen times to get the Grace and Faith
fourteen item in the list)
» a drop down list of strings to choose from, allowing new items not
already on the list to be entered – (using a ComboBox with a
DropDownStyle of DropDown)
» clicking on the drop-down arrow with the stylus provides a
scrollable list to choose from
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
» December 2013
» November 2013
» August 2013
» June 2013
» May 2013
» April 2013
» February 2013
» December 2012
» November 2012
Why this way? » September 2012
» August 2012
I needed to be able to show the difference between the bit of the text » June 2012
that has been typed in manually, and the bit of the text that was provided » May 2012
by auto-complete. (Otherwise, the interface is too unclear – a user » April 2012
wouldn’t know where their next key-press would go). » March 2012
» February 2012
This is normally done by highlighting the auto-completed text. For » January 2012
example, after typing “app”… » December 2011
» November 2011
» October 2011
» September 2011
» June 2011
» May 2011
» April 2011
TextBox lets you programmatically alter selected text – with attributes » February 2011
SelectionStart, SelectionLength and so on. But it doesn’t have a » December 2010
drop-down list. » November 2010
ComboBox doesn’t let you programmatically alter selected text – it » October 2010
doesn’t have any of the Selection... attributes. » September 2010
» August 2010
So I figured that the quickest and easiest way to get the best of both » July 2010
worlds would be to have both. » June 2010
» May 2010
» April 2010
The code
» March 2010
» February 2010
Fairly straight-forward, but here ya go.
» January 2010
Download it here, but here is the interesting bit… » December 2009
» November 2009
using System; » October 2009
using System.Text; » September 2009
using System.Windows.Forms; » August 2009
» July 2009
namespace AutoComplete » June 2009
{ » May 2009
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
/* » April 2009
* AutoComplete » March 2009
* » February 2009
* a text box which allows free-form entry of text » January 2009
* whilst also providing auto-complete from a given » December 2008
* list of strings » November 2008
* » October 2008
* an item can be selected from the list either by: » September 2008
* a) choosing from the drop-down combobox list » August 2008
* b) typing enough characters of the start of the » July 2008
* desired item to uniquely identify it » June 2008
* » May 2008
* To use in a form: » April 2008
* 1) Drag onto a Form Designer using Visual Studio, » March 2008
* using the Form Designer to resize/place the » February 2008
* box » January 2008
* 2) Use SetItems to provide the list of strings used » December 2007
* to match for auto-complete » November 2007
* 3) Use AddItem to add any additional strings » October 2007
*/ » September 2007
public partial class AutoCompleteTextBox : UserControl » August 2007
{ » July 2007
// stores the items that are used for auto-complete » June 2007
private string[] comboBoxItems = new string[0]; » May 2007
» April 2007
public AutoCompleteTextBox() » March 2007
{ » February 2007
InitializeComponent(); » January 2007
» December 2006
// if the user uses the ComboBox to choose an item, » November 2006
// handle this by mirroring it in the TextBox » October 2006
» September 2006
innerComboBox.SelectedValueChanged += new EventHandler(innerComboBox_SelectedValueChanged);
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Array.Sort(comboBoxItems); Links
» my CV
innerComboBox.Items.Clear(); » my diary
» my links
for (int i = 0; i < comboBoxItems.Length; i++) » my online profiles
{ » my photos
innerComboBox.Items.Add(comboBoxItems[i]); » where am i?
}
} Disclaimer
// add an item to the array of strings used for auto-complete matches
The postings on this site are my own
public void AddItem(string newitem) and don't necessarily represent the
positions, strategies or opinions of
{
IBM or SYA.
string[] newitems = new string[comboBoxItems.Length + 1];
newitems[comboBoxItems.Length] = newitem;
SetItems(newitems);
}
// clear the list of strings used for auto-complete matches
public void ClearItems()
{
comboBoxItems = new string[0];
innerComboBox.Items.Clear();
}
// the user has used the ComboBox to select one of the possible
// auto-complete matches
//
// handle this by mirroring it in the textbox
void innerComboBox_SelectedValueChanged(object sender, EventArgs e)
{
innerTextBox.Text = innerComboBox.Text;
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
// this is where we store any match found in the auto-complete list
string match = null;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
// we want first match - once found, break out
break;
}
}
if (match == null)
{
// user has typed something not already in the list
innerTextBox.Text = typedSoFar;
innerTextBox.SelectionStart = typedSoFar.Length;
innerTextBox.SelectionLength = 0;
}
else
{
// user has typed text which matches the start of something
// in the provided auto-complete list
innerComboBox.SelectedItem = match;
}
Will it work?
Probably?
Not sure if it will handle a user typing too much too fast, so some sort of
synchronisation might need to be looked at before this gets used in
earnest. But it is enough for my needs tonight, and seemed to do the
trick.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
This entry was posted on Sunday, June 17th, 2007 at 11:16 pm and is filed under code.
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and
pings are currently closed.
Hi,
I tried to open your code in VS.NET 2.0 but was contineously getting the error
“Error retreiving information from user database. Platform not found”.
dale says:
Friday 28th September 2007 at 9:46 am
@Hari
It sounds like you don’t have the Windows Mobile SDK installed. This is for
.NETCF (Compact Framework) – which is to say the version of .NET for mobile
devices, not “regular” .NET.
(Hari)
I had the same problem
In order to fix it I opended (with notepad) a “csproj” file of some (Well)
working project of mine, and copied the line
In my case it was:
3C41C503-53EF-4c2a-8DD4-A8217CAD115E
Then I opened “AutoComplete.csproj” and repalce the original line.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
now it seems working.
Tzahi.
(Hari)
…
This editor eliminates “tags” so I’m resending it,
this time without consider “[” as “<“.
Tzahi.
…
I had the same problem
In order to fix it I opended (with notepad) a “csproj” file of some (Well)
working project of mine, and copied the “PlatformID” line
In my case it was:
Tzahi.
dale says:
Wednesday 30th April 2008 at 1:34 pm
@Tzahi Kupfer – sounds like you hacked the project so that what was
originally a project targeted for the Windows Mobile platform could be built for
the Windows desktop platform.
It’s not what I’d originally intended (I think the ComboBox you get with
standard desktop .NET already gives you AutoComplete anyway!) but I’m glad
you found it useful anyway.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
dale says:
Wednesday 30th April 2008 at 1:34 pm
@Bobby Beckner – Glad it helped – thanks for taking the time to comment!
It’s that sort of thing that makes it worth keep sharing this sort of stuff.
Ricky says:
Friday 2nd May 2008 at 10:03 am
Hi Dale.
I ended up doing much the same as you and I want to take it to the next step
– have it do data binding.
For the life of me I can’t get the text, selectedindex or selectedvalue to show
on the bindings at design time.
I can do this if I inherit Combobox rather than Usercontrol but then I can’t get
the text box to receive the key input.
Any ideas?
regards, Ricky
Niru says:
Tuesday 30th September 2008 at 9:00 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Have a look at my ComboBox-derivation here:
http://larsole.com/files/autocomplete-combobox.cs
It uses interop (pieced that together from various other blog postings) to do
what it does. It has no textfield mojo other than what is already in a standard
combobox.
I can even use it with objects other than strings (objects with a ToString()
override) in the Items collection.
Vassili says:
Thursday 14th October 2010 at 11:22 pm
Excellent, exactly what I was looking for. Thanks a lot for sharing.
Just some small recommendation: it is not very intuitive what is going on
when the user tries to delete selected text (actually nothing in your sample).
To delete selected text, here is the change:
case (char)Keys.Back:
if (innerTextBox.SelectionLength > 0 || cursorLocation > 0)
{
if (innerTextBox.SelectionLength == 0)
{
cursorLocation -= 1;
}
typedSoFar = innerTextBox.Text.Substring(0, cursorLocation);
innerTextBox.Text = innerTextBox.Text.Remove(cursorLocation,
Math.Max(innerTextBox.SelectionLength, 1));
innerTextBox.SelectionStart = typedSoFar.Length;
innerTextBox.SelectionLength = 0;
goto key_handle_complete;
} break;
inyu says:
Tuesday 22nd February 2011 at 5:13 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
dale lane is proudly powered by WordPress
Entries (RSS) and Comments (RSS).
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD