【问题标题】:Possible to Create ToolStripMenuItem With Both TextBox and Label?可以使用 TextBox 和 Label 创建 ToolStripMenuItem 吗?
【发布时间】:2009-03-05 02:13:59
【问题描述】:

在 WinForms .Net 2.0 应用程序中,我想使用 ToolStripMenuItem 创建一个上下文菜单,该菜单在项目本身中具有标签和文本框。可以在 Access 中找到我正在谈论的示例 - 查看 Access 表时,上下文菜单具有“按选择过滤”、“过滤排除选择”和“过滤为:_ _ _ _ _ _ ”。第三个选项本质上是单个项目中的标签和文本框。这是我不知道该怎么做。

我用两个单独的 ToolStripMenuItems 来实现这个没有问题 - 一个用于文本,然后是一个只有文本框的子项。但这很尴尬,而且不如 Access 中的实现好看。

谁能指出我正确的方向?我无法搜索,因为我找到的所有内容似乎都与文本框本身的上下文菜单有关。

【问题讨论】:

    标签: .net winforms user-interface


    【解决方案1】:

    这里给你一个答案:

    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
    

    【讨论】:

    • 谢谢安迪!正是我想要的。当答案就在文档中时,我讨厌它,而我只是错过了它。感谢您的链接和示例。
    【解决方案2】:

    更通用的解决方案:

     public abstract class CaptionControl<T> : FlowLayoutPanel where T: Control, new()
    {
        public new Windows.Forms.Label With {.AutoSize = true, .Anchor == AnchorStyles.None, .Text == "Label"} Label
        {
    
        Public ReadOnly Property ControlMain() As new T();
    
        Public Property Caption As string;
            get
            {
                return Label.Text;
            }
            set
            {
                if (value != Label.Text)
                {
                    Label.Text = value;
                }
            }
        }
         
        public CaptionControl()
        {
    
            this.Dock = DockStyle.Top;
            //Me.Bounds = Parent.ClientRectangle
    
            this.FlowDirection = FlowDirection.LeftToRight;
            this.WrapContents = false;
            this.Anchor = AnchorStyles.Left | AnchorStyles.Right;
    
            this.Controls.Add(Label);
            this.Controls.Add(ControlMain);
    
            ControlMain.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            ControlMain.Dock = DockStyle.Top;
    
            // Me.Controls.Add(flp)
    
        }
    
    }
    public class CaptionTextBox : CaptionControl<TextBox>
    {
        public TextBox TextBox
        {
            get
            {
                return ControlMain;
            }
        }
    }
    public class CaptionNumericUpdown : CaptionControl<NumericUpDown>
    {
        public NumericUpDown Numericupdown
        {
            get
            {
                return ControlMain;
            }
        }
    }
    

    后跟:

     <System.ComponentModel.DesignerCategory("Code")>
        <System.Windows.Forms.Design.ToolStripItemDesignerAvailability(System.Windows.Forms.Design.ToolStripItemDesignerAvailability.All)>
        Public Class ToolstripCaptionTextBox
            Inherits ToolStripControlHost
            Public Sub New()
                MyBase.New(New CaptionTextBox)
    
            End Sub
            ''' <summary>Create a strongly typed property called MainControl - handy to prevent casting everywhere.</summary>
            <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
            Public ReadOnly Property HostedTextBox As CaptionTextBox
                Get
                    Return TryCast(Control, CaptionTextBox)
                End Get
            End Property
    
          
        End Class
    

    等等……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      相关资源
      最近更新 更多