【问题标题】:Exchange PowerShell dropdown issueExchange PowerShell 下拉问题
【发布时间】:2015-07-04 05:47:21
【问题描述】:

我正在尝试在我的表单中创建一个下拉框,该下拉框设置有两个文本字段。但是,当我尝试将下拉列表添加到表单时,它会更改最终值,即使我没有更改类型或答案,它也会更改它返回的值。我写的完美运行的原始代码是:

function button ($title,$mailbx, $WF, $TF) {

###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

#####Define the form size & placement

$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 750;
$form.Height = 500;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $mailbx;

##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $WF;

##############Define text label3

$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;

$textLabel3.Text = $TF;

############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;

############Define text box2 for input

$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;

############Define text box3 for input

$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;

#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;

#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;

############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};

$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();

#################return values

return $textBox1.Text, $textBox2.Text, $textBox3.Text
}

$return= button “Enter Info” “First Name” “Last Name” “Email Address”
$return2 = ($return[0] + " " + $return[1])
$return3 = ($return[0] + "." + $return[1])
$return4 = $return[0] + "." + $return[1] + "$return[2]"


New-Mailbox -Alias $return3 -Name $return2 -FirstName $return[0] -LastName $return[1] -UserPrincipalName $return4  -Password (ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
Set-User -Identity $return3 -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'

我的下拉代码是

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

# Edit This item to change the DropDown Values

[array]$DropDownArray = "@yahoo.com", "@gmail.com", "@lewisJ.com"

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {

 $Choice = $DropDown.SelectedItem.ToString()
 }

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




$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(400,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
 $DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
$DropDownLabel.size = new-object System.Drawing.Size(100,20) 
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "OK"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

我想拥有它,所以如果我输入名字为 Ben,姓氏为 Don,并使用下拉功能并选择 @gmail.com。它将返回这些值。当我尝试合并这两个代码时,它会将所有值更改为:

System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d5 0a3a0

有人知道如何正确设置此下拉菜单吗?

【问题讨论】:

  • 欢迎泰勒回来。所以返回值应该是下拉框中的“@gmail.com”而不是别的?
  • 嗨,马特!很高兴再次见到你。应该有3个值。 $return[0] 应该是名字。 $return[1] 应该是姓氏。我试图让第三个值成为 3 个下拉选项之一(@gmail.com、@yahoo.com、@msn.com)。但是,当我们输入下拉框的代码时,它会返回上面列出的值。
  • 对于初学者,您需要在下拉代码中以[System.Reflection.Assembly] 开头的行再次使用[void]。除非那是复制粘贴错误
  • 你说得对,我不知道我为什么忽略了这一点。

标签: powershell exchange-server


【解决方案1】:

您似乎再次需要取消代码中方法的某些返回值。如果您查看TechNet 的列表框(是的,我知道我们有一个下拉菜单),您会看到除了无效

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

您还必须处理Add 方法。

[void]$DropDown.Items.Add($Item) 

这应该确保您的回报是您想要的。您可能仍然对下拉列表中的值有疑问,但这将使您朝着正确的方向前进。

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多