DataGridView in Runspace Scrollbar error

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 6 years and 10 months 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
User avatar
r41z0r
Posts: 8
Last visit: Sun Mar 04, 2018 10:36 pm

DataGridView in Runspace Scrollbar error

Post by r41z0r »

Hello,

I have a problem, if I run a Task for query the eventlog in an runspace, pass my Datagridview to that runspace with a SessionStateProxy Variable and fill the DataGridView with elements in the runspace, it works fine to fill and show the result in the gui, but the problem is, that the scrollbar isn't working. It's displaying but the user can't interact with it:

Here is a little example of my problem, am I doing anything wrong?
  1. $global:Thread = [PowerShell]::Create().AddScript(
  2.  
  3. #------------------------------------------------------------------------
  4. # Source File Information (DO NOT MODIFY)
  5. # Source ID: bb36ea54-55c6-48e0-aba7-96d95fb99734
  6. # Source File:
  7. #------------------------------------------------------------------------
  8.  
  9. <#
  10.     .NOTES
  11.     --------------------------------------------------------------------------------
  12.      Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.139
  13.      Generated on:       24.05.2017 10:23
  14.      Generated by:      
  15.     --------------------------------------------------------------------------------
  16.     .DESCRIPTION
  17.         GUI script generated by PowerShell Studio 2017
  18. #>
  19.  
  20.  
  21. #----------------------------------------------
  22. #region Application Functions
  23. #----------------------------------------------
  24.  
  25. #endregion Application Functions
  26.  
  27. #----------------------------------------------
  28. # Generated Form Function
  29. #----------------------------------------------
  30. function Show-Test_psf {
  31.  
  32.     #----------------------------------------------
  33.     #region Import the Assemblies
  34.     #----------------------------------------------
  35.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  36.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  37.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  38.     #endregion Import Assemblies
  39.  
  40.     #----------------------------------------------
  41.     #region Generated Form Objects
  42.     #----------------------------------------------
  43.     [System.Windows.Forms.Application]::EnableVisualStyles()
  44.     $form1 = New-Object 'System.Windows.Forms.Form'
  45.     $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
  46.     $Column1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
  47.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  48.     #endregion Generated Form Objects
  49.  
  50.     #----------------------------------------------
  51.     # User Generated Script
  52.     #----------------------------------------------
  53.     $form1_Load={
  54.         #TODO: Initialize Form Controls here
  55.         $global:Thread = [PowerShell]::Create().AddScript(
  56.             {
  57.                 for ($i = 0; $i -lt 1000; $i++)
  58.                 {
  59.                     $datagridview1.Rows.Add("$i")
  60.                 }
  61.             })
  62.         $MultiThread = [RunspaceFactory]::CreateRunspace()
  63.         $MultiThread.ApartmentState = "STA"
  64.        
  65.         $MultiThread.Open()
  66.         $MultiThread.SessionStateProxy.SetVariable("datagridview1", $datagridview1)
  67.        
  68.         $global:Thread.Runspace = $MultiThread
  69.         $global:Thread.BeginInvoke()
  70.     }
  71.    
  72.     # --End User Generated Script--
  73.     #----------------------------------------------
  74.     #region Generated Events
  75.     #----------------------------------------------
  76.    
  77.     $Form_StateCorrection_Load=
  78.     {
  79.         #Correct the initial state of the form to prevent the .Net maximized form issue
  80.         $form1.WindowState = $InitialFormWindowState
  81.     }
  82.    
  83.     $Form_Cleanup_FormClosed=
  84.     {
  85.         #Remove all event handlers from the controls
  86.         try
  87.         {
  88.             $form1.remove_Load($form1_Load)
  89.             $form1.remove_Load($Form_StateCorrection_Load)
  90.             $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  91.         }
  92.         catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
  93.     }
  94.     #endregion Generated Events
  95.  
  96.     #----------------------------------------------
  97.     #region Generated Form Code
  98.     #----------------------------------------------
  99.     $form1.SuspendLayout()
  100.     #
  101.     # form1
  102.     #
  103.     $form1.Controls.Add($datagridview1)
  104.     $form1.AutoScaleDimensions = '6, 13'
  105.     $form1.AutoScaleMode = 'Font'
  106.     $form1.ClientSize = '284, 261'
  107.     $form1.Name = 'form1'
  108.     $form1.Text = 'Form'
  109.     $form1.add_Load($form1_Load)
  110.     #
  111.     # datagridview1
  112.     #
  113.     $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
  114.     [void]$datagridview1.Columns.Add($Column1)
  115.     $datagridview1.Dock = 'Fill'
  116.     $datagridview1.Location = '0, 0'
  117.     $datagridview1.Name = 'datagridview1'
  118.     $datagridview1.Size = '284, 261'
  119.     $datagridview1.TabIndex = 0
  120.     #
  121.     # Column1
  122.     #
  123.     $Column1.AutoSizeMode = 'Fill'
  124.     $Column1.HeaderText = 'Test'
  125.     $Column1.Name = 'Column1'
  126.     $form1.ResumeLayout()
  127.     #endregion Generated Form Code
  128.  
  129.     #----------------------------------------------
  130.  
  131.     #Save the initial state of the form
  132.     $InitialFormWindowState = $form1.WindowState
  133.     #Init the OnLoad event to correct the initial state of the form
  134.     $form1.add_Load($Form_StateCorrection_Load)
  135.     #Clean up the control events
  136.     $form1.add_FormClosed($Form_Cleanup_FormClosed)
  137.     #Show the Form
  138.     return $form1.ShowDialog()
  139.  
  140. } #End Function
  141.  
  142. #Call the form
  143. Show-Test_psf | Out-Null
Unbenannt.png
Unbenannt.png (39.46 KiB) Viewed 2596 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

As long as the runspace is looping on the grid variable it will be locked.

Consider using a JobTracker custom control set to load the grid.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

Yuo need to post a short PSF with a simple example of your issue.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

Here is one workaround for the scrollbar issue:
  1. $form1_Load={
  2.     $dataGridView1.ScrollBars = 'None'
  3.     $global:Thread = [PowerShell]::Create().AddScript(
  4.         {
  5.             for ($i = 0; $i -lt 1000; $i++) {
  6.                 $x = $datagridview1.Rows.Add("$i")
  7.                 $dataGridView1.FirstDisplayedScrollingRowIndex = $dataGridView1.RowCount-1      }
  8.         })
  9.     $MultiThread = [RunspaceFactory]::CreateRunspace()
  10.     $MultiThread.ApartmentState = "STA"
  11.    
  12.     $MultiThread.Open()
  13.     $MultiThread.SessionStateProxy.SetVariable("datagridview1", $datagridview1)
  14.    
  15.     $global:Thread.Runspace = $MultiThread
  16.     $global:Thread.Invoke()
  17.     $dataGridView1.ScrollBars = 'Vertical'
  18. }
User avatar
r41z0r
Posts: 8
Last visit: Sun Mar 04, 2018 10:36 pm

Re: DataGridView in Runspace Scrollbar error

Post by r41z0r »

Amazing, working like a charm.
Thanks for that

Damn, to early to be happy.
I need to use it with BeginInvoke(), so that the window isn't freezing.
Damn...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

Good. It took me about 15 minutes to realize that it was likely a bug. Searched and found the answer.

Always search for a short description of the condition. You will very often find an answer.
This topic is 6 years and 10 months 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