【问题标题】:powershell windows form textboxpowershell windows窗体文本框
【发布时间】:2018-03-01 06:21:30
【问题描述】:

我使用 Windows 窗体创建了一个 PowerShell GUI。 当我为文本框创建 PasswordChar 时,我正在解决一个问题,这仅在用户单击文本框时才有效。 如果用户使用 TAB 导航到文本框,PasswordChar 将不起作用。 我尝试了几件事,但没有成功。 这是我最近的尝试:

$textBox5 = New-Object Windows.Forms.MaskedTextBox

$textBox5.Text = "Enter your Current password:"

    $textBox5.Text = $null $textBox5.PasswordChar = '*' })

$textBox5.Add_TabIndexChanged({

    $textBox5.Text = "haha" })

$textBox5.Width = 203 
$textBox5.Height = 20 
$textBox5.location = new-object system.drawing.point(136,94) 
$textBox5.Font = "Lucida Bright,10" 
$Form.controls.Add($textBox5)

【问题讨论】:

  • 检查您的代码/格式。这些似乎不起作用,甚至可能与它不起作用的原因有关。
  • @marsze 那么,如果我使用了错误的子程序,那么正确的用法是什么?
  • 我刚刚注意到奇怪的注释弄乱了你的代码和一行中的多个语句等......首先清理你的代码。
  • 不清楚您想要的行为是什么以及您的问题是什么。
  • 请包含一个完整的可执行示例

标签: c# winforms powershell


【解决方案1】:

据我从 cmets 了解到,您正在寻找这样的东西。

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

$textbox1 = New-Object System.Windows.Forms.TextBox
$textbox1.Text = "Text Box 1"

$defaulttext = "Enter Password"
$textbox2 = New-Object System.Windows.Forms.MaskedTextBox
$textbox2.Location = "0,40"
$textbox2.Text = $defaulttext
$textbox2.Add_Gotfocus(
   {
       if($textbox2.Text -eq $defaulttext){
            $textbox2.Text = ""
            $textbox2.PasswordChar = "*"
        }

    }
)


$form.controls.Add($textbox1)
$form.controls.Add($textbox2)


$form.ShowDialog()

此代码创建 2 个文本框。 Textbox2 最初有文本“输入密码”。一旦 textbox2 获得焦点,初始字符串就会被删除,并且在文本框中输入的字符会被 '*' 掩盖。

initial form

Form once textbox2 gets focus

【讨论】:

    【解决方案2】:

    最好指定最好在创建新对象Form之前加载程序集,以免产生Type not found错误

    Add-Type -AssemblyName System.Windows.Forms
    

    或者

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2018-10-23
      • 2012-01-19
      相关资源
      最近更新 更多