向面板动态添加控件 [英] to add controls dynamically to a panel

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

问题描述

大家好,

我根据要求将动态控件添加到面板,即
根据计数(可以添加的次数),相同的过程可以重复并再次添加相同的控件.
但是,它只能看到最后添加的控件,而不是为先前的控件创建时占用的空间.无法看到其他控件.
那么如何使每个添加的控件可见?
在此先谢谢您.

Hi all,

I am adding a control dynamically to a panel depending on the requirement i.e
the same process can repeat and add the same controls again depending on the count(no of times to be added).
But the thing is am able to see only the last added control instead the space is taken when created for the previous controls. Am not able to see the other controls.
So how can i make each added control visible?
Thanks in advance.

推荐答案

我通常建议一种非常通用的解决此类问题的方法.如果您不知道如何放置一些控件,创建一些布局等,请尝试首先使用设计器.测试应用程序以查看结果.如果这是您想要的,请找到自动生成的代码并了解其工作方式.您将需要使用完全相同的技术.仅不要使用变量的丑陋命名方式以及设计人员通常使用的方法-这样的名称违反了(好的)Microsoft命名约定.给所有语义上合理的名称,不要使用数字字符和其他自动"污垢.

祝你好运,
—SA
I usually advise a very general method of solving such problems. If you don''t know how to put some control, create some layout, etc., try to do it using the designer first. Test the application to see the results. If this is what you want, locate the auto-generated code and learn how it works. You will need to use exact same techniques. Only don''t use ugly naming of the variables and method designers usually use — such names violate (good) Microsoft naming conventions. Give everything semantically sensible names, without numeric characters and other "automated" dirt.

Good luck,
—SA


如果您使用的是ASP.NET,则以下答案将适用.因此,它没有用.

仅能看到最后添加的控件,而为先前的控件创建时会占用空间.无法看到其他控件.
那么如何使每个添加的控件可见

在每个面板之后添加一个换行符,以将下一个换行.

添加方式之一:
If you were working on ASP.NET then the following answer would have applied. As such, it''s of no use.

am able to see only the last added control instead the space is taken when created for the previous controls. Am not able to see the other controls.
So how can i make each added control visible

Add a line break after every panel to push the next one in a new line.

One of the ways to add it:
myPanel.Controls.Add(new LiteralControl("<br />"));




更新:
正如Dave所指出的那样,对于Winforms,您需要提供Left&控件的top属性.像这样的东西:




UPDATE:
As Dave has already pointed out, for Winforms, you need to provide the Left & Top property of the control. Something like:

TextBox tb1 = new TextBox();

// set up the left and top position (the same as changing the location in the IDE)
tb1.Left = 100;
tb1.Top = 100;

// put any default text if you want
tb1.Text = "Hello World!"

// add the newly created control to the form
this.Controls.Add(tb1);


而没有看到用于创建这些代码的代码控件处于循环中,我们只能猜测.

很有可能您没有设置控件的顶部"和左侧"(或者位置")属性,因此它们都位于0,0之上,或者您将每个控件都设置在相同的位置.无论哪种情况,您都只会看到最后添加的控件.

您必须为每个控件的顶部"和/或左"属性添加一个适当的偏移量,以将其移离最后添加的控件.确切地说,您在这里所做的取决于您的要求.
Without seeing the code you''re using for creating these controls in a loop, we can only guess.

Chances are good you''re not setting the Top and Left (alternatively the Location) properties of the controls, so they are all sitting on top of each other at 0,0, or, you are setting every control to the same location. In either case, you will only see the last control added.

You have to add an appropriate offset to the Top and/or Left properties of each control in order to move them off the last control that was added. Exactly what you do here is dependent on your requirements.


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

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