Powershell:将数组项输出到Rich文本框

时间:2016-08-30 13:19:26

标签: powershell

我正在尝试编写一个针对IT的卡片版本并提出问题。

我有一个数组,我正在使用JFrame frame = new JFrame("Grid Hopper"); frame.setSize(1120,800); frame.setLayout(new FlowLayout()); JPanel pane = new JPanel(); pane.add(game); pane.add(score); frame.add(pane); 。我的第一个想法是在一个脚本上都有白色和沙皇卡片。但是我需要使用items = list of cards然后在items函数中使用for-each项。如果我在列表中有两种类型的卡片,我会认为该脚本不适用于2" list"的cmdlet。我可以将这些重命名为" card"和" czar"

这是我相信阵列中白卡的一个例子。

当我运行这个脚本而不是列表中只有一个项目返回时,所有这些都可以。我怎么能解决这个问题?任何方向总是受到赞赏。

Get-Random

1 个答案:

答案 0 :(得分:0)

好吧,必须做出一些改变。

在脚本顶部添加导致错误的类型时,Out-null之间有空格,因此请删除空格。

第二次更改,您不能使用名称$czar来引用您为沙皇卡点击的按钮,以及沙皇卡的可能值列表,因此我重命名了卡到$czarValues

接下来,你需要在脚本的顶部而不是在最后定义卡片,否则你第一次加载脚本时会发生错误,这是我发生的事情,所以我感动了卡片定义从头到尾。

最后,由于我们将卡片阵列的名称更改为$czarValues,您还需要更改Add_Click处理程序的值,因此请更改此内容:

$handler_Czar_Click= {
$richTextBox1.Text =  $czar | Get-Random | Out-String
}

到此:

$handler_Czar_Click= {
$richTextBox1.Text =  $czarValues | Get-Random | Out-String
}

还有一件事,就是如何填充白卡中的值。

$card_OnClick= {
$richTextBox2,$richTextBox3,$richTextBox4,$richTextBox5,$richTextBox6,$richTextBox7,$richTextBox8,$richTextBox9 | % {$_.Text = $white | Get-Random }
}

通过所有这些变化,游戏现在大部分都有效,如下所示:

enter image description here

完成的代码包含我所做的更改。

    #
# Cards
########################################################################################
  $white = 
  "Rotating the Backup Tapes" ,
  "a PEBCAK" ,
  "rm -rf /*.*" ,
  "a faulty crossover cable" ,
  "a KVM with a broken M" ,
  "a one button mouse" ,
  "a laser pointer" ,
  "scanning a little too much with nmap" ,
  "a coffee stained mousepad" ,
  "a webcam pointed at the coffee maker" ,
  "a bastion host" ,
  "a salvaged motherboard with a parallel port" ,
  "a razorsharp punchdown tool" ,
  "my rooted phone" ,
  "a mouse connected serial -> ps/2 -> usb" ,
  "0118 999 881 999 119 725...3" ,
  "my password" ,
  "a fingerprint scanner" ,
  "a scanning electron microscope" ,
  "a helpdesk ticket to 'fix the cpu'" ,
  "a hard drive degaussing machine" ,
  "a multitool with a bottle opener" ,
  "a larger database connection pool" ,
  "a rainbow pack of 3.5 floppy disks" ,
  "a usb stick found in the parking lot" ,
  "an Arduino runnning NetBSD" ,
  "a shell script written in BASIC" ,
  "Edward Snowden" ,
  "CryptoLocker" ,
  "a serial port mouse" ,
  "5000 free AOL CDs" ,
  "Angelina Jolie in Hackers" ,
  "an RJ-45 crimping tool" ,
  "Clippy" ,
  "Two Factor Auth" ,
  "Have you tried turning it off and on again" ,
  "a flaky VPN connection" ,
  "script kiddies" ,
  "an IP Webcam with a default password" ,
  "rebuilding a RAID array" ,
  "manually fixing 10/100/1000 'auto' negotiation" ,
  "viewing web filter block logs" ,
  "the server is down" ,
  "pinging 127.0.0.1" ,
  "Using hillary clinton's private email server"

  ##################################################################################################

  $czarValues = 
  "The new IT Director has a sick tattoo of ____________",
  "The last consultant was fired over ____________",
  "In a pinch, ____________ can be used to remote reboot a server",
  "____________ is Layer 8 of the OSI networking model",
  "Forgetting ____________ in the datacenter is a firing offense",
  "We're going to use ____________ instead of updating to IPv6",
  "____________ is the last stage of our ____________ rollout (Pick 2)",
  "That red plunger switch on the wall launches ____________",
  "I got this coffee mug from a conference about ____________ back in 1997",
  "Had a user fall prey to a sophisticated ____________ phishing attack",
  "Two factor auth with ____________ and ____________ (Pick 2)",
  "Forgot my badge this morning and had to use ____________ to get in",
  "____________ is now included for free with every OS Update",
  "____________ is now required every time you restart ____________ (Pick 2)",
  "The hot new videogame this year is ____________ Simulator!",
  "A helpdesk call to fix ____________ with ____________ (Pick 2)",
  "I just got a new certification in ____________"


#Generated Form Function
function GenerateForm {
########################################################################
# Generated On: 8/29/2016 1:53 PM
# Generated By: christnerw
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$richTextBox9 = New-Object System.Windows.Forms.RichTextBox
$card = New-Object System.Windows.Forms.Button
$richTextBox8 = New-Object System.Windows.Forms.RichTextBox
$richTextBox7 = New-Object System.Windows.Forms.RichTextBox
$richTextBox6 = New-Object System.Windows.Forms.RichTextBox
$richTextBox5 = New-Object System.Windows.Forms.RichTextBox
$richTextBox4 = New-Object System.Windows.Forms.RichTextBox
$richTextBox3 = New-Object System.Windows.Forms.RichTextBox
$Czar = New-Object System.Windows.Forms.Button
$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

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_richTextBox7_TextChanged= 
{
#TODO: Place custom script here

}

$handler_form1_Load= 
{
#TODO: Place custom script here

}



$card_OnClick= {
$richTextBox2,$richTextBox3,$richTextBox4,$richTextBox5,$richTextBox6,$richTextBox7,$richTextBox8,$richTextBox9 | % {$_.Text = $white | Get-Random }
}


$handler_richTextBox5_TextChanged= 
{
#TODO: Place custom script here

}
$handler_Czar_Click= {
$richTextBox1.Text =  $czarValues | Get-Random | Out-String
}

{


}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form   issue
$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,139,0,0)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 808
$System_Drawing_Size.Width = 1306
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Font = New-Object System.Drawing.Font("Microsoft Sans   Serif",12,1,3,1)
#$form1.Icon =         [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Users\christnerw\Desktop\Powers      hell Project\flat_icons-graficheria.ico')
$form1.Name = "form1"
$form1.Text = "Cards Aghinst IT"
$form1.add_Load($handler_form1_Load)

$richTextBox9.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox9.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox9.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox9.Font = New-Object System.Drawing.Font("Microsoft Sans  Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 992
$System_Drawing_Point.Y = 532
$richTextBox9.Location = $System_Drawing_Point
$richTextBox9.Name = "richTextBox9"
$richTextBox9.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox9.Size = $System_Drawing_Size
$richTextBox9.TabIndex = 12
$richTextBox9.Text = ""

$form1.Controls.Add($richTextBox9)


$card.DataBindings.DefaultDataSourceUpdateMode = 0
$card.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 1062
$System_Drawing_Point.Y = 198
$card.Location = $System_Drawing_Point
$card.Name = "card"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 51
$System_Drawing_Size.Width = 189
$card.Size = $System_Drawing_Size
$card.TabIndex = 10
$card.Text = "Click for White Card"
$card.UseVisualStyleBackColor = $True
$card.add_Click($card_OnClick)

$form1.Controls.Add($card)

$richTextBox8.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox8.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox8.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox8.Font = New-Object System.Drawing.Font("Microsoft Sans  Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 686
$System_Drawing_Point.Y = 532
$richTextBox8.Location = $System_Drawing_Point
$richTextBox8.Name = "richTextBox8"
$richTextBox8.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox8.Size = $System_Drawing_Size
$richTextBox8.TabIndex = 8
$richTextBox8.Text = ""

$form1.Controls.Add($richTextBox8)

$richTextBox7.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox7.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox7.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox7.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 365
$System_Drawing_Point.Y = 532
$richTextBox7.Location = $System_Drawing_Point
$richTextBox7.Name = "richTextBox7"
$richTextBox7.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 297
$richTextBox7.Size = $System_Drawing_Size
$richTextBox7.TabIndex = 7
$richTextBox7.Text = ""
$richTextBox7.add_TextChanged($handler_richTextBox7_TextChanged)

$form1.Controls.Add($richTextBox7)

$richTextBox6.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox6.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox6.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox6.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 532
$richTextBox6.Location = $System_Drawing_Point
$richTextBox6.Name = "richTextBox6"
$richTextBox6.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox6.Size = $System_Drawing_Size
$richTextBox6.TabIndex = 6
$richTextBox6.Text = ""

$form1.Controls.Add($richTextBox6)

$richTextBox5.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox5.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox5.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox5.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 274
$richTextBox5.Location = $System_Drawing_Point
$richTextBox5.Name = "richTextBox5"
$richTextBox5.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox5.Size = $System_Drawing_Size
$richTextBox5.TabIndex = 5
$richTextBox5.Text = ""
$richTextBox5.add_TextChanged($handler_richTextBox5_TextChanged)

$form1.Controls.Add($richTextBox5)

$richTextBox4.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox4.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox4.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox4.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 992
$System_Drawing_Point.Y = 274
$richTextBox4.Location = $System_Drawing_Point
$richTextBox4.Name = "richTextBox4"
$richTextBox4.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox4.Size = $System_Drawing_Size
$richTextBox4.TabIndex = 4
$richTextBox4.Text = ""

$form1.Controls.Add($richTextBox4)

$richTextBox3.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 686
$System_Drawing_Point.Y = 274
$richTextBox3.Location = $System_Drawing_Point
$richTextBox3.Name = "richTextBox3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 283
$richTextBox3.Size = $System_Drawing_Size
$richTextBox3.TabIndex = 3
$richTextBox3.Text = ""

$form1.Controls.Add($richTextBox3)


$Czar.DataBindings.DefaultDataSourceUpdateMode = 0
$Czar.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 199
$System_Drawing_Point.Y = 28
$Czar.Location = $System_Drawing_Point
$Czar.Name = "Czar"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 68
$System_Drawing_Size.Width = 135
$Czar.Size = $System_Drawing_Size
$Czar.TabIndex = 2
$Czar.Text = "Click for Czar  Card"
$Czar.UseVisualStyleBackColor = $True
$Czar.add_Click($handler_Czar_Click)

$form1.Controls.Add($Czar)

$richTextBox2.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox2.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 365
$System_Drawing_Point.Y = 274
$richTextBox2.Location = $System_Drawing_Point
$richTextBox2.Name = "richTextBox2"
$richTextBox2.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 223
$System_Drawing_Size.Width = 297
$richTextBox2.Size = $System_Drawing_Size
$richTextBox2.TabIndex = 1
$richTextBox2.Text = ""

$form1.Controls.Add($richTextBox2)

$richTextBox1.BackColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$richTextBox1.Cursor = [System.Windows.Forms.Cursors]::Hand
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)
$richTextBox1.ForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 391
$System_Drawing_Point.Y = 28
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Name = "richTextBox1"
$richTextBox1.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 221
$System_Drawing_Size.Width = 490
$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