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.

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 7 years and 1 week 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid item selection + "loading" while executing

Post by jvierra »

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Datagrid item selection + "loading" while executing

Post by stevens »

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"
  1. $datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     #$richtextboxoutput.Lines += "You selected $($datagridview1.Rows[$_.RowIndex].Cells['Name'].Value)"
  4.     $ComputerName =  "$($datagridview1.Rows[$_.RowIndex].Cells[0].Value)"
  5.     $Date = "$($datagridview1.Rows[$_.RowIndex].Cells[1].Value)"
  6.    
  7.     $richtextboxoutput.Lines += "You selected Computer $ComputerName and date $Date"
  8.  
  9.     $richtextboxoutput.ScrollToCaret()
  10.    
  11. }
  12.  
  13. $textbox1_Validated={
  14.     $NameContainsWildcard = '*' + $textbox1.Text + '*'
  15.     Write-Host $NameContainsWildcard -fore green
  16.     $computers = Get-ADComputer -Filter "Name -like '$NameContainsWildcard'" -Properties * |
  17.         Select-Object Name, LastLogonDate, IPv4Address, OperatingSystem, OperatingSystemVersion, Enabled, WhenChanged, DistinGuishedName
  18.     if($computers){
  19.         Load-DataGridView -DataGridView $datagridview1 -Item $computers
  20.     }
  21. }

This is my code, I don't see where it could be wrong:
  1. $datagridviewComputer_CellContentClick = [System.Windows.Forms.DataGridViewCellEventHandler]{
  2.     try
  3.     {
  4.         $Global:ComputerName = "$($datagridviewComputer.Rows[$_.RowIndex].Cells[0].Value)"
  5.         $Global:UserName = "$($datagridviewComputer.Rows[$_.RowIndex].Cells[1].Value)"
  6.         Add-Logs -text "Your selection: Computer $ComputerName and User $UserName"
  7.     }
  8.     catch
  9.     {
  10.         set-catch
  11.     }
  12.    
  13. }
Note:
$ComputerName = $txtBoxComputerSearch.Text
$ComputersSearchResult = Get-ComputerName -NameContains $ComputerName
$datagridviewComputer.DataSource = ConvertTo-DataTable -InputObject $ComputersSearchResult
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid item selection + "loading" while executing

Post by jvierra »

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Datagrid item selection + "loading" while executing

Post by stevens »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid item selection + "loading" while executing

Post by jvierra »

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Datagrid item selection + "loading" while executing

Post by stevens »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid item selection + "loading" while executing

Post by jvierra »

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Datagrid item selection + "loading" while executing

Post by stevens »

Ok, I give up now. Can't figure it out why the form behaves like that.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Datagrid item selection + "loading" while executing

Post by stevens »

Got a brand new form with almost no code and exactly the same problem(!)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagrid item selection + "loading" while executing

Post by jvierra »

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.
This topic is 7 years and 1 week 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