WinForms: Is there a concept of associating a label with a textbox?

前端 未结 7 828
梦谈多话
梦谈多话 2020-12-18 18:21

I\'m using Visual Studio 2010 with C#. Is there a concept in Windows Forms development of somehow linking a label with is text box? Something so that they move together as

相关标签:
7条回答
  • 2020-12-18 19:02

    There doesn't appear to be a built in one. You can roll your own Field class though. Below is a complete example.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace FieldClassTest
    {
        class Field : FlowLayoutPanel
        {
            public Label label;
            public TextBox text_box;
    
            public Field(string label_text)
                : base()
            {
                AutoSize = true;
    
                label = new Label();
                label.Text = label_text;
                label.AutoSize = true;
                label.Anchor = AnchorStyles.Left;
                label.TextAlign = ContentAlignment.MiddleLeft;
    
                Controls.Add(label);
    
                text_box = new TextBox();
    
                Controls.Add(text_box);
            }
        }
    
        static class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                var form = new Form();
    
                var panel = new FlowLayoutPanel();
                panel.FlowDirection = FlowDirection.TopDown;
                panel.Dock = DockStyle.Fill;
    
                var first_name = new Field("First Name");
                panel.Controls.Add(first_name);
    
                var last_name = new Field("Last Name");
                panel.Controls.Add(last_name);
    
                form.Controls.Add(panel);
    
                Application.Run(form);
            }
        }
    }
    

    Here's what the example program looks like on my system:

    enter image description here

    0 讨论(0)
  • 2020-12-18 19:03

    No there is not - at least with the out of the box controls. If you want this you could achieve it with a user control.

    In general the winforms is not line driven in the same way as HTML is.

    0 讨论(0)
  • 2020-12-18 19:08

    If you want to group labels with other controls (or group controls in general), then use the System.Windows.Forms.Panel control. The specific purpose of the Panel control is to group collections of controls.

    More information Panel Class (System.Windows.Forms)

    If you want a higher degree of control (rather than using a Panel), then you could create a UserControl, that encapsulates a Label and a Control.

    0 讨论(0)
  • 2020-12-18 19:14

    You can use extension methods to do it, follow example:

    Private associatedLabels As New Dictionary(Of Control, Label)    
    <Extension()>
    Public Sub AssociateLabel(ByVal control As Control, ByVal label As Label)
        If (Not associatedLabels.ContainsKey(control)) Then
            associatedLabels.Add(control, label)
        End If
    End Sub
    
    <Extension()>
    Public Function GetAssociatedLabel(ByVal control As Control) As Label
        If (associatedLabels.ContainsKey(control)) Then
            Return associatedLabels(control)
        Else
            Throw New Exception("There is no associated label")
        End If
    End Function
    
    0 讨论(0)
  • 2020-12-18 19:15

    I 2nd @Neils answer of just creating a user control with a textbox in it. The panel can be used to group controls, but it can be pretty tedious if you have a lot of controls on the form.

    If you want to support more than just textboxes, WinForms allows you to create your own designer. If you inherit your designer from the ParentControlDesigner class, you can drop any control you want into your custom label control.

    0 讨论(0)
  • 2020-12-18 19:17

    i think the best bet would be to use a GroupBox.

    0 讨论(0)
提交回复
热议问题