📄 labeledcontrolbase.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace CodeTemplate
{
internal class TagChangedEventArgs: EventArgs
{
public TagChangedEventArgs(String tagName, String tagValue)
{
this.Name = tagName;
this.Value = tagValue;
}
public readonly String Name;
public readonly String Value;
}
internal delegate void TagChangedEventHandler(Object sender, TagChangedEventArgs e);
/// <summary>
/// Summary description for LabeledControlBase.
/// </summary>
internal class LabeledControlBase : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label m_label;
private System.Windows.Forms.Control m_control;
protected const Int32 s_ctrlDist = 7;
protected const Int32 s_margin = 1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public static LabeledControlBase CreateControl(Tag tag)
{
LabeledControlBase ctrl = null;
if(tag.IsCombo)
ctrl = new LabeledComboBox(tag.ControlType, tag.Formats[tag.ControlType == TagControlType.DropDownControl ? "VALUES" : "FIXEDVALUES"].Arguments);
else
ctrl = new LabeledTextBox(tag.ControlType, tag.ControlType == TagControlType.MultiLineControl ? tag.Formats["MULTILINE"].Arguments : String.Empty);
return ctrl;
}
public LabeledControlBase(Control textCtrl)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
this.m_label = new System.Windows.Forms.Label();
this.m_control = textCtrl;
this.SuspendLayout();
//
// m_label
//
this.m_label.Location = new System.Drawing.Point(1, 1);
this.m_label.Name = "m_label";
this.m_label.Size = new System.Drawing.Size(80, 20);
this.m_label.TabIndex = 0;
this.m_label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// m_control
//
this.m_control.Location = new System.Drawing.Point(88, 1);
this.m_control.Name = "m_control";
this.m_control.Size = new System.Drawing.Size(110, 20);
this.m_control.TabIndex = 1;
this.m_control.Text = "";
this.m_control.TextChanged += new System.EventHandler(this.m_control_TextChanged);
//
// LabeledTextBox
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.m_control,
this.m_label});
this.Name = "LabeledTextBox";
this.Size = new System.Drawing.Size(200, 24);
this.SizeChanged += new System.EventHandler(this.LabeledTextBox_SizeChanged);
this.ResumeLayout(false);
resizeControls((Width - s_ctrlDist - (2 * s_margin)) / 2); // Initially split 50/50
}
public new Int32 Height
{
get
{
Int32 height = m_label.Height > m_control.Height ? m_label.Height : m_control.Height;
return height += (2 * s_margin);
}
}
private void resizeControls(Int32 labelWidth)
{
Int32 ctrlHeight = this.Height - (2 * s_margin);
m_label.Location = new Point(s_margin, s_margin);
m_label.Size = new Size(labelWidth, m_label.Height);
m_control.Location = new Point(s_margin + labelWidth + s_ctrlDist, s_margin);
m_control.Size = new Size(Width - (labelWidth + s_ctrlDist + (2 * s_margin)), m_control.Height);
}
private Int32 validateLabelWidth(Int32 newWidth)
{
if(newWidth > (Width - s_ctrlDist - (2 * s_margin)) * 0.75f)
return m_label.Width;
return newWidth;
}
public String Label
{
get { return m_label.Text; }
set { m_label.Text = value; }
}
public override String Text
{
get { return m_control.Text; }
set { m_control.Text = value; }
}
public String TagName
{
get { return (String) base.Tag; }
set { base.Tag = value.ToUpper(); }
}
public Int32 LabelWidth
{
get { return m_label.Width; }
set { m_label.Size = new Size(validateLabelWidth(value), m_label.Height); }
}
protected Control Control
{
get { return m_control; }
}
private void LabeledTextBox_SizeChanged(Object sender, System.EventArgs e)
{
resizeControls(m_label.Width);
}
private void m_control_TextChanged(Object sender, System.EventArgs e)
{
OnTagChanged(new TagChangedEventArgs(this.TagName, m_control.Text));
}
#region Custom Events
public virtual event TagChangedEventHandler TagChanged;
protected virtual void OnTagChanged(TagChangedEventArgs e)
{
if(TagChanged != null)
TagChanged(this, e);
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(Boolean disposing)
{
if(disposing)
{
if(components != null)
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -