Remote Desktop application GUI

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 2 years and 8 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
rweiss68
Posts: 5
Last visit: Tue Jul 06, 2021 1:38 am

Remote Desktop application GUI

Post by rweiss68 »

Hello,

I'm new to WinForm and I need help concerning filtering.
I created a GUI that list the TS active session of my servers and allow me to take control of a session using a "connect" button.
The list is working, the connect button too, but I'm trying to create an interactive filter textbox, I want that when I enter "r" in my textbox, all ServerName or Username or any column containing "r" are directly filtered.
How can I implement that ?

Thanks for reading me

My code :

Code: Select all

$ErrorActionPreference = 'SilentlyContinue'
Import-Module PSTerminalServices

function GetTsSession{
$servers = (Get-ADComputer -filter * | where {$_.name -like '*PUT_SERVERNAME_HERE*'}).DNSHostName | sort-object

#Active Session List
$Result = foreach ($serv in $servers){
    if(Test-Connection $serv -Count 1 )
    {  
    Get-TSSession -ComputerName $serv -State Active
    }
    Else {
    #Write-Host "No Ping :" $serv 
    }  
}
$Result | select @{n='Server';e={$_.server.ServerName}},SessionID,ClientName,Username
}

function RemoteControl {
$srv=$dataGridView1.CurrentRow.Cells['server'].Value
$id=$dataGridView1.CurrentRow.Cells['sessionid'].Value
mstsc /v:$srv /shadow:$id /control
}

#This filter function reload the entire datagridview1...
function funfilter {
$TBfilter=$textBox.text
GetTsSession | where-object {$_ -like "*$TBfilter*"}
}

function Call-MainForm_pff {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$MainForm = New-Object 'System.Windows.Forms.Form'
	$dataGridView1 = New-Object 'System.Windows.Forms.DataGridView'
	$refreshButton = New-Object 'System.Windows.Forms.Button'
    $connectionButton = New-Object 'System.Windows.Forms.Button'
    $textBox = New-Object 'System.Windows.Forms.TextBox'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$OnLoadFormEvent={
	#TODO: Initialize Form Controls here
	}
	
	$refreshButton_Click={
		#the filter function is disabled here
	        #$list=[system.Collections.ArrayList](funfilter)
        	$list=[system.Collections.ArrayList](GetTsSession)
		$dataGridView1.DataSource=$list
	}

	$remoteButton_Click={
	    $list=[system.Collections.ArrayList](RemoteControl)
	}
    $textBox_KeyDown = [System.Windows.Forms.KeyEventHandler]{
 
        if ($_.KeyCode -eq 'Enter')
        {
            $refreshButton_Click.Invoke()
            #Suppress sound from unexpected use of enter on keyPress/keyUp
            $_.SuppressKeyPress = $true
        }
    }
	
	
		# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$MainForm.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$refreshButton.remove_Click($refreshButton_Click)
            $remoteButton.remove_Click($remoteButton_Click)
            $textBox.remove_Keydown($textBox_KeyDown)
			$MainForm.remove_Load($OnLoadFormEvent)
			$MainForm.remove_Load($Form_StateCorrection_Load)
			$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	#
	# MainForm
	#
	$MainForm.Controls.Add($dataGridView1)
	$MainForm.Controls.Add($refreshButton)
    $MainForm.Controls.Add($connectionButton)
    $MainForm.Controls.Add($textBox)
	$MainForm.ClientSize = '520, 900'
	$MainForm.Name = "MainForm"
	$MainForm.StartPosition = 'CenterScreen'
	$MainForm.Text = "Remote Control GUI"
    $MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
    $MainForm.MaximizeBox = $False
	$MainForm.add_Load($OnLoadFormEvent)
	#
	# datagridview1
	#
	$dataGridView1.ColumnHeadersHeightSizeMode = 'AutoSize'
	$dataGridView1.Location = '10, 50'
	$dataGridView1.Name = "datagridview1"
	$dataGridView1.Size = '500, 840'
    $dataGridView1.AutoSizeColumnsMode = 'AllCells'
    $dataGridView1.AllowUserToResizeRows = $false
    $dataGridView1.MultiSelect = $false
    $dataGridView1.AllowUserToAddRows = $false
    $dataGridView1.ReadOnly = $true
    $dataGridView1.SelectionMode = 'FullRowSelect'
	#
	# refreshButton
	#
	$refreshButton.Location = '10,24'
	$refreshButton.Name = "refreshButton"
    $refreshButton.Text = "Search"
	$refreshButton.UseVisualStyleBackColor = $True
	$refreshButton.add_Click($refreshButton_Click)
	#
	# connectionButton
	#
	$connectionButton.Location = '90,24'
	$connectionButton.Name = "connectionButton"
	$connectionButton.Text = "Connection"
	$connectionButton.UseVisualStyleBackColor = $True
	$connectionButton.add_Click($remoteButton_Click)
	#
	# textBox
	#
	$textBox.Location = '10, 1'
	$textBox.Name = "textBox"
	$textBox.Size = '500,22'
	$textBox.UseVisualStyleBackColor = $True
    $textBox.add_Keydown($textBox_KeyDown)
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $MainForm.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$MainForm.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $MainForm.ShowDialog()

} #End Function

#Call the form
Call-MainForm_pff | Out-Null
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Desktop application GUI

Post by jvierra »

What columsn? What textbox? Your question is too vague.
rweiss68
Posts: 5
Last visit: Tue Jul 06, 2021 1:38 am

Re: Remote Desktop application GUI

Post by rweiss68 »

Hello jvierra,

I have a textBox on top of my GUI,I first added a keydown when I push enter button to test the behavior but it's not for interactive filter

Code: Select all

	
$textBox.Location = '10, 1'
$textBox.Name = "textBox"
$textBox.Size = '500,22'
$textBox.UseVisualStyleBackColor = $True
I have 4 columns
Server / SessionID / ClientName / UserName
example:
Server1 / 12 / computer1 / sergei

I want that the filter work for all columns : the ServerName column as weel as the sessionID, clientname and username.
if I type "ser" on the textbox I should be able to see the row that contains "server1" and the row that contain "sergei" or whatever name containing ser (server,clientname,username)

I'm a new user so I cannot post a link, but if you replace the _ with / you could see what I mean with my screenshot
Hope it's not wrong to do that ? otherwise I will edit the topic, just told me
link to a screenshot : i.ibb.co_mt5Yp5j_remote-Control.jpg

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Desktop application GUI

Post by jvierra »

Four colums of what? Four colums of buttons, textboxes, ListBoxes?

Here is a place to start: viewtopic.php?t=11041
rweiss68
Posts: 5
Last visit: Tue Jul 06, 2021 1:38 am

Re: Remote Desktop application GUI

Post by rweiss68 »

Four colums of listbox
Like I said I'm noob to winform, just working with powershell at normal time.
I will focus on your link and Demo-DGVSortFormat.psf, this psf could not be executed as it is but I will study the content to know how can I use this
rweiss68
Posts: 5
Last visit: Tue Jul 06, 2021 1:38 am

Re: Remote Desktop application GUI

Post by rweiss68 »

Thanks for your help jvierra.
I made it work but I have one more question concerning the filter:
$datagridview1.DataSource.defaultview.rowfilter = "UserName LIKE '$($textBox.text)*'"

'$($textBox.text)* work like "search for string that start with $textbox.text
How can I change it to "contain" and not "start with"?

And if there is more column (1 column servername and 1 username) that I want to filter, how can I write this
$datagridview1.DataSource.defaultview.rowfilter = "UserName LIKE '$($textBox.text)*' OR ServerName LIKE '$($textBox.text)*'" ?


Regards
Last edited by rweiss68 on Tue Jul 06, 2021 1:14 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Desktop application GUI

Post by jvierra »

You cannot use single quotes in that way in a SQL expression.
  1. $rowfilter = "UserName LIKE '{0}*' OR ServerName LIKE '{0}*'"  -f $textbox.Text
  2. $datagridview1.DataSource.defaultview.rowfilter = $rowfilter
rweiss68
Posts: 5
Last visit: Tue Jul 06, 2021 1:38 am

Re: Remote Desktop application GUI

Post by rweiss68 »

Wow thanks a lot.
That is exactly what I was looking for.
Have a beautiful day :D

PS: I just used '*{0}*' instead of '{0}*'

$rowfilter = "UserName LIKE '*{0}*' OR ServerName LIKE '*{0}*'" -f $textbox.Text
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Desktop application GUI

Post by jvierra »

The easiest way to build strings is to use teh string formatter. I recommend looking it up to see the power it enables when working with strings.

String formatters are one of the oldest and most used tool iin any programming language.
This topic is 2 years and 8 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