locked
Richtextbox problem RRS feed

  • General discussion

  • Hi,

    why if I add this code to richtextbox , won't see it , when I run it only in powershell console it is ok.

    "$user = Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |  Format-Table -Property Folder, @{ Label = "Size of Folder"; Expression = { Class-Size($_.Length) } } | Out-String
     Add-RichTextBox $user

    "

    Function Richtextbox is here :

    #region Add-RichTextBox
    # Function - Add Text to RichTextBox
    function Add-RichTextBox{
        [CmdletBinding()]
        param ($text)
        #$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
        $richtextbox_output.Text += "$text"
        $richtextbox_output.Text += "`n# # # # # # # # # #`n"
    }
    #Set-Alias artb Add-RichTextBox -Description "Add content to the RichTextBox"
    #endregion

    thanx for answer

    Tuesday, November 19, 2019 6:17 AM

All replies

  • Hi,

    why if I add this code to richtextbox , won't see it , when I run it only in powershell console it is ok.

    "$user = Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |  Format-Table -Property Folder, @{ Label = "Size of Folder"; Expression = { Class-Size($_.Length) } } | Out-String
     Add-RichTextBox $user

    "

    Function Richtextbox is here :

    #region Add-RichTextBox
    # Function - Add Text to RichTextBox
    function Add-RichTextBox{
        [CmdletBinding()]
        param ($text)
        #$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
        $richtextbox_output.Text += "$text"
        $richtextbox_output.Text += "`n# # # # # # # # # #`n"
    }
    #Set-Alias artb Add-RichTextBox -Description "Add content to the RichTextBox"
    #endregion

    thanx for answer

    • Merged by jrv Tuesday, November 19, 2019 6:34 AM DUPLICATE
    Tuesday, November 19, 2019 6:20 AM
  • The easiest way is like this:

    $form = [system.windows.forms.form]::New()
    $rtb = [System.Windows.Forms.RichTextBox]::new()
    $btn = [System.Windows.Forms.Button]::new()
    $form.Controls.AddRange(@($rtb,$btn))
    $form.StartPosition = 'CenterScreen'
    $rtb.Anchor = 'left,right,bottom'
    $rtb.Top = 40
    $rtb.Width = 250
    $btn.Text = 'click me'
    $btn.add_click({$rtb.Lines += 'Hello world'})
    
    $form.ShowDialog()

    Click multiple time to see how this works.


    \_(ツ)_/



    • Edited by jrv Tuesday, November 19, 2019 6:47 AM
    Tuesday, November 19, 2019 6:45 AM
  • here is full code still dont work :(

        param ($Path = "c:\Users"
            #[Parameter(Position = 0, ValueFromPipeline = $True, Mandatory = $True)]
            #[ValidateNotNullOrEmpty()]
            #[System.String]$Path
        )
            
        function Get-FolderSize ($_ = (get-item .))
        {
            Process
            {
                $ErrorActionPreference = "SilentlyContinue"
                $length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
                $obj = New-Object PSObject
                $obj | Add-Member NoteProperty Folder ($_.FullName)
                $obj | Add-Member NoteProperty Length ($length)
                Write-Output $obj
            }
        }
        
        
        Function Class-Size($size)
        {
            
            IF ($size -ge 1GB)
            {
                "{0:n2}" -f ($size / 1GB) + " GigaBytes"
            }
            ELSEIF ($size -ge 1MB)
            {
                "{0:n2}" -f ($size / 1MB) + " MegaBytes"
            }
            ELSE
            {
                "{0:n2}" -f ($size / 1KB) + " KiloBytes"
            }
        }
        
        $text = Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |  Format-Table -Property Folder, @{ Label = "Size of Folder"; Expression = { Class-Size($_.Length) } }
        $richtextbox.AppendText($text)
    }

    ****************************

    and function Richtextbox

    function Add-RichTextBox{
        [CmdletBinding()]
        param ($text)
        #$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
        $richtextbox_output.Text += "$text"
        $richtextbox_output.Text += "`n# # # # # # # # # #`n"    
    }

    Tuesday, November 19, 2019 5:57 PM
  • Thanks for reply , hello world is ok , but my $text not show( user folder size) , i will see each user folder size in richtextbox or in winform or popup window
    Tuesday, November 19, 2019 6:00 PM
  • Please only post code using the tool provided. Read the following link and edit your post. It is not eradable and cannot be copied from the web page without becoming corrupted. That is why we use the tool:

    Also now that you cannot use that control or any control outside of a form.  The post does not help to explain what your issue is.


    \_(ツ)_/


    • Edited by jrv Tuesday, November 19, 2019 6:02 PM
    Tuesday, November 19, 2019 6:01 PM
  • Thanks i am Newbie,here is my code how can i see result in Richtexbox,
    param ($Path = "c:\Users"
    		#[Parameter(Position = 0, ValueFromPipeline = $True, Mandatory = $True)]
    		#[ValidateNotNullOrEmpty()]
    		#[System.String]$Path
    	)
    		
    	function Get-FolderSize ($_ = (get-item .))
    	{
    		Process
    		{
    			$ErrorActionPreference = "SilentlyContinue"
    			$length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
    			$obj = New-Object PSObject
    			$obj | Add-Member NoteProperty Folder ($_.FullName)
    			$obj | Add-Member NoteProperty Length ($length)
    			Write-Output $obj
    		}
    	}
    	
    	
    	Function Class-Size($size)
    	{
    		
    		IF ($size -ge 1GB)
    		{
    			"{0:n2}" -f ($size / 1GB) + " GigaBytes"
    		}
    		ELSEIF ($size -ge 1MB)
    		{
    			"{0:n2}" -f ($size / 1MB) + " MegaBytes"
    		}
    		ELSE
    		{
    			"{0:n2}" -f ($size / 1KB) + " KiloBytes"
    		}
    	}
    	
    	$text = Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |  Format-Table -Property Folder, @{ Label = "Size of Folder"; Expression = { Class-Size($_.Length) } }
    	$richtextbox.AppendText($text)
    here is Function Richtexbox

    function Add-RichTextBox{
    	[CmdletBinding()]
    	param ($text)
    	#$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
    	$richtextbox_output.Text += "$text"
    	$richtextbox_output.Text += "`n# # # # # # # # # #`n"	
    }

    • Edited by Vader100 Tuesday, November 19, 2019 7:24 PM
    Tuesday, November 19, 2019 7:23 PM
  • There is still no idea of what you are doing. You posted a function that is never called and there is no form. The code, as posted, does nothing and throws numerous errors when run.  I recommend learning basic PowerShell before trying to copy code and change it.  You will also not be able to work with forms without basic PowerShell experience.


    \_(ツ)_/

    Tuesday, November 19, 2019 7:33 PM
  • sorry, this is real code with call function Add-richtexbox
    param ($Path = "c:\Users"
    		#[Parameter(Position = 0, ValueFromPipeline = $True, Mandatory = $True)]
    		#[ValidateNotNullOrEmpty()]
    		#[System.String]$Path
    	)
    		
    	function Get-FolderSize ($_ = (get-item .))
    	{
    		Process
    		{
    			$ErrorActionPreference = "SilentlyContinue"
    			$length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
    			$obj = New-Object PSObject
    			$obj | Add-Member NoteProperty Folder ($_.FullName)
    			$obj | Add-Member NoteProperty Length ($length)
    			Write-Output $obj
    		}
    	}
    	
    	
    	Function Class-Size($size)
    	{
    		
    		IF ($size -ge 1GB)
    		{
    			"{0:n2}" -f ($size / 1GB) + " GigaBytes"
    		}
    		ELSEIF ($size -ge 1MB)
    		{
    			"{0:n2}" -f ($size / 1MB) + " MegaBytes"
    		}
    		ELSE
    		{
    			"{0:n2}" -f ($size / 1KB) + " KiloBytes"
    		}
    	}
    	
    	$text = Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |  Format-Table -Property Folder, @{ Label = "Size of Folder"; Expression = { Class-Size($_.Length) } }
    	Add-RichTextBox $text

    Tuesday, November 19, 2019 8:32 PM
  • Sorry but the code can't be executed. It does not make much sense and has no form. A RichTextBox can only be used in a form.


    \_(ツ)_/

    Tuesday, November 19, 2019 8:39 PM