快速添加和删除控件 [英] Adding and removing controls on the fly

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

问题描述

我在Google上做了一些搜索,设法找到了一些有关如何从正在运行的应用程序中添加控件的文章,但似乎都没有以适合我需要的方式对其进行解释.我想知道是否有人可以帮助我了解如何解决这个问题,或者将我定向到可以做到这一点的资源.

我正在编写一个食谱书应用程序,并且在添加/编辑食谱的过程中,我希望能够让用户从列表中添加或删除配料.显然,无法知道任何给定食谱中将包含多少成分,因此我无法事先添加那么多文本框.

我希望有一个文本框,在其右侧有一个+/-按钮,就像您在许多网站上看到的那样,这样,当用户单击+时,它将在其下方创建一个新控件,如果他们单击-,则它将删除该控件.另外,尽管主题可能略有不同,但我也想知道如何让用户重新排列文本框,以便它们在打印输出上相应显示.

I''ve googled a bit and managed to find a few articles on how to add controls from within a running application, but neither seem to explain it in a way that suits my needs. I wonder if anybody can help me understand how I can go about this, or direct me to a resource that will do the same.

I''m writing a recipe book application, and as part of the process of adding/editing a recipe, I want to be able to let the user add or remove ingredients from a list. Obviously, there''s no way to know how many ingredients will be in any given recipe, so I cannot add that many textboxes beforehand.

I want to have a textbox with a +/- button to the right of it like you see on many websites, so that when the user clicks the +, it will make a new control below it, and if they click the -, then it will delete that control. Also, while it may be a slightly different topic, I''d also like to know how I can let the user re-arrange the textboxes so they display accordingly on a printout.

推荐答案

处理添加和删除按钮的Click事件:
Handle the Click event for the add and remove buttons:
private Button runTimeButton = null;
private void butAdd_Click(object sender, EventArgs e)
    {
    Button but = sender as Button;
    if (but != null)
        {
        if (runTimeButton == null)
            {
            runTimeButton = new Button();
            runTimeButton.Text = "Added!";
            runTimeButton.Location = new Point(but.Location.X, but.Location.Y + 30);
            runTimeButton.Click += new EventHandler(runTimeButton_Click);
            Controls.Add(runTimeButton);
            }
        }
    }

void runTimeButton_Click(object sender, EventArgs e)
    {
    MessageBox.Show("Added Button Clicked!");
    }

private void butRemove_Click(object sender, EventArgs e)
    {
    if (runTimeButton != null)
        {
        Controls.Remove(runTimeButton);
        runTimeButton.Click -= runTimeButton_Click;
        runTimeButton = null;
        }
    }


但是,如果您正在查看向用户添加大量信息的用户,则可能要考虑使用DataGridView -它是为用户添加和删除行而设计的.

在打印时,请勿让用户移动任何东西!
不要尝试打印实际的表格-看起来会很恐怖.而是使用 ^ ]处理您的打印输出-它使您可以完全控制打印内容和打印位置,包括字体,大小,颜色等,而这些内容不必以任何方式与屏幕相关联显示.


However, if you are looking at the user adding lots of information, you might want to consider using a DataGridView instead - it is designed for the user to add and remove rows.

When it comes to printing, don''t let users move anything!
Don''t try to print the actual form - it will look horrible. Instead, use the PrintDocument class[^] to handle your print outs - it gives you full control over what and where things are printed, including fonts, sizes, colours, etc., which do not have to be related in any way to the screen display.


关于打印问题位置,我想我可能会更清楚.

我已将其设置为将配方数据存储在XML文档中,并且正在使用XSL和CSS,以便如果用户选择,该文档将显示在普通的Web浏览器中.它还使用相同的技术从我的winform中显示配方.我希望用户能够以逻辑上合理的顺序订购列表中的配料.该网页将被打印.

不过,我将研究datagridview控件.听起来可能是最好的选择.
Regarding the printing question, I guess I could have been more clear.

I have it setup to store the recipe data in an XML document, and am using XSL and CSS so that the document will display in a normal web browser if the user chooses. It also uses this same technology to display the recipe from within my winform. I want the user to be able to order the ingredients in the list in whatever order makes logical sense. The webpage will be what gets printed.

I''ll look into the datagridview control, though. Sounds like it may be the best option.


这篇关于快速添加和删除控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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