Combo Box Problem

by chrisuk11 at 2013-05-01 13:28:49

I have a form based powershell tool.

I am trying to populate a combobox from a text file. This works fine.

However the only way I can get the box to populate is to use the on_click event button, however this means that my combo box keeps populating (duplicating the choices) every time I click on it

Code used is as follows -

function FNNewStarterJobDescription {
#$NewStarterJobDescriptionComboBox.items.clear()

$list = Get-content "c:\list.txt"

foreach ($line in $list) {
$NewStarterJobDescriptionComboBox.Items.Add($line) }
$NewStarterJobDescriptionComboBox.EndUpdate()
}
$NewStarterJobDescriptionComboBox_SelectedIndexChanged={
#TODO: Place custom script here
FNNewStarterJobDescription
}


I am using powershell studio if this is relevant

Thanks

Chris
by veroli at 2013-05-02 08:48:08
Chris,
you need to look at the SelectedIndexChanged

for example
$comboBox2_SelectedIndexChanged=
{
#do some stuff
$comboBox2.Items.clear()
}

then after this you will need to define it by adding
$comboBox2.add_SelectedIndexChanged($comboBox2_SelectedIndexChanged)
so when you actually click on it it will update the comboboxdan
by chrisuk11 at 2013-05-02 10:09:42
Hi,

Thanks for the response, but I don’t think I made it very clear what exactly the problem was.

The combobox doesn’t need to update (It just needs to read in the text file and display these options). The problem is I can’t get the combo box to list anything, unless I choose an event, such as "on click" run Function "FNNewStarterJobDescription"

This means that every time I clickm the box, it populates it again with the text file data giving me duplicates, if I click on it a third time, it populates again giving me 3 of each value and so on.

Hope this helps to explain my issue :slight_smile:

Thanks
by veroli at 2013-05-03 01:34:46
HI Chris,

Ok i understand now i was in a bit of a rush yesterday and wanted to help quickly.
There are a number of ways to accomplish what you are doing and i used to use a for loop like you are doing.
However this is much more elegant I think, although probably many people will disagree :frowning:


$list = Get-content .\list.txt
$NewStarterJobDescriptionComboBox.Items.Addrange($list)


hope this helps
dan
by chrisuk11 at 2013-05-03 10:29:54
thanks Dan,

That’s exactly what I needed.

So close yet so far!!
by chrisuk11 at 2013-05-03 11:11:08
Unsure If I shopuld raise a new thread or just continue with this…

The combox box works fine. I have now used the same format to add another combobox to the same form, however it does not work. However If i stop the first combo box from working the second poluates fine!?

Here is the code -

function FNNewStarterJobDescription {

$NewStarterJobDescriptionList = Get-content C:\Users\Chris\Documents\HelpDeskToolConfig\JobDescription.txt
$NewStarterJobDescriptionComboBox.Items.Addrange($NewStarterJobDescriptionList)
}
$NewStarterJobDescriptionComboBox_SelectedIndexChanged={
#TODO: Place custom script here
FNNewStarterJobDescription
#FNNewStarterDepartment
}


#New Starter Tab - Department

function FNNewStarterDepartment {

$NewStarterJobDepartmentList = Get-content C:\Users\Chris\Documents\HelpDeskToolConfig\Departments.txt
$NewStarterDepartmentComboBox.Items.Addrange($NewStarterJobDepartmentList)

}
$NewStarterDepartmentComboBox_SelectedIndexChanged={
#TODO: Place custom script here
FNNewStarterDepartment

}
by chrisuk11 at 2013-05-03 13:30:17
Ok,

Scrap my last 2 responses. It doesn’t populate the combo boxes at all. The reason it was working -

The tool has numerous tabs on it; I added an event so that when you clicked on the tab where my combo boxes are, it would populate the combo boxes (hence I thought the amended script worked). Obviously I then had issues populating my 2nd combo box (this was because I hadn’t added an event, when you clicked the tab to populate the 2nd combo box).

Hope the above makes sense.

So my first question still stands. Can I not populate the combox boxes without some form of trigger? As I am still getting the same issue, every time I click the tab it populates the combo box and so I end up with duplicate entrties in the combo box. (If you click the tab numerous times)

Again apologises for the mis-information in the last 2 posts, this is what happens when you start trying things and then come back to it a day or 2 later!! :slight_smile:
by chrisuk11 at 2013-05-03 13:38:35
Update - I have a workaround

function FNNewStarterJobDescription {
$NewStarterJobDescriptionComboBox.Items.clear()
$NewStarterJobDescriptionList = Get-content C:\Users\Chris\Documents\HelpDeskToolConfig\JobDescription.txt
$NewStarterJobDescriptionComboBox.Items.Addrange($NewStarterJobDescriptionList)
}


The above is set to run when I click on the tab. This means that when I click on the combo box it doesn’t clear my selection (This was happening when I originally set the function to run when you clicked on the combo box itself).

HThanks to Dan for his suggestions as it has allowed me to have this workaround. I assume that the form has to have some form of trigger then to populate the combo boxes…