472,353 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

'Autocompleting' Listbox?

CJM
I would like to be able to use an autocompleting listbox - that is, one
where you can select an item in the list by typing the first few characters;
the standard IE listbox only responds to the first character.

I'm happy to pay a few quid for a 3rd-party component/activex control if
necessary but I imagine something equally good could be done with
client-side code...

Any suggestions?

Note: This is for an intranet-based ASP application, where all clients are
using IE6.

Chris

--
cj*******@REMOVEMEyahoo.co.uk
[remove the obvious bits]
Jul 22 '05 #1
3 1809
CJM wrote:
I would like to be able to use an autocompleting listbox - that is,
one where you can select an item in the list by typing the first few
characters; the standard IE listbox only responds to the first
character.
I'm happy to pay a few quid for a 3rd-party component/activex control
if necessary but I imagine something equally good could be done with
client-side code...

Any suggestions?

Note: This is for an intranet-based ASP application, where all
clients are using IE6.


See if you can adapt the dynamic listbox demo I wrote which is posted here:
http://www.thrasherwebdesign.com/ind...asp&c=&a=clear

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #2
"CJM" <cj*******@newsgroup.nospam> wrote in message
news:#B**************@TK2MSFTNGP09.phx.gbl...
I would like to be able to use an autocompleting listbox - that is, one
where you can select an item in the list by typing the first few characters; the standard IE listbox only responds to the first character.

I'm happy to pay a few quid for a 3rd-party component/activex control if
necessary but I imagine something equally good could be done with
client-side code...

Any suggestions?

Note: This is for an intranet-based ASP application, where all clients are
using IE6.

Chris

--
cj*******@REMOVEMEyahoo.co.uk
[remove the obvious bits]

For an IE-only solution, check out:

// Cooking with JavaScript & DHTML
// Bonus Recipe: Typing select Element Choices in IE for Windows
// http://www.oreillynet.com/pub/a/java...nygoodman.html
// http://www.oreillynet.com/lpt/a/4135
Jul 22 '05 #3
CJM
Thanks Guys...

I actually found a script which works in virtually the same way as the
O'Reilly one, using javascript on the clientside rather than Bob's XML Data
Island solution.

I'm not sure which is the better approach, but I can guarantee the
javascript is enabled in my target browsers, so I've gone for that option.
I'm saving a few server roundtrips with no drawbacks that I can see...

My code snippet is below. In a similar vein to the O'Reilly solution, I'm
calling this procedure via the OnKeyPress event handler

Chris

Snippet:
var toFind = ""; // Variable that acts as keyboard buffer
var timeoutID = ""; // Process id for timer (used when stopping
// the timeout)
timeoutInterval = 250; // Milliseconds. Shorten to cause keyboard
// buffer to be cleared faster
var timeoutCtr = 0; // Initialization of timer count down
var timeoutCtrLimit = 3 ; // Number of times to allow timer to count
// down
var oControl = ""; // Maintains a global reference to the
// control that the user is working with.

function listbox_onkeypress(){

// This function is called when the user presses a key while focus is in
// the listbox. It maintains the keyboard buffer.
// Each time the user presses a key, the timer is restarted.
// First, stop the previous timer; this function will restart it.
window.clearInterval(timeoutID)

// Which control raised the event? We'll need to know which control to
// set the selection in.
oControl = window.event.srcElement;

var keycode = window.event.keyCode;
if(keycode >= 32 ){
// What character did the user type?
var c = String.fromCharCode(keycode);
c = c.toUpperCase();
// Convert it to uppercase so that comparisons don't fail
toFind += c ; // Add to the keyboard buffer
find(); // Search the listbox
timeoutID = window.setInterval("idle()", timeoutInterval);
// Restart the timer
}
}

function listbox_onblur(){
// This function is called when the user leaves the listbox.

window.clearInterval(timeoutID);
resetToFind();
}

function idle(){
// This function is called if the timeout expires. If this is the
// third (by default) time that the idle function has been called,
// it stops the timer and clears the keyboard buffer

timeoutCtr += 1
if(timeoutCtr > timeoutCtrLimit){
resetToFind();
timeoutCtr = 0;
window.clearInterval(timeoutID);
}
}

function resetToFind(){
toFind = ""
}
function find(){
// Walk through the select list looking for a match

var allOptions = document.all.item(oControl.id);

for (i=0; i < allOptions.length; i++){
// Gets the next item from the listbox
nextOptionText = allOptions(i).text.toUpperCase();

// By default, the values in the listbox and as entered by the
// user are strings. This causes a string comparison to be made,
// which is not correct for numbers (1 < 11 < 2).
// The following lines coerce numbers into an (internal) number
// format so that the subsequent comparison is done as a
// number (1 < 2 < 11).

if(!isNaN(nextOptionText) && !isNaN(toFind) ){
nextOptionText *= 1; // coerce into number
toFind *= 1;
}

// Does the next item match exactly what the user typed?
if(toFind == nextOptionText){
// OK, we can stop at this option. Set focus here
oControl.selectedIndex = i;
window.event.returnValue = false;
break;
}

// If the string does not match exactly, find which two entries
// it should be between.
if(i < allOptions.length-1){

// If we are not yet at the last listbox item, see if the
// search string comes between the current entry and the next
// one. If so, place the selection there.

lookAheadOptionText = allOptions(i+1).text.toUpperCase() ;
if( (toFind > nextOptionText) &&
(toFind < lookAheadOptionText) ){
oControl.selectedIndex = i+1;
window.event.cancelBubble = true;
window.event.returnValue = false;
break;
} // if
} // if

else{

// If we are at the end of the entries and the search string
// is still higher than the entries, select the last entry

if(toFind > nextOptionText){
oControl.selectedIndex = allOptions.length-1 // stick it
// at the end
window.event.cancelBubble = true;
window.event.returnValue = false;
break;
} // if
} // else
} // for
} // function
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex =...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) ...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.