2013-02-27 51 views
1

我有一些動態創建帶有上下文菜單條的標籤。當您右鍵單擊標籤時,會爲您提供上下文菜單條。如何在單擊上下文菜單條中的項目時獲取文本?我知道這是一個非常簡單的問題,但我需要一些幫助。 我試過右鍵單擊時動態創建項目的文本

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click 
     MsgBox(sender.Text) 
End Sub 

我試圖讓產生的ContextMenuStrip標籤的文本。

回答

2

我認爲你正在尋找該控件激發上下文菜單中,這樣的事情:

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DeleteToolStripMenuItem.Click 

    Dim menuItem As ToolStripItem = TryCast(sender, ToolStripItem) 
    If menuItem IsNot Nothing Then 
    Dim owner As ContextMenuStrip = TryCast(menuItem.Owner, ContextMenuStrip) 
    If owner IsNot Nothing Then 
     Dim sourceControl As Control = owner.SourceControl 
     MessageBox.Show(sourceControl.Text) 
    End If 
    End If 
End Sub 

轉換爲VB.Net從Determine what control the ContextMenuStrip was used on

+0

我試圖讓'標籤的文本',而不是'contextmenustrip'。這會爲此工作...? – 2013-02-27 17:46:28

+0

@Gordan什麼是FileItem?標籤或菜單項? – LarsTech 2013-02-27 17:49:11

+0

FileItem是一個標籤。我會改變代碼。對困惑感到抱歉。 – 2013-02-27 17:50:02