Support Forums

We are currently experiencing failures on the SAPIEN store and are working hard to rectify the situation. In the meantime, please DO NOT place an order through the store. Instead, please contact us directly at sales@sapien.com. We apologize for any inconvenience.

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

Re: Datagrid item selection + "loading" while executing

Postby jvierra » Mon Feb 20, 2017 7:29 am

stevens wrote: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.


There is no simple answer to your issue. If you had a design or program bug from the beginning then it is likely you have now built dependencies into the code. It is critical in programming that all "bugs" or issues be resolved before adding more code.
Events in a form can and do influence events that occur later. Finding tis can be difficult. Coding errors can make a form misbehave.

The PSF file and the support files can get corrupted by nulls and other unprintable characters although my suspicion is that you have a subtle coding or design error.

I would start by deleing the support files in the project folder. Look for files with "TempPoint" in the name and delete.

Your issue is one that all new programmers arrive at sooner or later. You have coded yourself into a corner. Now you will have to backtrack and try to see where you made your mistake. There is no magic way to do this. Use the debugger to try und understand what your code is doing and hopefully you will see the mistake or detect where you are altering the event context.
User avatar
stevens
Posts: 218
Joined: Wed Jun 15, 2016 2:26 am

Re: Datagrid item selection + "loading" while executing

Postby stevens » Tue Feb 28, 2017 1:33 am

Ok, I give up now. Can't figure it out why the form behaves like that.
User avatar
stevens
Posts: 218
Joined: Wed Jun 15, 2016 2:26 am

Re: Datagrid item selection + "loading" while executing

Postby stevens » Thu Mar 02, 2017 9:54 am

Got a brand new form with almost no code and exactly the same problem(!)
User avatar
jvierra
Posts: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid item selection + "loading" while executing

Postby jvierra » Thu Mar 02, 2017 4:09 pm

Unfortunately I still can't quite understand what your problem is. Perhaps just a simple statement of what you are trying to do and what is not working. So far it seems you are trying to do something that cannot be done.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest