Change windows form after windows draw.

rated by 0 users
This post has 1 Reply | 1 Follower

Top 500 Contributor
Posts 6
Randomize Posted: 12-13-2011 11:14 PM

Hello!

I have a script witch draw a window with richtextbox. The problem is in the script. After i start command $formMCIScript.ShowDialog() i can't change text in the richtextbox. I need to change it after different commands to show status. Does anyone know how to make this changeble?

 

#region Application Functions

function OnApplicationLoad {

return $true #return true for success or false for failure

}

function OnApplicationExit {

$script:ExitCode = 0 #Set the exit code for the Packager

}

#endregion Application Functions

function Call-MCI {

 

#region Import the Assemblies

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

[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

[void][reflection.assembly]::Load("System, 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.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

#endregion Import Assemblies

#region Generated Form Objects

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

[System.Windows.Forms.Application]::EnableVisualStyles()

$formMCIScript = New-Object System.Windows.Forms.Form

$richtextbox1 = New-Object System.Windows.Forms.RichTextBox

$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

#endregion Generated Form Objects

 

$formMCIScript_Load={}

#TODO: FOR Example!

$test = Get-ChildItem "c:\*"

$richtextbox1.AppendText($($test|Out-String))

$test1 = Get-ChildItem "D:\*"

$richtextbox1.AppendText($($test1|Out-String))

# --End Script--

#region Generated Events

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

$Form_StateCorrection_Load=

{

#Correct the initial state of the form to prevent the .Net maximized form issue

$formMCIScript.WindowState = $InitialFormWindowState

}

$Form_Cleanup_FormClosed=

{

#Remove all event handlers from the controls

try

{

$formMCIScript.remove_Load($formMCIScript_Load)

$formMCIScript.remove_Load($Form_StateCorrection_Load)

$formMCIScript.remove_FormClosed($Form_Cleanup_FormClosed)

}

catch [Exception]

{ }

}

#endregion Generated Events

#region Generated Form Code

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

# formMCIScript

#

$formMCIScript.Controls.Add($richtextbox1)

$formMCIScript.ClientSize = New-Object System.Drawing.Size(733,362)

$formMCIScript.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 

$formMCIScript.Name = "formMCIScript"

$formMCIScript.Text = "MCI Script"

$formMCIScript.add_Load($formMCIScript_Load)

#

# richtextbox1

#

$richtextbox1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 

$richtextbox1.Location = New-Object System.Drawing.Point(-1,1)

$richtextbox1.Name = "richtextbox1"

$richtextbox1.Size = New-Object System.Drawing.Size(731,359)

$richtextbox1.TabIndex = 0

$richtextbox1.Text = ""

$richTextBox1.font = "Courier New"

#endregion Generated Form Code

 

#Save the initial state of the form

$InitialFormWindowState = $formMCIScript.WindowState

#Init the OnLoad event to correct the initial state of the form

$formMCIScript.add_Load($Form_StateCorrection_Load)

#Clean up the control events

$formMCIScript.add_FormClosed($Form_Cleanup_FormClosed)

#Show the Form

return $formMCIScript.ShowDialog()

 

} #End Function

#Call OnApplicationLoad to initialize

if((OnApplicationLoad) -eq $true)

{

#Create the form

Call-MCI | Out-Null

#Perform cleanup

OnApplicationExit

}

 

Top 10 Contributor
Posts 597
Microsoft MVP
Top Contributor

your particular code sample is too complex to be answered here. We can only comment on particular code snippets.

BUT when you call ShowDialog(), you basically hand over control to the form. Whatever you want to change must now occur from "inside" the form, either event driven (i.e. timer), or from logic inside your form. In addition, to actually display UI changes, you may also have to refresh the UI elements to give them a chance to redraw.

It may be much easier for you to use WPF, depending on the .NET framework version your systems support.

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.