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

前端 未结 7 829
梦谈多话
梦谈多话 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:19

    This is my solution, using a TableLayoutPanel to place label and input control,

    Presetted some useful properties:

    • Auto size label by content
    • Label margin top for vertical-align:middle like style
    • Fill remain space by input control

    Preview

    The code may need further wrap

    internal class TextBoxField : TableLayoutPanel
    {
        private readonly TextBox _textBox;
    
        public string Text
        {
            get => _textBox.Text;
            set => _textBox.Text = value;
        }
    
        public TextBoxField(string labelText)
        {
            var label = new Label { Text = labelText, AutoSize = true };
            var labelMargin = label.Margin;
            labelMargin.Top = 8;
            label.Margin = labelMargin;
            _textBox = new TextBox { Dock = DockStyle.Fill };
    
            AutoSize = true;
    
            ColumnCount = 2;
            RowCount = 1;
            ColumnStyles.Add(new ColumnStyle());
            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            RowStyles.Add(new RowStyle());
            Controls.Add(label, 0, 0);
            Controls.Add(_textBox, 1, 0);
        }
    }
    
    internal class DateTimePickerField : TableLayoutPanel
    {
        private readonly DateTimePicker _dateTimePicker;
    
        public DateTime Value
        {
            get => _dateTimePicker.Value;
            set => _dateTimePicker.Value = value;
        }
    
        public DateTimePickerField(string labelText)
        {
            var label = new Label { Text = labelText, AutoSize = true };
            var labelMargin = label.Margin;
            labelMargin.Top = 8;
            label.Margin = labelMargin;
            _dateTimePicker = new DateTimePicker { Dock = DockStyle.Fill };
    
            AutoSize = true;
    
            ColumnCount = 2;
            RowCount = 1;
            ColumnStyles.Add(new ColumnStyle());
            ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            RowStyles.Add(new RowStyle());
            Controls.Add(label, 0, 0);
            Controls.Add(_dateTimePicker, 1, 0);
        }
    }
    

    Useage:

    var frm = new Form
    {
        AutoSize = true,
        StartPosition = FormStartPosition.CenterParent,
        Text = "Assoc Device",
    };
    
    var txtGpsCode = new TextBoxField("GpsCode") { Dock = DockStyle.Bottom, TabIndex = 1 };
    var dtp = new DateTimePickerField("Expire date") { Dock = DockStyle.Bottom, TabIndex = 2 };
    var button = new Button { Text = "OK", DialogResult = DialogResult.OK, Dock = DockStyle.Bottom };
    
    frm.Controls.Add(txtGpsCode);
    frm.Controls.Add(dtp);
    frm.Controls.Add(button);
    
    frm.AcceptButton = button;
    
    frm.Height = 0;
    
    frm.ShowDialog();
    
    0 讨论(0)
提交回复
热议问题