如何在运行时删除动态按钮? [英] How to delete dynamic buttons in run time?

查看:99
本文介绍了如何在运行时删除动态按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#中的visual studio在运行时使用以下代码创建了动态按钮。现在我想删除我在运行时创建的一些按钮。我可以这样做吗



我尝试了什么:



按钮dynamicButton = new Button();

dynamicButton.Location = new Point(300 + tx4,200 + tx3);

dynamicButton.Height = nh;

dynamicButton.Width = nw;

dynamicButton.BackColor = Color.Gray;

dynamicButton.ForeColor = Color.Blue;

dynamicButton.Click + = new EventHandler(DynamicButton_Click);

Controls.Add(dynamicButton);

i created dynamic buttons using following code in run times using visual studio in c#. now i want to deleting some buttons that i created in run time.how can i do that

What I have tried:

Button dynamicButton = new Button();
dynamicButton.Location = new Point(300 + tx4, 200 + tx3);
dynamicButton.Height = nh;
dynamicButton.Width = nw;
dynamicButton.BackColor = Color.Gray;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Click += new EventHandler(DynamicButton_Click);
Controls.Add(dynamicButton);

推荐答案

你的示例代码显示的是动态创建一个Button。

创建完成后,Button被添加到它的Host(可能是Form)的Controls-Collection中。

如果你想删除一个Control,你只需要从sa中删除它me Controls-Collection(Controls.Remove)。

这里的问题可能是确定正确的删除控件。为此,为每个Control赋予一个名称可能会有所帮助 - Visual-Studio-Designer通常会为您执行此操作。如果您创建一个Control动态,您应该通过代码来篡改...
What your sample-code is showing is the dynamic creation of a Button.
After this creation the Button is added to the Controls-Collection of it's Host (perhaps the Form).
If you want to delete a Control you only need to remove it from the same Controls-Collection (Controls.Remove).
The Problem here perhaps is to identify the right Control for removing. For this it could be helpful to give each Control a Name - the Visual-Studio-Designer normally does this for you. If you create a Control dynamic you should mame it by your code ...


我建​​议您阅读MSDN文档:如何:在运行时添加或删除控件集合 [ ^ ]
I'd suggest to read MSDN documentation: How to: Add to or Remove from a Collection of Controls at Run Time[^]


这篇关于如何在运行时删除动态按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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