我在C#中使用Visual Studio 2010。在Windows窗体开发中是否有一种概念将某个标签与文本框相链接?是什么让他们一起移动?在ASP.NET世界中,有标签控件的AssociatedControlId属性。我还想我记得MS Access表单设计者有一些关联(或链接)标签和控件的方式。 Visual Studio世界中是否存在此功能?WinForms:有没有关联标签和文本框的概念?
如果不是,你如何将控件分组,如果你移动一个文本框,你不必手动移动标签?
我在C#中使用Visual Studio 2010。在Windows窗体开发中是否有一种概念将某个标签与文本框相链接?是什么让他们一起移动?在ASP.NET世界中,有标签控件的AssociatedControlId属性。我还想我记得MS Access表单设计者有一些关联(或链接)标签和控件的方式。 Visual Studio世界中是否存在此功能?WinForms:有没有关联标签和文本框的概念?
如果不是,你如何将控件分组,如果你移动一个文本框,你不必手动移动标签?
不,没有 - 至少与开箱即用的控制。如果你想要这个,你可以通过用户控制来实现它。
一般而言,winforms不是像HTML那样以线驱动。
如果您想将标签与其他控件(或通常的组控件)分组,则使用System.Windows.Forms.Panel
控件。 Panel控制的具体目的是group collections of controls
。
如果你想要更高程度的控制(而不是使用面板)的更多信息Panel Class (System.Windows.Forms)
,那么你可以创建一个UserControl
,一个封装Label
和Control
。
我第二@Neils答案只是创建一个用户控件与其中的文本框。该面板可用于对控件进行分组,但如果您在表单上有很多控件,则该控件可能非常繁琐。
如果您想要支持的不仅仅是文本框,WinForms允许您创建自己的设计器。如果您从ParentControlDesigner类继承设计师,则可以将任何需要的控件放入自定义标签控件中。
我认为最好的选择是使用GroupBox。
似乎没有内置的一个。虽然你可以推出自己的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);
}
}
}
这里的示例程序是什么样子我的系统上:
您可以使用扩展方法来做到这一点,请按照例如:
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
这是我的解决办法,使用TableLayoutPanel
来放置标签和输入控件,
预置了一些有用的特性:通过内容
预览
的代码还需要包装
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);
}
}
用途:
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();
随着访问“现场控制”,当你点击的焦点是考虑到链接文本框的标签上。您也可以在设计模式下自由移动两个控件(标签和文本框)。我不会说这是一个竞争的例子... – 2016-07-14 15:45:55