如何使用C#在面板内部创建动态控件 [英] How to create dynamic control inside a Panel using c#

查看:170
本文介绍了如何使用C#在面板内部创建动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在面板中有5个控件;
一个是日期,
一个是位置发件人,
一个是位置到和
最后一个是总费用.和一个添加按钮.

以上所有控件均在面板中.
位置来源,位置到这些是下拉菜单,值来自数据库.根据下拉值生成总成本.这三个字段是动态的.

从这些控件中选择值后,单击添加"按钮,所有值都将插入到DB中,并再次在前一个控件下方自动生成5个控件.

现在我的问题是,当面板中只有5个控件时,我可以执行此操作吗?在这种情况下我该怎么办?每次添加按钮都会生成5个控件,并且添加按钮后面的代码将被编写一次;第一次添加按钮可将值插入DB,但是从下一次起它将同时用作删除按钮以及删除控件.

希望我能正确解释我的观点.如有任何疑问,请随时提出.
您也可以在此处进行检查:在线预订 [

In the panel there are 5 controls;
one is Date,
one is Location From,
one is Location To and
the last one is Total Cost. and one add button.

All the above controls are in a panel.
Location From, Location To these are drop-down menu and the values are come from DB. Total cost is generated as per the Drop-down value. These 3 fields are dynamic.

After selecting value from these control when add button is clicked all the values are inserted into DB and again the 5 controls are generated automatically below the previous control.

Now my question is i can do this when there is only 5 controls in the panel; how should i do in this case? the add button generate the 5 controls every time and the code behind the add button is written once;the 1st time add button works to insert value into DB but from the next time it will work as remove button as well as remove the controls also.

hope i can explain my view properly. if any confusion don''t hesitate to ask.
you can also check this over here: Online Booking[^]

推荐答案

我要做的第一件事就是设计它应该做的看起来像所有信息都已填写完毕.执行此操作时,您可能希望将希望同时填写的控件分组在一起.

我不知道您使用的是WinForms,WPF,ASP.NET还是什么,但是我可以想到三种通用方法来获取所需的动态控件填充.

1.创建一个托管要一起显示的控件的控件.然后在运行时创建并将其插入到UI容器中.

2.创建一个控件,以承载要一起显示的控件或将它们承载在同一容器中,然后将该控件或容器放置在UI中.在设计器中将其收起(控件/容器),并在适当的时候使它在代码中再次可见.

3.使用具有可折叠行或列的表或网格,当折叠时,它们也将隐藏行或列的内容.如果这是WPF,我将使用Grid控件,并命名行/列定义或绑定行/列定义的大小.当需要显示控件时,使行/列对用户可见.

方法1将按照您的要求动态创建控件.方法2和3会让您看起来像是动态创建的控件,即使它们始终存在.
The first thing I would do is design what it should look like once all the information has been filled in. When doing this, you might want to group controls that are expected to be filled in at the same time together.

I don''t know if you are using WinForms, WPF, ASP.NET or what, but I can think of three general ways get the dynamic control fill-in that you want.

1. Create a control that hosts the controls you want to show up together. Then create and insert it into the UI container at run time.

2. Create a control that hosts the controls you want to show up together or host them in the same container, place the control or container in the UI. Collapse it (the control/container) it in the designer, and make it visible again when in code once the time is right.

3. Use a table or grid with collapsible rows or columns, that will also hide the contents of the rows or columns when collapsed. If this were WPF, I would use a Grid control, and I would either name the row/column definitions or bind the sizes of the row/column definitions. When it''s time to show the controls, make the row/column visible to the user.

Method 1 will create controls dynamically, like you asked. Methods 2 and 3 will just make it look like you created the controls dynamically even though they''re always there.


这篇关于如何使用C#在面板内部创建动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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