Label和TextBox组合控件的显示问题

xzjxylophone 2009-03-23 09:55:20
需要一个Label和TextBox组合控件
定义的类如下:
class LabelTextBox : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
public LabelTextBox(string labelName, string textBoxName,
Size labelSize, Size textBoxSize,
Point labelPoint, Point textBoxPoint)
{
this.label = new Label();
label.AutoSize = false;
label.Location = labelPoint;
label.Name = "label";
label.Size = labelSize;
label.Text = labelName + ":";
label.TextAlign = ContentAlignment.MiddleRight;



this.textBox = new TextBox();
textBox.Location = textBoxPoint;
textBox.Name = "textBox";
textBox.Size = textBoxSize;
textBox.Text = textBoxName;


this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
}
}


在Form中调用此控件为什么一直不显示了?
string labelName = xsa.Name;
string textBoxName = xsa.QualifiedName.Name;
Size labelSize = new System.Drawing.Size(150, 20);
Size textBoxSize = new System.Drawing.Size(150, 20);
Point labelPoint = new System.Drawing.Point(5, 20);
Point textBoxPoint = new System.Drawing.Point(160, 20);


LabelTextBox ltb = new LabelTextBox(labelName, textBoxName,
labelSize, textBoxSize,
labelPoint, textBoxPoint);
this.groupBox1.Controls.Add(ltb);

怎么只显示label控件而没有textbox控件?
...全文
402 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzjxylophone 2009-03-23
  • 打赏
  • 举报
回复
OK
第一个解决方案是在构造函数中加入如下的代码:

public LabelTextBox(string labelName, string textBoxName,
Size labelSize, Size textBoxSize,
Point labelPoint, Point textBoxPoint)
{
this.label = new Label();
label.AutoSize = false;
label.Location = labelPoint;
label.Name = "label";
label.Size = labelSize;
label.Text = labelName + ":";
label.TextAlign = ContentAlignment.MiddleRight;


this.textBox = new TextBox();
textBox.Location = textBoxPoint;
textBox.Name = "textBox";
textBox.Size = textBoxSize;
textBox.Text = textBoxName;

this.Size = new Size(labelSize.Width + textBoxSize.Width, labelSize.Height + textBoxSize.Height);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
}
}


ojekleen 2009-03-23
  • 打赏
  • 举报
回复
直接写复合控件,把两个控件拉进去.
xzjxylophone 2009-03-23
  • 打赏
  • 举报
回复
问题已经解决了
谢谢你了啊!!

ExTextBox代码如下:
class ExTextBox : System.Windows.Forms.TextBox
{
private System.Windows.Forms.Label label;
private XmlSchemaAttribute xsAttribut;
public ExTextBox(string labelName, Size labelSize,Point labelPoint, XmlSchemaAttribute xsAttribut)
{
this.label = new Label();
label.AutoSize = false;
label.Location = labelPoint;
label.Name = "label";
label.Size = labelSize;
label.Text = labelName + ":";
label.TextAlign = ContentAlignment.MiddleRight;
this.xsAttribut = xsAttribut;

}
public Label Label
{
get
{
return this.label;
}
set
{
this.label = value;
}
}

调用如下:
string labelName = xsa.Name;
string textBoxName = xsa.QualifiedName.Name;
Size labelSize = new System.Drawing.Size(userSizeX, userSizeY);
Size textBoxSize = new System.Drawing.Size(userSizeX, userSizeY);
Point labelPoint = new System.Drawing.Point(userX, userY);
Point textBoxPoint = new System.Drawing.Point(userRightX, userY);
XmlSchemaAttribute xsAttribut = xsa;

ExTextBox etb = new ExTextBox(labelName, labelSize, labelPoint, xsAttribut);
etb.Name = textBoxName;
etb.Text = textBoxName;
etb.Size = textBoxSize;
etb.Location = textBoxPoint;
this.groupBox1.Controls.Add(etb.Label);
this.groupBox1.Controls.Add(etb);



增加一个属性就OK了
然后在groupbox里再添加就OK了

注意标志为红色的字体
xzjxylophone 2009-03-23
  • 打赏
  • 举报
回复
heyu52

我按照你的方法实了, 为什么 只显示了TextBox 而没有显示Label了??
我的代码如下:
class ExTextBox : System.Windows.Forms.TextBox
{
private System.Windows.Forms.Label label;
private XmlSchemaAttribute xsAttribut;
public ExTextBox(string labelName, Size labelSize,Point labelPoint, XmlSchemaAttribute xsAttribut)
{
this.label = new Label();
label.AutoSize = false;
label.Location = labelPoint;
label.Name = "label";
label.Size = labelSize;
label.Text = labelName + ":";
label.TextAlign = ContentAlignment.MiddleRight;
this.xsAttribut = xsAttribut;

}
}


调用如下:
string labelName = xsa.Name;
string textBoxName = xsa.QualifiedName.Name;
Size labelSize = new System.Drawing.Size(userSizeX, userSizeY);
Size textBoxSize = new System.Drawing.Size(userSizeX, userSizeY);
Point labelPoint = new System.Drawing.Point(userX, userY);
Point textBoxPoint = new System.Drawing.Point(userRightX, userY);
XmlSchemaAttribute xsAttribut = xsa;

ExTextBox etb = new ExTextBox(labelName, labelSize, labelPoint, xsAttribut);
etb.Name = textBoxName;
etb.Text = textBoxName;
etb.Size = textBoxSize;
etb.Location = textBoxPoint;

this.groupBox1.Controls.Add(etb);
heyu52 2009-03-23
  • 打赏
  • 举报
回复
你为什么不重写textbox类,然后在它里面添加一个label的属性,这样子不就方便多了

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧