Autocomplete text and additions

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

Autocomplete text and additions

Post by jimbobukii »

I have been looking at the textbox auto complet function, very good.

A simple form:
PowerShell Code
Double-click the code block to select all.
$form1_Load={
	#TODO: Initialize Form Controls here
	$list = Get-Content "c:\list.txt"
}

$textbox1_TextChanged={
	#TODO: Place custom script here
	$textbox1.AutoCompleteCustomSource.AddRange($list)
}

$button1_Click={
	#TODO: Place custom script here
	#[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][System.Windows.Forms.MessageBox]::Show("Selected = $($textbox1.Text)","")
}
What I would like to do is if an item is not in the list, when I click button1 it is added to $list(file) and the msgbox is then presented again
It does not add if it already exists so std append is no good

What I would also like is $list to be displayed as is. ie. $list contains:
  • One
    Two
    Three
using
PowerShell Code
Double-click the code block to select all.
$textbox1.AutoCompleteCustomSource.AddRange($list)
The list is sorted alphabetically

Any help?

Thanks in advance
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Autocomplete text and additions

Post by davidc »

I'm not sure I quite understand what you want.

If you want to display the complete list, then I recommend using the ComboBox control instead. You ComboBox can also sort the items.

If you want to add to a new item to the list then do the following:
PowerShell Code
Double-click the code block to select all.
$button1_Click={
	#TODO: Place custom script here
	$textbox1.AutoCompleteCustomSource.Add($textbox1.Text)
}
You might want to check if the text isn't already in the list before doing so.

David
David
SAPIEN Technologies, Inc.
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

Re: Autocomplete text and additions

Post by jimbobukii »

Thanks helped perfectly

I was wondering how to refresh the textbox so if any new lines were added to the file they would appear in the text box custom source without having to exit the form?
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

Re: Autocomplete text and additions

Post by jimbobukii »

its ok I worked this out :)
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked