我們要實現上圖中的效果,需要如下的操作:
VB: Private Sub TestContextMenu_Load(sender As Object, e As EventArgs)Handles MyBase.Load Dim matTable As New DataTable matTable.Columns.Add("MAT_IMG", GetType(String)) matTable.Columns.Add("MAT_DESC1", GetType(String)) matTable.Rows.Add() matTable.Rows(0)("MAT_IMG") = "log" matTable.Rows(0)("MAT_DESC1") = "COMS" matTable.Rows.Add() matTable.Rows(1)("MAT_IMG") = "logon" matTable.Rows(1)("MAT_DESC1") = "smobiler" Me.gridView1.DataSource = matTable Me.gridView1.DataBind() End Sub
C#: private void TestContextMenu_Load(object sender, EventArgs e) { DataTable matTable = new DataTable(); matTable.Columns.Add("MAT_IMG", typeof(string)); matTable.Columns.Add("MAT_DESC1", typeof(string)); matTable.Rows.Add(); matTable.Rows[0]["MAT_IMG"] = "log"; matTable.Rows[0]["MAT_DESC1"] = "COMS"; matTable.Rows.Add(); matTable.Rows[1]["MAT_IMG"] = "logon"; matTable.Rows[1]["MAT_DESC1"] = "smobiler"; this.gridView1.DataSource = matTable; this.gridView1.DataBind(); }
VB: Private Sub gridView1_CellLongClick(sender As Object, e As GridViewCellEventArgs) Handles gridView1.CellLongClick contextMenu1.Show() End Sub
C#: private void gridView1_CellLongClick(object sender, GridViewCellEventArgs e) { contextMenu1.Show(); }
注:調用ContextMenu控件
新創建MobileForm項,並命名為MessageShow,並拖入一個Label控件和一個Image控件,如圖1;
Label1的DataMember屬性(綁定需要顯示的列),如圖2;
contextmenu的Layout屬性,綁定新建的窗體MessageShow1,如圖3;
圖1 圖2 圖3獲取或設置ContextMenuItem的背景,默認設置為“White”,如圖1;
打開集合編輯器,並點擊"添加",ForeColor屬性(文本顏色),Icon屬性(Item的Icon圖像資源),Text屬性(Item的文本),Value屬性(內部值,不在界面上顯示),如圖2、圖3;
設置ContextMenu顯示的位置,默認設置為“LastTouch”,表示顯示在最後觸摸的地方,如圖4;
若將該屬性設置為“CenterScreen”,則表示顯示在屏幕中心。
圖1 圖2 圖3 圖4