WinForms:是否存在将标签与文本框相关联的概念?

Use*_*ser 20 c# label textbox visual-studio winforms

我正在使用Visual Studio 2010和C#.在Windows窗体开发中是否存在以某种方式将标签链接到文本框的概念?什么东西让他们作为一个整体一起移动?在ASP.NET世界中,有标签控件的AssociatedControlId属性.我还认为我记得MS Access表单设计者有一些方法可以将标签与控件相关联(或链接).此功能是否甚至存在于Visual Studio世界中?

如果没有,如何使用控件对标签进行分组,以便在移动文本框时也不必手动移动标签?

dha*_*ech 23

似乎没有内置的.你可以自己Field上课.以下是一个完整的例子.

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的系统上示例程序的样子:

在此输入图像描述


Nei*_*eil 9

没有 - 至少有开箱即用的控件.如果你想要这个,你可以通过用户控件实现它.

通常,winforms不像HTML那样以行方式驱动.