Support Forums

Datagrid item selection + "loading" while executing

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.
User avatar
jvierra
Posts: 10326
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid item selection + "loading" while executing

Postby jvierra » Fri Jan 27, 2017 1:33 pm

If you read the article carefully you will notice that the script blocks provided are just example. You have to write your own script blocks or modify the examples to do what you need. The "ArgumentList" is how we pass anything into the job script.. Whatever your script needs in the way of parameters would be passed here. Consider teh script block as if it is a function that the job is going t execute for you.
User avatar
stevens
Posts: 168
Joined: Wed Jun 15, 2016 2:26 am

Re: Datagrid item selection + "loading" while executing

Postby stevens » Thu Feb 16, 2017 1:03 pm

The job, I have made it working. Thanks for that. However the datagrid item selection, I'm struggling with it for a long time now.

You mentioned before that I could take the $textbox value and should not define $global:computername but my whole script is built upon it and I'm not only selecting computername, username, but also other values in other datagridviews.
I posted my full script to your server (for review due to hanging form on a RDS-server) but below is the code for item selection.

Problem is that selection occurs ok first few time but when clicking several items (user/computer) in the gridview it doesn't refresh the logs textbox "your selection: ..."

Please advise.

This is the code you posted to tell me "how to use datagridview selection"
$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#$richtextboxoutput.Lines += "You selected $($datagridview1.Rows[$_.RowIndex].Cells['Name'].Value)"
$ComputerName = "$($datagridview1.Rows[$_.RowIndex].Cells[0].Value)"
$Date = "$($datagridview1.Rows[$_.RowIndex].Cells[1].Value)"
 
$richtextboxoutput.Lines += "You selected Computer $ComputerName and date $Date"
 
$richtextboxoutput.ScrollToCaret()
 
}
 
$textbox1_Validated={
$NameContainsWildcard = '*' + $textbox1.Text + '*'
Write-Host $NameContainsWildcard -fore green
$computers = Get-ADComputer -Filter "Name -like '$NameContainsWildcard'" -Properties * |
Select-Object Name, LastLogonDate, IPv4Address, OperatingSystem, OperatingSystemVersion, Enabled, WhenChanged, DistinGuishedName
if($computers){
Load-DataGridView -DataGridView $datagridview1 -Item $computers
}
}



This is my code, I don't see where it could be wrong:

$datagridviewComputer_CellContentClick = [System.Windows.Forms.DataGridViewCellEventHandler]{
try
{
$Global:ComputerName = "$($datagridviewComputer.Rows[$_.RowIndex].Cells[0].Value)"
$Global:UserName = "$($datagridviewComputer.Rows[$_.RowIndex].Cells[1].Value)"
Add-Logs -text "Your selection: Computer $ComputerName and User $UserName"
}
catch
{
set-catch
}
 
}


Note:
$ComputerName = $txtBoxComputerSearch.Text
$ComputersSearchResult = Get-ComputerName -NameContains $ComputerName
$datagridviewComputer.DataSource = ConvertTo-DataTable -InputObject $ComputersSearchResult
User avatar
jvierra
Posts: 10326
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid item selection + "loading" while executing

Postby jvierra » Thu Feb 16, 2017 1:46 pm

WHy all of the quotes?

$Global:ComputerName = $datagridviewComputer.Rows[$_.RowIndex].Cells[0].Value
$Global:UserName = $datagridviewComputer.Rows[$_.RowIndex].Cells[1].Value

Use your debugger to inspect what is happening.
CellClick occurs on every cell click and will constantly rewrite your values so you need to code accordingly.
User avatar
stevens
Posts: 168
Joined: Wed Jun 15, 2016 2:26 am

Re: Datagrid item selection + "loading" while executing

Postby stevens » Fri Feb 17, 2017 6:35 am

Thanks, but there is not really something to debug. Sometimes the selection (clicking a cell) does happen correctly and it shows "your selection is $username, $computername".
Then suddenly this mechanisms stops (no errors) and nothing happens till you do some extra clicks and it works again.
User avatar
jvierra
Posts: 10326
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid item selection + "loading" while executing

Postby jvierra » Fri Feb 17, 2017 10:28 am

Sounds like either a corrupt PSF file or some piece of code that is causing the focus to change before the event occurs. What other events are you handling on the grid and on the form.
User avatar
stevens
Posts: 168
Joined: Wed Jun 15, 2016 2:26 am

Re: Datagrid item selection + "loading" while executing

Postby stevens » Mon Feb 20, 2017 4:32 am

Corrupt PSF file? How could I discover if that is the case?

There are quite some events on the form, but they are all in the rightmouse button, whereas the leftclick is only on $datagridviewComputer_CellContentClick
I have this issue from day one I created this project (almost a year ago).
I'd be so glad to solve it but don't have any idea where to look.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: stevens and 2 guests