如何使用菜单条控制tabpages?

时间:2013-03-24 18:29:20

标签: c# .net powershell powershell-v2.0 powershell-v3.0

我创建了一个powershell脚本,我正在尝试使主界面使用选项卡式布局。

我得到了我想要的所有功能,除了我想隐藏顶部的标签并使用菜单条上的按钮切换标签。

我已经成功创建了菜单条并将标签隐藏在顶部,但我无法弄清楚如何让菜单上的按钮选择不同的标签。

任何人都有办法怎么做?

示例:

在我的菜单条上:Control1 Control2 我的标签控件上的我的标签页:Tabpage1 Tabpage2

如何使用Control1显示Tabpage1 Tabpage1,使用Control2显示Tabpage2?

以下代码:

#----------------------------------------------
#region Application Functions
#----------------------------------------------

function OnApplicationLoad {
#Note: This function is not called in Projects
#Note: This function runs before the form is created
#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
#Important: Form controls cannot be accessed in this function
#TODO: Add snapins and custom code to validate the application load

return $true #return true for success or false for failure
}

function OnApplicationExit {
#Note: This function is not called in Projects
#Note: This function runs after the form is closed
#TODO: Add custom code to clean up and unload snapins when the application exits

$script:ExitCode = 0 #Set the exit code for the Packager
}

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-example_pff {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$tabcontrol1 = New-Object 'System.Windows.Forms.TabControl'
$tabpage1 = New-Object 'System.Windows.Forms.TabPage'
$tabpage2 = New-Object 'System.Windows.Forms.TabPage'
$menustrip1 = New-Object 'System.Windows.Forms.MenuStrip'
$control1ToolStripMenuItem = New-Object 'System.Windows.Forms.ToolStripMenuItem'
$control2ToolStripMenuItem = New-Object 'System.Windows.Forms.ToolStripMenuItem'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------
$form1_Load={
    #TODO: Initialize Form Controls here

}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
    #Remove all event handlers from the controls
    try
    {
        $form1.remove_Load($form1_Load)
        $form1.remove_Load($Form_StateCorrection_Load)
        $form1.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch [Exception]
    { }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
#
# form1
#
$form1.Controls.Add($tabcontrol1)
$form1.Controls.Add($menustrip1)
$form1.ClientSize = '842, 355'
$form1.MainMenuStrip = $menustrip1
$form1.Name = "form1"
$form1.Text = "Form"
$form1.add_Load($form1_Load)
#
# tabcontrol1
#
$tabcontrol1.Controls.Add($tabpage1)
$tabcontrol1.Controls.Add($tabpage2)
$tabcontrol1.Location = '9, 71'
$tabcontrol1.Name = "tabcontrol1"
$tabcontrol1.SelectedIndex = 0
$tabcontrol1.Size = '811, 256'
$tabcontrol1.TabIndex = 1
#
# tabpage1
#
$tabpage1.Location = '4, 22'
$tabpage1.Name = "tabpage1"
$tabpage1.Padding = '3, 3, 3, 3'
$tabpage1.Size = '803, 230'
$tabpage1.TabIndex = 0
$tabpage1.Text = "tabpage1"
$tabpage1.UseVisualStyleBackColor = $True
#
# tabpage2
#
$tabpage2.Location = '4, 22'
$tabpage2.Name = "tabpage2"
$tabpage2.Padding = '3, 3, 3, 3'
$tabpage2.Size = '803, 230'
$tabpage2.TabIndex = 1
$tabpage2.Text = "tabpage2"
$tabpage2.UseVisualStyleBackColor = $True
#
# menustrip1
#
$menustrip1.BackColor = 'Red'
[void]$menustrip1.Items.Add($control1ToolStripMenuItem)
[void]$menustrip1.Items.Add($control2ToolStripMenuItem)
$menustrip1.Location = '0, 0'
$menustrip1.Name = "menustrip1"
$menustrip1.Size = '842, 24'
$menustrip1.TabIndex = 0
$menustrip1.Text = "menustrip1"
#
# control1ToolStripMenuItem
#
$control1ToolStripMenuItem.Name = "control1ToolStripMenuItem"
$control1ToolStripMenuItem.Size = '65, 20'
$control1ToolStripMenuItem.Text = "Control1"
#
# control2ToolStripMenuItem
#
$control2ToolStripMenuItem.Name = "control2ToolStripMenuItem"
$control2ToolStripMenuItem.Size = '65, 20'
$control2ToolStripMenuItem.Text = "Control2"
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()

} #End Function

#Call OnApplicationLoad to initialize
if((OnApplicationLoad) -eq $true)
{
#Call the form
Call-example_pff | Out-Null
#Perform cleanup
OnApplicationExit
}

1 个答案:

答案 0 :(得分:0)

您需要事件处理程序。您可以通过单击GUI元素并双击要处理的事件处理程序字段,在PrimalForms中为这些生成存根。要查看事件处理程序,请单击闪电图标。

这样做会关联一个脚本块来处理控件的事件。然后添加代码来处理事件。根据您的需求,我认为您正在寻找:

$tabcontrol1.SelectTab(0)

PrimalForm将附加如下所示的脚本块:

$control1ToolStripMenuItem.add_Click($control1ToolStripMenuItem_OnClick)
$control2ToolStripMenuItem.add_Click($control2ToolStripMenuItem_OnClick)

然后在Generated Form Code部分找到并修改它们,如下所示:

$control1ToolStripMenuItem_OnClick=
{
    $tabcontrol1.SelectTab(0)  
}

$control2ToolStripMenuItem_OnClick=
{
    $tabcontrol1.SelectTab(1)  
}