我只想在textbox.text变量内添加值,而不希望对该问题使用另一个变量:
$temp = '$textbox'
$textbox.text = "this will store this string exactly as I want"
# I want to use temp for that issue but this code of course won't work:
$temp.text = "this will not work"
如何使用此$temp
变量调用$textbox
变量?
编辑:
我有一个GUI,带有8个文本框,每行2个。 左行命名为textbox2 textbox4 textbox6,依此类推 右行命名为textbox3 textbox5 textbox7,依此类推。 这个想法是这样的: 我有一个包含8个项目的数组。 如果我在左侧的文本框中有文本,则将数组项放在右侧的相应文本框中。例如:
if (textbox2.textlength -gt 0) #meaning its not empty textbox
{textbox3.text = $array[0]} #corresponding textbox to the right will get value from array
所以我必须检查textbox2、4、6、8等,每次我看到一个值时,我都需要获取下一个数组项并将其放在正确的文本框中 我要添加图片:
我希望清楚。 我想为此问题创建一个循环,但是我不能做类似的事情:for ($i=2; $i -lt 17; $i=$i+2){
if (textbox$i.textlength -gt 0 ) #of cours this wont work..
更多编辑: 这是我想放入循环中的代码:
Function FillBoxes ($passarray) {
if ($textbox2.textlength -gt 0) {
$textbox3.text = $passarray[0]}
if ($textbox4.textlength -gt 0) {
$textbox5.text = $passarray[1]}
if ($textbox6.textlength -gt 0) {
$textbox7.text = $passarray[2]}
if ($textbox8.textlength -gt 0) {
$textbox9.text = $passarray[3]}
if ($textbox10.textlength -gt 0) {
$textbox11.text = $passarray[4]}
if ($textbox12.textlength -gt 0) {
$textbox13.text = $passarray[5]}
if ($textbox14.textlength -gt 0) {
$textbox15.text = $passarray[6]}
if ($textbox16.textlength -gt 0) {
$textbox17.text = $passarray[7]}
}
答案 0 :(得分:1)
就像Lee_Dailey所说的那样,我真的被您在这里所说的话吓了一跳。
如果不进行逻辑检查,您是否需要进行循环工作?
[array]$passarray = 1..17
for ($i=2; $i -lt 17; $i=$i+2){
switch ($i)
{
2
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[0])"
}
4
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[1])"
}
6
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[2])"
}
8
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[3])"
}
10
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[4])"
}
12
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[5])"
}
14
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[6])"
}
16
{
"Textbox_text$i"
"Value to assign to text box is: $($passarray[7])"
}
default {$PSItem | Out-Null}
}
}
# Results
Textbox_text2
Value to assign to text box is: 1
Textbox_text4
Value to assign to text box is: 2
Textbox_text6
Value to assign to text box is: 3
Textbox_text8
Value to assign to text box is: 4
Textbox_text10
Value to assign to text box is: 5
Textbox_text12
Value to assign to text box is: 6
Textbox_text14
Value to assign to text box is: 7
Textbox_text16
Value to assign to text box is: 8
当然,您可以轻松地在交换机中将该逻辑扩展到您选择的任何内容。
答案 1 :(得分:1)
在创建TextBoxes时,也许可以通过使用.Name
属性来简化操作:
for ($i = 2; $i -lt 17; $i++) {
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Name = "TextBox$i"
# add the rest of the textbox properties and add to the forms Controls array
$form.Controls.Add($textBox)
}
然后,在您的FillBoxes
函数中,可以按框的名称选择框
for ($i = 2; $i -lt 17; $i += 2) {
$tb = $form.Controls.Item("TextBox$i")
if (!([string]::IsNullOrWhiteSpace($tb.Text))) {
$form.Controls.Item("TextBox$($i + 1)").Text = $passarray[$i - 2]
}
}
希望有帮助