2009-03-05 19 views
3

在WinForms .Net 2.0应用程序中,我想创建一个带有ToolStripMenuItem的上下文菜单,该ToolStripMenuItem在项目本身中同时包含标签和文本框。在Access中可以找到我正在讨论的一个示例 - 在查看Access表时,上下文菜单中有“筛选依据选择”,“筛选除外选择”和“筛选:_ _ _ _ _ _ ”。这第三个选项本质上是一个标签和单个项目中的文本框。这是我无法弄清楚如何去做的。可能与TextBox和Label创建ToolStripMenuItem?

我已经没有问题实施这与两个单独 ToolStripMenuItems - 一个文本,然后一个只有文本框的孩子。但是这很尴尬,不像在Access中的实现那么好看。

任何人都可以在正确的方向指向我吗?我在搜索时遇到了问题,因为我发现的所有内容似乎都与文本框本身的上下文菜单相关。

回答

2

这是给你一个答案:

How to: Wrap a Windows Forms Control with ToolStripControlHost
ToolStripControlHost Class

而且,一个简短的演示,我写(记住它看起来可怕,因为我还没有称呼它在所有):

(VB.net,因为我喜欢这一点,你没有指定者优先哪种语言)

Public Class ToolStripEntry 
    Inherits ToolStripControlHost 

    Public Sub New() 
     MyBase.New(New ControlPanel) 

    End Sub 

    Public ReadOnly Property ControlPanelControl() As ControlPanel 
     Get 
      Return CType(Me.Control, ControlPanel) 
     End Get 
    End Property 

End Class 


Public Class ControlPanel 
    Inherits Panel 

    Friend WithEvents txt As New TextBox //with events so you can just use the events 
    Friend WithEvents lbl As New Label //don think you can just do that in c#, but you get the idea 

    Public Sub New() 

     lbl.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Bottom 
     lbl.Text = "Test" 
     lbl.TextAlign = ContentAlignment.MiddleLeft 
     lbl.Size = New Size(30, Me.Height)   //think of somthing! 
     lbl.Location = New Point(0, 0) 
     lbl.Parent = Me 

     txt.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top 
     txt.Location = New Point(lbl.Right, 0) 
     txt.Width = Me.Width - txt.Left 
     txt.Parent = Me 

    End Sub 

End Class 
+0

感谢安迪!正是我在找什么。当我在文档中找到答案并且我错过了它时,我讨厌它。感谢您的链接和示例。 – Rob3C 2009-03-05 11:19:54