キャプション、Click、Select、Popup の各イベントに対して定義されているイベント ハンドラ、メニュー項目のショートカット キー、マージの種類、およびマージ順序を指定して、MenuItem クラスの新しいインスタンスを初期化します。
名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
'宣言
Public Sub New ( _
mergeType As MenuMerge, _
mergeOrder As Integer, _
shortcut As Shortcut, _
text As String, _
onClick As EventHandler, _
onPopup As EventHandler, _
onSelect As EventHandler, _
items As MenuItem() _
)
'使用
Dim mergeType As MenuMerge
Dim mergeOrder As Integer
Dim shortcut As Shortcut
Dim text As String
Dim onClick As EventHandler
Dim onPopup As EventHandler
Dim onSelect As EventHandler
Dim items As MenuItem()
Dim instance As New MenuItem(mergeType, mergeOrder, shortcut, text, onClick, onPopup, onSelect, items)
public MenuItem (
MenuMerge mergeType,
int mergeOrder,
Shortcut shortcut,
string text,
EventHandler onClick,
EventHandler onPopup,
EventHandler onSelect,
MenuItem[] items
)
public:
MenuItem (
MenuMerge mergeType,
int mergeOrder,
Shortcut shortcut,
String^ text,
EventHandler^ onClick,
EventHandler^ onPopup,
EventHandler^ onSelect,
array<MenuItem^>^ items
)
public MenuItem (
MenuMerge mergeType,
int mergeOrder,
Shortcut shortcut,
String text,
EventHandler onClick,
EventHandler onPopup,
EventHandler onSelect,
MenuItem[] items
)
public function MenuItem (
mergeType : MenuMerge,
mergeOrder : int,
shortcut : Shortcut,
text : String,
onClick : EventHandler,
onPopup : EventHandler,
onSelect : EventHandler,
items : MenuItem[]
)
パラメータ
- mergeType
MenuMerge 値の 1 つ。
- mergeOrder
メニュー項目がマージされた場合に、このメニュー項目がマージ後のメニューで占める位置を相対的に示す値。
- shortcut
Shortcut 値の 1 つ。
- text
メニュー項目のキャプション。
- onClick
このメニュー項目の Click イベントを処理する EventHandler。
- onPopup
このメニュー項目の Popup イベントを処理する EventHandler。
- onSelect
このメニュー項目の Select イベントを処理する EventHandler。
- items
このメニュー項目のサブメニュー項目が格納されている MenuItem オブジェクトの配列。
解説
text パラメータを使用してメニュー項目のキャプションを指定するときに、アクセス キーも指定できます。指定する場合は、キャプションの末尾にアンパサンド (&) を追加し、その後にアクセス キーとして使用する英字を大文字で入力し、アンパサンドを含めて前後を () で囲みます。たとえば、"ファイル" メニューのアクセス キーとして "F" を指定するには、メニュー項目のキャプションを "ファイル(&F)" と指定します。この機能を使用すると、キーボードを使用してメニュー内を移動できます。
text パラメータを "-
" に設定すると、メニュー項目が標準のメニュー項目ではなく、区分線 (水平線) として表示されます。
items パラメータを使用すると、メニュー項目の配列を割り当てて、メニュー項目のサブメニューを定義できます。配列内の各項目にも、メニュー項目の配列を割り当てることができます。これにより、完成度の高いメニュー構造を作成し、メニュー項目のコンストラクタに割り当てることができます。
mergeType パラメータと mergeOrder パラメータを使用すると、メニュー項目が別のメニューにマージされた場合に、そのメニュー項目がどのように動作するかを決定できます。mergeType パラメータに指定した値に応じて、メニュー項目とそのサブメニュー項目をマージ先のメニューに対して追加、削除、置換、またはマージできます。mergeOrder パラメータは、作成されているメニュー項目が、メニューがマージされた場合に配置される位置を決定します。
さらに、このコンストラクタを使用して MenuItem を作成し、そのメニュー項目のクリック イベントを処理するイベント ハンドラに結びつけることができます。このコンストラクタに渡す EventHandler は、Click イベントを処理できるイベント ハンドラを呼び出すように設定する必要があります。このバージョンのコンストラクタを使用すると、Popup イベントと Select イベントを結び付けることもでき、メニュー項目がいつ選択されたのかを判断できます。これらのイベントは、サブメニュー項目の横にチェック マークを表示するかどうか、またはアプリケーションの状態に基づいてメニュー項目を有効にするか無効にするかなどを決定するタスクに対して使用できます。Select イベントと Click イベントは、親メニュー項目ではない MenuItem オブジェクトに対してだけ発生します。イベント処理の詳細については、「イベントとデリゲート」を参照してください。
使用例
キャプションとショートカット キーが指定されているメニュー項目を作成するコード例を次に示します。このメニュー項目には、Popup、Click、Select の各イベントに対して定義されているイベント ハンドラもあります。このメニュー項目がマージされる場合、この項目はマージ順序 0 でメニューに追加されます。
Public Sub CreateMyMenuItem()
' Submenu item array.
Dim SubMenus(3) as MenuItem
' Create three menu items to add to the submenu item array.
Dim SubMenuItem1, SubMenuItem2, SubMenuItem3 as MenuItem
SubMenuItem1 = New MenuItem ("Red")
SubMenuItem2 = New MenuItem ("Blue")
SubMenuItem3 = New MenuItem ("Green")
' Add the submenu items to the array.
SubMenus(0) = SubMenuItem1
SubMenus(1) = SubMenuItem2
SubMenus(2) = SubMenuItem3
' Create a MenuItem with caption, shortcut key,
' a Click, Popup, and Select event handler, menu merge type and order, and an
' array of submenu items specified.
Dim MenuItem1 As MenuItem
MenuItem1 = New MenuItem(MenuMerge.Add, 0, Shortcut.CtrlShiftC, "&Colors", _
AddressOf Me.MenuItem1_Click, _
AddressOf Me.MenuItem1_Popup, _
AddressOf Me.MenuItem1_Select, SubMenus)
End Sub
' The following method is an event handler for MenuItem1 to use when connecting the Click event.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs)
' Code goes here that handles the Click event.
End Sub
' The following method is an event handler for MenuItem1 to use when connecting the Popup event.
Private Sub MenuItem1_Popup(ByVal sender As System.Object, ByVal e as System.EventArgs)
' Code goes here that handles the Click event.
End Sub
' The following method is an event handler for MenuItem1 to use when connecting the Select event
Private Sub MenuItem1_Select(ByVal sender As System.Object, ByVal e as System.EventArgs)
' Code goes here that handles the Click event.
End Sub
public void CreateMyMenuItem()
{
// Submenu item array.
MenuItem[] subMenus = new MenuItem[3];
// Create three menu items to add to the submenu item array.
MenuItem subMenuItem1 = new MenuItem("Red");
MenuItem subMenuItem2 = new MenuItem("Blue");
MenuItem subMenuItem3 = new MenuItem("Green");
// Add the submenu items to the array.
subMenus[0] = subMenuItem1;
subMenus[1] = subMenuItem2;
subMenus[2] = subMenuItem3;
/* Create a MenuItem with caption, shortcut key,
a Click, Popup, and Select event handler, merge type and order, and an
array of submenu items specified.
*/
MenuItem menuItem1 = new MenuItem(MenuMerge.Add, 0,
Shortcut.CtrlShiftC, "&Colors",
new EventHandler(this.MenuItem1_Click),
new EventHandler(this.MenuItem1_Popup),
new EventHandler(this.MenuItem1_Select), subMenus);
}
// The following method is an event handler for menuItem1 to use when connecting the Click event.
private void MenuItem1_Click(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
}
// The following method is an event handler for menuItem1 to use when connecting the Popup event.
private void MenuItem1_Popup(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
}
// The following method is an event handler for menuItem1 to use when connecting the Select event
private void MenuItem1_Select(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
}
public:
void CreateMyMenuItem()
{
// Submenu item array.
array<MenuItem^>^ subMenus = gcnew array<MenuItem^>(3);
// Create three menu items to add to the submenu item array.
MenuItem^ subMenuItem1 = gcnew MenuItem( "Red" );
MenuItem^ subMenuItem2 = gcnew MenuItem( "Blue" );
MenuItem^ subMenuItem3 = gcnew MenuItem( "Green" );
// Add the submenu items to the array.
subMenus[ 0 ] = subMenuItem1;
subMenus[ 1 ] = subMenuItem2;
subMenus[ 2 ] = subMenuItem3;
/* Create a MenuItem with caption, shortcut key,
a Click, Popup, and Select event handler, merge type and order, and an
array of submenu items specified.
*/
MenuItem^ menuItem1 = gcnew MenuItem( MenuMerge::Add, 0,
Shortcut::CtrlShiftC, "&Colors",
gcnew EventHandler( this, &Form1::MenuItem1_Click ),
gcnew EventHandler( this, &Form1::MenuItem1_Popup ),
gcnew EventHandler( this, &Form1::MenuItem1_Select ), subMenus );
}
private:
// The following method is an event handler for menuItem1 to use when connecting the Click event.
void MenuItem1_Click( Object^ sender, EventArgs^ e )
{
// Code goes here that handles the Click event.
}
// The following method is an event handler for menuItem1 to use when connecting the Popup event.
void MenuItem1_Popup( Object^ sender, EventArgs^ e )
{
// Code goes here that handles the Click event.
}
// The following method is an event handler for menuItem1 to use when connecting the Select event
void MenuItem1_Select( Object^ sender, EventArgs^ e )
{
// Code goes here that handles the Click event.
}
public void CreateMyMenuItem()
{
// Submenu item array.
MenuItem subMenus[] = new MenuItem[3];
// Create three menu items to add to the submenu item array.
MenuItem subMenuItem1 = new MenuItem("Red");
MenuItem subMenuItem2 = new MenuItem("Blue");
MenuItem subMenuItem3 = new MenuItem("Green");
// Add the submenu items to the array.
subMenus.set_Item(0,subMenuItem1);
subMenus.set_Item(1,subMenuItem2);
subMenus.set_Item(2,subMenuItem3);
/* Create a MenuItem with caption, shortcut key,
a Click, Popup, and Select event handler, merge type and order, and
an array of submenu items specified.
*/
MenuItem menuItem1 = new MenuItem(MenuMerge.Add, 0,
Shortcut.CtrlShiftC, "&Colors",
new EventHandler(this.menuItem1_Click),
new EventHandler(this.menuItem1_Popup),
new EventHandler(this.menuItem1_Select), subMenus);
} //CreateMyMenuItem
// The following method is an event handler for menuItem1 to use
// when connecting the Click event.
private void menuItem1_Click(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
} //menuItem1_Click
// The following method is an event handler for menuItem1 to use
// when connecting the Popup event.
private void menuItem1_Popup(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
} //menuItem1_Popup
// The following method is an event handler for menuItem1 to use
// when connecting the Select event
private void menuItem1_Select(Object sender, EventArgs e)
{
// Code goes here that handles the Click event.
} //menuItem1_Select
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
参照
関連項目
MenuItem クラス
MenuItem メンバ
System.Windows.Forms 名前空間
Popup
Select
Click
MergeType
MergeOrder