【问题标题】:Using another variable to store context inside a different variable使用另一个变量将上下文存储在另一个变量中
【发布时间】:2020-05-31 14:07:54
【问题描述】:

我只想在 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 变量?

编辑:

我有一个带有 8 个文本框的 gui,每行 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]}
     }

【问题讨论】:

  • $textbox 是具有 text 属性的类吗?
  • 我承认我无法弄清楚你在说什么。也许如果你展示你想要的东西,它会更容易理解......
  • 您是否亲自定义了.text 类?很抱歉我看不懂你的代码。
  • 你是想说你想创建一个文本框数组,然后如果任何文本框不为空,那么做一些事情
  • 代码应该检查 service1 文本框,如果它不为空,它应该转到右边的文本框并将信息放在那里。它应该遍历所有文本框,代码应该放入其中的信息存储在一个数组中。这意味着 $array[0] 将转到第一个文本框(右上角) $array[1] 将转到第二个文本框(右侧第二个),依此类推

标签: windows powershell


【解决方案1】:

也许您可以在创建文本框时使用.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]
    }
}

【讨论】:

    【解决方案2】:

    就像 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
    

    当然,您可以轻松地将开关中的逻辑扩展到您选择的任何内容。

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2018-03-09
      • 1970-01-01
      相关资源
      最近更新 更多