Home > Software design >  Formatting textbox contents
Formatting textbox contents

Time:01-25

i'm trying to create a gui script for our helpdesk to use to grant users share permissions. I'm currently stuck at textbox with share paths, i want it to accept input, upon "Textchange" event, validate paths and then change text so only valid paths are left and invalid ones would be deleted. The event looks like this:

$Pathbox.Add_Textchanged({
#$PathsToGrant = @()
$this.Text -split '\r?\n' -ne '' | ForEach-Object {
     if ($_ -match "C:" -or $_ -match "ProgramFile" -or $_ -match "D:" -or $_ -match "Appdata")
     {
     $msgboxinput =[System.Windows.Messagebox]::Show("Some text",'Warning','Ok','Warning')
     $mod = "(0,666)"
     }
 elseif (-not$(Test-Path -Path $_))
     { 
     $msgboxinput =[System.Windows.Messagebox]::Show("Some Text",'Warning','Ok','Warning')
     $mod = "(0,666)"
     }
 else 
     {
     $PathsToGrant  = $_
     #Write-Host $PathsToGrant
     }
     }
     Write-Host $PathsToGrant.Count
     $PathBox.Text = ""
     $PathBox.Text = $PathsToGrant 
                           })

I've been adviced here to use $this.Text -split '\r?\n' -ne '' so my textbox would behave like an array instead of 1 long string, but when i try to rewrite the Textbox's text at the end with legit values - it just inserts 2 array objects as 1 row anyway and it creates broken cycle. Maybe i should not have used textbox control in the firt place ? Are there any other more suitable controls for theese kinds of things ? Thank You.

CodePudding user response:

Your $PathsToGrant is a string, not an array. When you add your path like this :

$PathsToGrant  = $_

it is likely you write

$PathsToGrant = $PathsToGrant   "new path"

this is because you have commented your variable declaration, since the first value for $PathsToGrant is a string, the variable becomes a string. So uncomment the second line

$PathsToGrant = @()

If for some reason that I don't understand, it is a problem for you, then add the path within an array like this

$PathsToGrant  = , $_

(the comma is important). No need to do the last if you declare your array (which is best for me)

finally, ensure that your textbox is multiline

 $Pathbox.Multiline = $true

and set the text with a join :

$PathBox.Text = $PathsToGrant -join "`r`n"

However, as a textbox, no choice can be done by selecting one line. You may prefer a listbox (but you cannot type any value) :

$listbox1 = New-Object System.Windows.Forms.ListBox
$listbox1.DataSource = $PathsToGrant

EDIT: if $PathsToGrant is already declared at the start of the script, the $PathsToGrant inside your function is not in the same scope, so it is considered as a new variable when you are setting the value. If this variable needs to be script wide, then keep your variable declaration as commented inside the function $Pathbox.Add_Textchanged :

#$PathsToGrant = @()

and add the values on the script scope variable this way :

$script:PathsToGrant  = $_

you should usually avoid this, but gui scripts can be considered as an exception, especially for event functions to which you cannot pass arguments as you want.

  •  Tags:  
  • Related