winforms 如何删除TextBox中的多余字符?

xtupzzrd  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(177)

此表单的目的是首先检索用户名,然后使用该用户名检索电子邮件地址。它会获取地址,但也会检索其他字符。textbox 3最终阅读@{EmailAddress=first. domain.com}。我尝试过修剪字符,但没有效果。

Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
    
    [Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
    <# 
    .NAME
        Template
    #>
    
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = New-Object System.Drawing.Point(400,400)
    $Form.text                       = "Form"
    $Form.TopMost                    = $false
    
    $TextBox1                        = New-Object system.Windows.Forms.TextBox
    $TextBox1.multiline              = $false
    $TextBox1.width                  = 100
    $TextBox1.height                 = 20
    $TextBox1.location               = New-Object System.Drawing.Point(61,52)
    $TextBox1.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
    
    $TextBox2                        = New-Object system.Windows.Forms.TextBox
    $TextBox2.multiline              = $false
    $TextBox2.width                  = 100
    $TextBox2.height                 = 20
    $TextBox2.location               = New-Object System.Drawing.Point(219,52)
    $TextBox2.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
    
    $TextBox3                        = New-Object system.Windows.Forms.TextBox
    $TextBox3.multiline              = $false
    $TextBox3.width                  = 100
    $TextBox3.height                 = 20
    $TextBox3.location               = New-Object System.Drawing.Point(61,130)
    $TextBox3.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
    
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.text                    = "button"
    $Button1.width                   = 60
    $Button1.height                  = 30
    $Button1.location                = New-Object System.Drawing.Point(228,127)
    $Button1.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
    $Button1.Add_Click({getacctname})
    $Form.controls.AddRange(@($TextBox1,$TextBox2,$TextBox3,$Button1))
    
    
    #region Logic 
    function getacctname {
        $fname = $TextBox1.Text
        $lname = $TextBox2.Text
    
         $User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" |
            Select-Object -ExpandProperty 'SamAccountName' |
               Out-Gridview -Title 'Windows Logon' -PassThru
               $TextBox3.Text = Get-ADUser -identity $User.text -Properties * | select EmailAddress
               
    }
    #endregion
    
    [void]$Form.ShowDialog()
3wabscal

3wabscal1#

您的程式码有两个问题:
1.正如注解中所解释的,您需要从对象的EmailAddress属性中获取,否则,由于.Text属性值只能是 string,因此您将看到的结果是PSCustomObject的字符串表示。
1.对$User.Text的赋值无效,并且会产生错误,除非您没有向我们显示您的实际代码。$User变量未定义,因此不能具有.Text属性。尝试对其赋值将产生如下错误:
在此对象上找不到属性“Text”。请验证该属性是否存在并且可以设置。
您的职能部门应如何解决这两个问题:

function getacctname {
    $fname, $lname = $TextBox1.Text.Trim(), $TextBox2.Text.Trim()
    $TextBox3.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties mail |
        Select-Object SamAccountName, mail |
        Out-Gridview -Title 'Windows Logon' -PassThru |
        Select-Object -ExpandProperty mail
}

另外,我建议您将$TextBox3设置为ReadOnly TextBox:

$TextBox3.ReadOnly = $true

相关问题