Author Posts

October 2, 2017 at 1:24 pm

Hi all. I have a GUI form that has a combobox bound to a csv using .datasource. The csv records (Printers and their IP addresses) populate the combobox but not in the way i wanted them to. I currently see each record in the format printername, IPAddress (ACCPrint-04, 10.10.20.121). I'd like to see just the printer name in the combo but be able to use the IP in the add_SelectedIndexChanged event after selecting a printer (to open the web console for the selected printer)

Code creating and populating the combobox. PrinterIPtxt is my test text box where i am attempting to display only the IP address:

#CREATE AND ADD PRINTERS COMBOBOX
$PrinterCombo = New-Object system.Windows.Forms.ComboBox
$PrinterCombo.location = New-Object system.Drawing.Size(310,470)
$PrinterCombo.size = New-Object System.Drawing.Size(200,20)
$PrinterCombo.DropDownStyle = "Dropdownlist"
$PrinterCombo.AutoCompleteMode = "Suggest"
$PrinterCombo.FlatStyle = "Flat"
$PrinterCombo.Items.Clear()
$Header = "PrinterName", "PrinterIP"

$PrinterList = Import-Csv -Delimiter "`t" -Path "C:\Scripts\PoShForm\PrintersandPortsFixed.csv" -Header $Header | select -ExpandProperty PrinterName

$PrinterCombo.DataSource = ($PrinterList)

$PrinterCombo.DisplayMember = 'PrinterName'

$PrinterCombo.ValueMember = 'PrinterIP'

$Form.controls.add($PrinterCombo)

$PrinterCombo.add_SelectedIndexChanged({GetPrinterSelected})

$PrinterIPtxt = New-Object system.Windows.Forms.TextBox
$PrinterIPtxt.location = New-Object system.Drawing.Size(310,500)
$PrinterIPtxt.size = New-Object System.Drawing.Size(100,20)

$Form.Controls.Add($PrinterIPtxt)

Code fired at event:

function GetPrinterSelected {
	$PrinterIPtxt.Text = $PrinterCombo.SelectedItem
} #end GetPrinterSelected

I hope i post this ok, first attempt here. Thanks in advance for all your help on this, i don't have much more hair to pull out.

October 3, 2017 at 3:39 pm

Couple things pop out. It looks like you're specifying a tab delimiter and then piping your imported CSV to Select just the PrinterName property. If that were working as intended, you shouldn't be seeing both the Name and IP in your ComboBox. Given that you're seeing both separated by a comma in your display, I'm guessing your CSV is comma delimited and you can remove the "-Delimiter '`t'" portion of your Import-CSV command.

Doing that would fix your display issue, but it would also break the ability to access the IP address.

I would modify lines 11-13 like so:
$PrinterList = Import-Csv -Path "C:\Scripts\PoShForm\PrintersandPortsFixed.csv" -Header $Header
$PrinterNames = $PrinterList | select -ExpandProperty PrinterName
$PrinterCombo.DataSource = ($PrinterNames)

Then in your function declaration do this:
function GetPrinterSelected {
$PrinterIPtxt.Text = ($PrinterList | where PrinterName -eq $PrinterCombo.SelectedItem).PrinterIP
} #end GetPrinterSelected

You may also need to change $PrinterCombo.SelectedItem to $PrinterCombo.SelectedItem.ToString().

October 4, 2017 at 7:06 am

Hi Eli. Firstly, that works!! Thank you very much. I now see only the PrinterName value in the combo box and can see the correct IP address in the text box after selection. Thanks for the help with this