动态添加控件以形成C# [英] Dynamically add controls to form C#

查看:49
本文介绍了动态添加控件以形成C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用windows应用程序用c#

我想在表格上动态添加1个以上的标签。

让我一步一步解释:

1)我有一些textBoxes,1个Form上的按钮。

2)当我点击文本框内的按钮文本时,应该在Label中显示。

3)假设我有5个文本框,按钮点击标签应该通过一行代码显示

4)在下一个按钮上单击下一行应该生成。前一行不应该被删除。每个按钮点击该标签的行应该增加。



怎么做。?



我尝试了什么:



这是我用于生成标签按钮上的按钮动态点击:

标签l1 = 标签(); 
l1.Text = t1.Text;
l1.AutoSize = true ;
Controls.Add(l1);
l1.Location = new Point( 139 270 );

标签l2 = 标签();
l2.Text = t2.Text;
Controls.Add(l2);
l2.Location = new Point( 251 270 );


标签l3 = 标签();
l3.Text = t3.Text;
l3.AutoSize = true ;

Controls.Add(Date);
l3.Location = new Point( 337 270 );

标签l4 = 标签();
l4.Text = t4.Text;

Controls.Add(l4);
l4.Location = new Point( 412 270 );

解决方案

如果我理解正确,你想要在每个Button-Click增量,以编程方式重新生成5个标签表示TextBoxes的实际输入,同时保留前一组5个标签,用于调用'row'



[FIRST]

蓝绿色黄色黑色

[第二个]

...

白色紫色橙色深蓝色< br /> 
蓝色绿色黄色黑色





我显示你在VB.NET中的一个例子

  Dim  ROW  As  整数 =  0  
私有 Sub Button_Refresh_Click(发件人作为 对象,e As EventArgs)
ROW + = 1

Dim Labels_Set( 4 作为标签' (5个标签)
对于 i = 1 5
Labels_Set(i-1)= 标签()
Dim lbl As Label = Labels_Set(i-1)
选择 案例 i
案例 1 :lbl.Text = TextBox1.Text
案例 2 :lbl.Text = TextBox2.Text
案例 3 :lbl.T ext = TextBox3.Text
案例 4 :lbl.Text = TextBox4.Text
案例 5 :lbl.Text = TextBox5.Text
结束 选择

.Controls.Add (lbl)
lbl.Name = LABEL& CStr (ROW)& _& CStr(i)' LABEL * _ *'
lbl.Location = 点( 50 +(i * lbl.Width), 50 +(ROW * lbl.Height))
下一步

结束 Sub





此代码删除特定行的任何标签集

 私有  Sub  RemoveSet( ByVal  row < span class =code-keyword>作为 整数
对于 < span class =code-keyword>每个 ctl .Controls()
如果 TypeOf (ctl) 标签然后
如果 ctl.Name.Split( CChar _))( 0 )= LABEL& CStr (行)然后
.Controls.Remove(ctl)
' ctl.Dispose
< span class =code-keyword>结束 如果
结束 如果
下一步
结束 Sub



我还没有在IDE上检查过这个。

希望帮助


HELLO,i am using windows appliation with c#
I want to add more than 1 Label on Form Dynamically.
let me explain this step by step:
1)i have some textBoxes,1 Button on Form.
2)when i click on button text inside textboxes should be display in Label.
3)Suppose i have 5 textboxes, on button click labels should be display through code in one row
4)on the next button click Next row should be generated.and previous row should not be deleted.on every button click that label's row should get increase.

how to do that.?

What I have tried:

this is my ode for geenrating Labels on button Click dynamically:

Label l1= new Label();
l1.Text = t1.Text;
l1.AutoSize = true;
Controls.Add(l1);
l1.Location = new Point(139, 270);

Label l2= new Label();
l2.Text = t2.Text;
Controls.Add(l2);
l2.Location = new Point(251, 270);


Label l3= new Label();
l3.Text = t3.Text;
l3.AutoSize = true;

Controls.Add(Date);
l3.Location = new Point(337, 270);

Label l4= new Label();
l4.Text = t4.Text;

Controls.Add(l4);
l4.Location = new Point(412, 270);

解决方案

If i understood you correctly, you want at each Button-Click increment, to regenerate 5 Labels programatically that will represent the actual inputs of the TextBoxes, while preserving the previous set of 5 Labels, that you use to call a 'row'

[FIRST]
BLUE GREEN YELLOW RED BLACK
[SECOND]
...

WHITE PURPLE ORANGE GRAY DARKBLUE<br />
BLUE GREEN YELLOW RED BLACK



Im showing you an example in VB.NET

Dim ROW As Integer = 0
Private Sub Button_Refresh_Click(sender As Object, e As EventArgs)
  ROW+=1

  Dim Labels_Set(4) As Label '(5 Labels)
  For i = 1 To 5
     Labels_Set(i-1) = New Label()
     Dim lbl As Label = Labels_Set(i-1)
     Select Case i
        Case 1 : lbl.Text = TextBox1.Text
        Case 2 : lbl.Text = TextBox2.Text
        Case 3 : lbl.Text = TextBox3.Text
        Case 4 : lbl.Text = TextBox4.Text
        Case 5 : lbl.Text = TextBox5.Text
     End Select
  
     Me.Controls.Add(lbl)
     lbl.Name = "LABEL" & CStr(ROW) & "_" &CStr(i) 'LABEL*_*'
     lbl.Location = New Point(50 + (i * lbl.Width), 50 + (ROW * lbl.Height))
  Next

End Sub



And this code removes any set of Labels of specific row

Private Sub RemoveSet(ByVal row As Integer) 
    For Each ctl in Me.Controls()
        If TypeOf(ctl) Is Label Then
            If ctl.Name.Split(CChar("_"))(0) = "LABEL" & CStr(row) Then
               Me.Controls.Remove(ctl)
               'ctl.Dispose
            End If   
        End If
    Next
End Sub


I have not checked this on the IDE.
Hope that helps.


这篇关于动态添加控件以形成C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆