txtLabelText 要么是属性,要么是字段.
不太明白你的意思.
不太明白你的意思.
解决方案 »
- 关于Code First 代码迁移问题
- C# 如何定位到TXT文档的某一行??
- 我做了个进销存系统我做的功能模块比较多,但不知道用几个库比较合适。
- 【求助】关于“覆盖Object.Equals()”和“隐式类型转换”的问题,谢谢!
- VS.NET新建不项目怎么办,有人遇到过这个问题么??
- C#教程电子书有那一些写好的
- 一个线程内创建的控件为什么不能成为另一个线程内创建的控件的父级<望好心人不遗余力帮助解决>
- C# 面试归来 分享下问的问题
- 调用 activex 控件问题,请教
- 请教WebClient下载文件显示进度条的问题
- 用过水晶报表DataSet1的高手,请进!在线待问题。。。。。。。。。
- 大散200分!感情复杂!斧头走了思归来了,,!!!今天才发现的。。。。要分的人进来接吧。。给200,,,UP的有分
下面是全部源代码:
1。自定义控件
namespace LabelTextbox
{
public class ctlLabelTextbox : System.Windows.Forms.UserControl
{
public enum PositionEnum
{
Right,
Below
} private System.Windows.Forms.Label lblTextBox;
private System.Windows.Forms.TextBox txtLabelText;
private System.ComponentModel.Container components = null; private PositionEnum mPosition = PositionEnum.Right;
private int mTextboxMargin = 0; public event System.EventHandler PositionChanged; public event System.EventHandler MaxLengthChanged; public event System.EventHandler MaxLengthReached; public ctlLabelTextbox()
{
InitializeComponent();
this.SizeChanged += new System.EventHandler(this.OnSizeChanged);
this.Load += new EventHandler(this.OnLoad); this.txtLabelText.KeyDown += new KeyEventHandler(this.txtLabelText_KeyDown);
this.txtLabelText.KeyUp += new KeyEventHandler(this.txtLabelText_KeyUp);
this.txtLabelText.KeyPress += new KeyPressEventHandler(this.txtLabelText_KeyPress);
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
} public PositionEnum Position
{
get
{
return mPosition;
}
set
{
mPosition = value;
MoveControls();
if (PositionChanged != null)
{
System.Delegate[] subscribers = PositionChanged.GetInvocationList();
foreach (System.EventHandler target in subscribers)
{
target(this, new EventArgs()); // Call the method
}
}
}
}
public int TextboxMargin
{
get
{
return mTextboxMargin;
}
set
{
mTextboxMargin = value;
MoveControls();
}
} public string LabelText
{
get
{
return lblTextBox.Text;
}
set
{
lblTextBox.Text = value;
MoveControls(); // Call MoveControls to make the size the textbox if needed
}
} public string TextboxText
{
get
{
return txtLabelText.Text;
}
set
{
txtLabelText.Text = value;
}
}
public int MaxLength
{
get
{
return txtLabelText.MaxLength;
} set
{
txtLabelText.MaxLength=value; if(MaxLengthChanged!=null)
{
System.Delegate[] subscribers=MaxLengthChanged.GetInvocationList(); foreach (System.EventHandler target in subscribers)
{
target(this,new EventArgs());
}
}
}
private void MoveControls()
{
switch (mPosition)
{
case PositionEnum.Below:
{
// Place the top of the Textbox just below the label
this.txtLabelText.Top = this.lblTextBox.Bottom;
this.txtLabelText.Left = this.lblTextBox.Left; // Change the width of the Textbox to equal the width of the control
this.txtLabelText.Width = this.Width;
this.Height = txtLabelText.Height + lblTextBox.Height;
break;
}
case PositionEnum.Right:
{
// Set the top of the textbox to equal that of the label
txtLabelText.Top = lblTextBox.Top; // If the margin is zero, we'll place the textbox next to the label
if (mTextboxMargin == 0)
{
int width = this.Width-lblTextBox.Width-3;
txtLabelText.Left = lblTextBox.Right + 3;
txtLabelText.Width = width;
}
else
{
// If the margin isn't zero, we place the textbox where the user have specified
txtLabelText.Left = mTextboxMargin;
txtLabelText.Width = this.Right-mTextboxMargin;
}
break;
}
}
} private void OnSizeChanged(object sender, System.EventArgs e)
{
MoveControls();
} private void txtLabelText_KeyDown(object sender, KeyEventArgs e)
{
OnKeyDown(e);
} private void txtLabelText_KeyUp(object sender, KeyEventArgs e)
{
OnKeyUp(e);
} private void txtLabelText_KeyPress(object sender, KeyPressEventArgs e)
{
OnKeyPress(e);
} private void OnControlAdded(object sender, ControlEventArgs e)
{
MoveControls();
} private void OnLoad(object sender, EventArgs e)
{
lblTextBox.Text = this.Name;
this.Height = txtLabelText.Height + lblTextBox.Height;
MoveControls();
} #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()
{
this.lblTextBox = new System.Windows.Forms.Label();
this.txtLabelText = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lblTextBox
//
this.lblTextBox.AutoSize = true;
this.lblTextBox.Location = new System.Drawing.Point(0, 0);
this.lblTextBox.Name = "lblTextBox";
this.lblTextBox.Size = new System.Drawing.Size(35, 17);
this.lblTextBox.TabIndex = 0;
this.lblTextBox.Text = "Label";
//
// txtLabelText
//
this.txtLabelText.Location = new System.Drawing.Point(8, 40);
this.txtLabelText.Name = "txtLabelText";
this.txtLabelText.TabIndex = 1;
this.txtLabelText.Text = "";
this.txtLabelText.TextChanged += new System.EventHandler(this.txtLabelText_TextChanged);
//
// ctlLabelTextbox
//
this.Controls.Add(this.txtLabelText);
this.Controls.Add(this.lblTextBox);
this.Name = "ctlLabelTextbox";
this.Load += new System.EventHandler(this.ctlLabelTextbox_Load);
this.ResumeLayout(false); }
#endregion private void ctlLabelTextbox_Load(object sender, System.EventArgs e)
{
} private void txtLabelText_TextChanged(object sender, System.EventArgs e)
{
if(MaxLengthReached!=null)
{
System.Delegate[] subscribers=MaxLengthReached.GetInvocationList();
foreach (System.EventHandler target in subscribers)
{
target(this,new EventArgs()); } }
} }
}
namespace LabelTextboxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private LabelTextbox.ctlLabelTextbox myControl=new LabelTextbox.ctlLabelTextbox();
private LabelTextbox.ctlLabelTextbox ctlLabelTextbox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
myControl.PositionChanged += new EventHandler(this.myControl_PositionChanged); myControl.MaxLengthChanged+=new EventHandler(this.myControl_MaxLengthChanged); myControl.MaxLengthReached+=new EventHandler(this.myControl_MaxLengthReached);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form 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()
{
this.button1 = new System.Windows.Forms.Button();
this.ctlLabelTextbox1 = new LabelTextbox.ctlLabelTextbox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(125, 258);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(90, 25);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// ctlLabelTextbox1
//
this.ctlLabelTextbox1.LabelText = "ctlLabelTextbox";
this.ctlLabelTextbox1.Location = new System.Drawing.Point(0, 0);
this.ctlLabelTextbox1.MaxLength = 32767;
this.ctlLabelTextbox1.Name = "ctlLabelTextbox1";
this.ctlLabelTextbox1.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Right;
this.ctlLabelTextbox1.Size = new System.Drawing.Size(150, 38);
this.ctlLabelTextbox1.TabIndex = 1;
this.ctlLabelTextbox1.TextboxMargin = 0;
this.ctlLabelTextbox1.TextboxText = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 96);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(350, 294);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.ctlLabelTextbox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if (myControl.Position == LabelTextbox.ctlLabelTextbox.PositionEnum.Below)
{
myControl.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Right;
} else {
myControl.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Below;
}
} private void myControl_PositionChanged(object sender, EventArgs e)
{
MessageBox.Show("Changed");
} private void myControl_MaxLengthChanged(object sender,EventArgs e)
{
//int intMaxLengthChanged;
//intMaxLengthChanged=myControl.MaxLength;
MessageBox.Show(Convert.ToString(myControl.MaxLength)); } private void textBox1_TextChanged(object sender, System.EventArgs e)
{
myControl.MaxLength=Convert.ToInt16(textBox1.Text); } private void myControl_MaxLengthReached(object sender,EventArgs e)
{
TextBox tb = (TextBox)myControl.Controls[1]; if(tb.Text.Length==myControl.MaxLength)
{
MessageBox.Show("MaxLength reached!"); }
} }
}