c# 动态生成label 谁能给我个详细的源代码?

athena444 2009-03-20 03:14:16
初学c# 谁能帮我写一个动态生成label的代码 详细 详细
在Form里生成的 WindowsApplication


谢啦!!!!
...全文
493 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
athena444 2009-03-20
  • 打赏
  • 举报
回复
谢过~~~~
cja03 2009-03-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 athena444 的回复:]
access数据库 有一个表plan 一个字段message
message里有个值 this.label1.Visible = true;

在c#WindowsApplication的程序里 select出这个message的内容
但是就单纯的显示出了 this.labelDate.Visible = true;

我想让这条语句在c#里执行 该怎么做?
[/Quote]

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;

上MSDN查相关信息。


[Quote=引用 5 楼 athena444 的回复:]
createLabel(this.comboBox1.Text);
括号里的参数 是干什么用的?
[/Quote]

给Label一个文本而已。
athena444 2009-03-20
  • 打赏
  • 举报
回复
createLabel(this.comboBox1.Text);
括号里的参数 是干什么用的?
athena444 2009-03-20
  • 打赏
  • 举报
回复
access数据库 有一个表plan 一个字段message
message里有个值 this.label1.Visible = true;

在c#WindowsApplication的程序里 select出这个message的内容
但是就单纯的显示出了 this.labelDate.Visible = true;

我想让这条语句在c#里执行 该怎么做?

cja03 2009-03-20
  • 打赏
  • 举报
回复

public partial class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"Label1",
"Label2"});
this.comboBox1.Location = new System.Drawing.Point(23, 23);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(193, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(23, 69);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(193, 469);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(239, 579);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.GroupBox groupBox1;






public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
createLabel(this.comboBox1.Text);
}

void createLabel(string LabelText)
{
Label label = new Label();
label.Text = LabelText;
label.Location = new Point(12, this.groupBox1.Controls.Count * 18+24);
label.AutoSize = true;
this.groupBox1.Controls.Add(label);
}
}
athena444 2009-03-20
  • 打赏
  • 举报
回复
不好用啊 显示不出来

本来我是有一个combobox 我在cmbobox里选择一个 就让显示个label1 选择另一个就显示label2

我写了个方法
public void addlabelDate()
{
Label labelDate = new Label();
labelDate.AutoSize = true;
labelDate.Location = new System.Drawing.Point(26, 44);
labelDate.Name = "labelDate";
labelDate.Size = new System.Drawing.Size(29, 12);
labelDate.TabIndex = 0;
labelDate.Text = "日期";
this.groupBox2.Controls.Add(labelDate);
}

然后在这里调用
private void comboBoxCycle_TextChanged(object sender, EventArgs e)
{
addlabelDate();
}
但是显示不出来
zgke 2009-03-20
  • 打赏
  • 举报
回复
int _Height=0;
for (int i = 0; i < 10; i++)
{
Label sy = new Label();
sy.AutoSize = true;
sy.Location = new Point(0, _Height);
_Height += sy.Height;
sy.Text = i.ToString();
Controls.Add(sy);
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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