我希望在用户点击它时更改richTextBox的背景颜色。我想我需要一个点击事件。颜色应该是特定的,因此它们不会使文本难以阅读。我想我需要一个列表或数组来选择颜色。我不认为我需要一个随机函数,只是一种改变richTextBox颜色的方法。任何想法?
由于
#Generated Form Function
function GenerateForm {
#######################################################################
# Generated On: 8/30/2016 4:54 PM
# Generated By: christnerw
########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$richTextBox4 = New-Object System.Windows.Forms.RichTextBox
$richTextBox3 = New-Object System.Windows.Forms.RichTextBox
$richTextBox2 = New-Object System.Windows.Forms.RichTextBox
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 262
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"
$richTextBox4.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 150
$richTextBox4.Location = $System_Drawing_Point
$richTextBox4.Name = "richTextBox4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 100
$System_Drawing_Size.Width = 100
$richTextBox4.Size = $System_Drawing_Size
$richTextBox4.TabIndex = 3
$richTextBox4.Text = ""
$form1.Controls.Add($richTextBox4)
$richTextBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 32
$System_Drawing_Point.Y = 150
$richTextBox3.Location = $System_Drawing_Point
$richTextBox3.Name = "richTextBox3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 100
$System_Drawing_Size.Width = 100
$richTextBox3.Size = $System_Drawing_Size
$richTextBox3.TabIndex = 2
$richTextBox3.Text = ""
$form1.Controls.Add($richTextBox3)
$richTextBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 32
$System_Drawing_Point.Y = 37
$richTextBox2.Location = $System_Drawing_Point
$richTextBox2.Name = "richTextBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 98
$System_Drawing_Size.Width = 100
$richTextBox2.Size = $System_Drawing_Size
$richTextBox2.TabIndex = 1
$richTextBox2.Text = ""
$form1.Controls.Add($richTextBox2)
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 37
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Name = "richTextBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 98
$System_Drawing_Size.Width = 100
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 0
$richTextBox1.Text = ""
$form1.Controls.Add($richTextBox1)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
答案 0 :(得分:0)
我使用Hashtable存储颜色对(后退/前移),然后在GotFocus和LostFocus事件上执行颜色更改。
$Colors = @{
'Black'='White'
'White'='Black'
}
Function ColorSwap = {
Param([string]$Ctrl)
Invoke-Expression ('${0}.BackColor = $Colors.(${0}.BackColor)' -f $Ctrl)
Invoke-Expression ('${0}.ForeColor = $Colors.(${0}.ForeColor)' -f $Ctrl)
}
$richTextBox1.add_GotFocus({ColorSwap richTextBox1})
$richTextBox1.add_LostFocus({ColorSwap richTextBox1})
$richTextBox2.add_GotFocus({ColorSwap richTextBox2})
$richTextBox2.add_LostFocus({ColorSwap richTextBox2})
$richTextBox3.add_GotFocus({ColorSwap richTextBox3})
$richTextBox3.add_LostFocus({ColorSwap richTextBox3})
$richTextBox4.add_GotFocus({ColorSwap richTextBox4})
$richTextBox4.add_LostFocus({ColorSwap richTextBox4})
应该这样做,当你在框中点击它时,它将背景设置为黑色,字体设置为白色,并在单击框外单击时将其交换回来。如果需要,您可以将颜色对添加到哈希表中。