Listbox type-to-value

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
owinsloe
Posts: 96
Joined: Tue Jul 10, 2012 10:37 am

Listbox type-to-value

Post by owinsloe »

Hi, Does anyone have an example or link to an example of custom code for a listbox control where you can type to a value? The default behaviour supports a single key being typed that will position the listbox at the value location, but a second typed letter does not look at the value's 2nd character, but rather performs the default behaviour again.The default is fine if you have a listbox with few contents, but I have some with hundreds. I have achieved this in Perl capturing character keypress events but pretty sure that this will be simpler in PS. Thanks

User avatar
owinsloe
Posts: 96
Joined: Tue Jul 10, 2012 10:37 am

Listbox type-to-value

Post by owinsloe »

Hi, Does anyone have an example or link to an example of custom code for a listbox control where you can type to a value? The default behaviour supports a single key being typed that will position the listbox at the value location, but a second typed letter does not look at the value's 2nd character, but rather performs the default behaviour again.The default is fine if you have a listbox with few contents, but I have some with hundreds. I have achieved this in Perl capturing character keypress events but pretty sure that this will be simpler in PS. Thanks

jvierra
Posts: 14668
Joined: Tue May 22, 2007 9:57 am
Answers: 5
Has voted: 1 time
Been upvoted: 5 times
Contact:

Listbox type-to-value

Post by jvierra »

That is what a combobox is used for. A listbox has no keyboard so you cannot type into it. A COmboBox has an 'AutoCOmplete' feature that can show you a preview of the next items that match the string you have entered.


See: http://msdn.microsoft.com/en-us/library ... emode.aspx

This is the keyboard you see on listboxes with an edit control which is a combobox.
jvierra2012-07-17 17:05:11

User avatar
owinsloe
Posts: 96
Joined: Tue Jul 10, 2012 10:37 am

Listbox type-to-value

Post by owinsloe »



Normal
0


false
false
false







MicrosoftInternetExplorer4






/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}



-Thanks
(once again).

The new utility I am constructing is based off an earlier Perl Tk version
(we're slowly migrating to PS). I had a reason for not using combobox controls
(which eludes me now) and as I was using listbox in the Perl version, I opted
for the same in PS.

I had a KeyPress event in Perl Tk bound, to do something similar to the below
code, but after much searching and trialling, see that this will not work in PS
as the control has the final say on what the selected item will be.

One reason for using listbox over combo is that everything is within one
control...ie. I dont have a separate field for entry and another for
list-entries (aesthetics) . I had other reasons, but as stated (can't recall)

I will either leave as-is using listbox and single click type-to-value or if I
get too many complaints, may need to revisit the combobox option.

Thanks again

$listbox1_PreviewKeyDown=[System.Windows.Forms.PreviewKeyDownEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PreviewKeyDownEventArgs]
#TODO: Place custom script here
$KeyChar=$_.KeyData
$Index = $listbox1.FindString($KeyChar)
if ( $Index -lt 0 ){ return }
$script:SearchBefore = $script:SearchNow
$script:SearchNow = get-date

# Get total elapsed secs between keystrokes
$ElapsedSecs=0
$ElapsedSecs = $ElapsedSecs + (
($script:SearchNow)-($script:SearchBefore) ).days * (24 * 60 * 60)
$ElapsedSecs = $ElapsedSecs + (
($script:SearchNow)-($script:SearchBefore) ).hours * (60 * 60)
$ElapsedSecs = $ElapsedSecs + (
($script:SearchNow)-($script:SearchBefore) ).minutes * 60
$ElapsedSecs = $ElapsedSecs + (
($script:SearchNow)-($script:SearchBefore) ).seconds
if ( $ElapsedSecs -ge 2 ){
$script:CharString="$KeyChar"
}else{
$script:CharString+="$KeyChar"

}

[int]$Index=0
$Index=$listBox1.FindString("$script:CharString")
if ( $Index -ge 0 ){
$listbox1.SetSelected($Index, $True)
}

}

jvierra
Posts: 14668
Joined: Tue May 22, 2007 9:57 am
Answers: 5
Has voted: 1 time
Been upvoted: 5 times
Contact:

Listbox type-to-value

Post by jvierra »

Here are two examples to go along with Davids blog references.

http://www.designedsystemsonline.com/up ... ox.ps1.txt

http://www.designedsystemsonline.com/up ... x2.ps1.txt

One demos Davids recommendation amd the ohre shows how to make the keyboard really shin as you type.

This is all part of the Windows Standard and the industrie's CUA requirements.

User avatar
owinsloe
Posts: 96
Joined: Tue Jul 10, 2012 10:37 am

Listbox type-to-value

Post by owinsloe »

Guys, Thanks for the assist but IU figured out how to do this using listbox and it seems to work a treat...function Listbox-KeyPress{ Param ( $ListBox, [string]$KeyChar ) # assign the previous selection to the listbox tag if ( !$script:SearchNow ){ [DateTime]$script:SearchNow=Get-Date } [DateTime]$script:SearchBefore = $script:SearchNow [DateTime]$script:SearchNow = get-date # Get total elapsed secs between keystrokes $ElapsedSecs=0 $ElapsedSecs = $ElapsedSecs + ( ($script:SearchNow)-($script:SearchBefore) ).days * (24 * 60 * 60) $ElapsedSecs = $ElapsedSecs + ( ($script:SearchNow)-($script:SearchBefore) ).hours * (60 * 60) $ElapsedSecs = $ElapsedSecs + ( ($script:SearchNow)-($script:SearchBefore) ).minutes * 60 $ElapsedSecs = $ElapsedSecs + ( ($script:SearchNow)-($script:SearchBefore) ).seconds if ( $ElapsedSecs -ge 2 ){ $script:CharString="$KeyChar" return }else{ $script:CharString+="$KeyChar" } [int]$Index=0 $Index=$listBox.FindString("$script:CharString") if ( $Index -lt 0 ){ # Set back to previous selection $Index=$ListboxSelectedIndex } $listbox.ClearSelected() $listbox.SetSelected($index, $True)}$listbox6_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs] #TODO: Place custom script here $KeyChar=$_.KeyChar $_.KeyChar = $null Listbox-KeyPress $listbox6 $KeyChar}

Locked