C# 在tablelayuot中添加标签
我尝试向表格布局添加标签和行。当我像这样添加到列表视图时:
ListViewItem ite = new ListViewItem(tag);
ite.Text = (tag + " " + description + war);
listView2.Items.Add(ite.Text);
它有效,但是当我尝试另一个时它不起作用。为什么?没有任何错误或异常。
foreach (DataElement elementy in sq)
{
for (int k = 0; k == row_number; k = k + 1)
{
tag = elementy.Tag.ToString();
description = elementy.VR.Tag.GetDictionaryEntry().Description;
// ListViewItem ite = new ListViewItem(tag);
//ite.Text = (tag + " " + description + war);
//listView2.Items.Add(ite.Text);
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Label et_tag = new Label();
et_tag.AutoSize= true;
et_tag.Text = tag;
tableLayoutPanel1.Controls.Add(et_tag, 0, k);
Label op = new Label();
op.AutoSize = true;
op.Text = description;
tableLayoutPanel1.Controls.Add(op, 1, k);
}
}
I tried to add labels and rows to tablelayout. When i used adding to listview like that:
ListViewItem ite = new ListViewItem(tag);
ite.Text = (tag + " " + description + war);
listView2.Items.Add(ite.Text);
it work,but when i try another it doesn't work. Why? There aren't any errors or exceptions.
foreach (DataElement elementy in sq)
{
for (int k = 0; k == row_number; k = k + 1)
{
tag = elementy.Tag.ToString();
description = elementy.VR.Tag.GetDictionaryEntry().Description;
// ListViewItem ite = new ListViewItem(tag);
//ite.Text = (tag + " " + description + war);
//listView2.Items.Add(ite.Text);
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Label et_tag = new Label();
et_tag.AutoSize= true;
et_tag.Text = tag;
tableLayoutPanel1.Controls.Add(et_tag, 0, k);
Label op = new Label();
op.AutoSize = true;
op.Text = description;
tableLayoutPanel1.Controls.Add(op, 1, k);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非
row_number
为 0,否则内部for
循环不会运行。否则,它将在 K 初始化后立即满足其条件,因此不执行任何操作。您的循环几乎肯定没有运行。如果 row_number 实际上为 0,则循环将为外部foreach
循环中的每个项目运行一次。您希望循环在什么条件下运行?你的意思是
k < row_number 来代替?您是否只想对其中
k == row_number
的单个 k 执行此操作,在这种情况下,您应该简单地将 row_number 分配给 k 并完全摆脱循环?我不知道row_number从哪里来,所以我不知道这段代码想要做什么。Your inner
for
loop won't run unlessrow_number
is 0. Otherwise, it will fail its condition immediately after K is initialized and, as a consequence, do nothing. Your loop almost certainly isn't running. If row_number actually is 0, your loop will run exactly once for every item in your outerforeach
loop.What condition do you want the loop to run on? Do you mean
k < row_number
instead? Do you just want to do this for a single k wherek == row_number
, in which case you should simply assign row_number to k and get rid of the loop entirely? I don't know where row_number comes from, so I don't know what this code wants to do.不确定你如何初始化 row_number,但也许应该是这样的:
Not sure how you're initializing row_number, but perhaps it should be something like: