C#代码删除控件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#代码删除控件相关的知识,希望对你有一定的参考价值。
我用了两个button,点击就添加控件,另一个点击就删除所有刚刚添加的控件,可以添加,但却删不了,怎么办啊
代码为:
public int y = 0;
// 添加控件
private void button1_Click(object sender, EventArgs e)
ComboBox combo = new ComboBox() ;
combo.Name = "cbl" + y;
panel1.Controls.Add(combo);
y=y+1;
// 删除控件
private void button2_Click(object sender, EventArgs e)
??????
请帮忙看看
我知道用这个方法.
panel1.Controls.Remove(???);
但是因为里面的名字是我加尾数命名的,而且程序运行过程中,根本无法知道到底加了多少个组合框进去,
删除的时候,名字根本写不了,怎么办?我是这样写的
try
for (int i = 0; i < 100; i++)
panel1.Controls.Remove("cbl" + i);
finally
y = 0;
两个错误:
1,与"System.Windows.Forms.Contol.ControlCollection.Remove(System.Windows.Forms.Contol)"最匹配的重载方法具有一些无效参数.
2,参数"1":无法从"string"转换为"System.Windows.Forms.Contol"
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144183
ComponentArt.Charting.WebChart.dll
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144386
ComponentArt.Web.UI 2007.2源代码+实例+DLL
http://bbs.topsage.com/dispbbs_123_161434_1.html
ComponentArt.WebUI.2007.1破解DLL
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143055
ComponentArt.WebUI.2007.1源代码
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143224
ComponentArt.Web.UI.2006.2
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143212
ComponentArt.Web.UI.2006.2源代码
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143219
ComponentArt.Web.UI.2006.1
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=142983
Infragistics NetAdvantage for ASP.NET 2007 Vol 2
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159861
Infragistics NetAdvantage AppStylist 2007 Vol 2
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159859
Infragistics TestAdvantage WinForms 2007 For CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159870
Infragistics TestAdvantage WinForms 2007 for CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159868
Infragistics NetAdvantage for Windows Forms 2007 Vol 2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159863
Infragistics NetAdvantage 2007 for WPF
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159857
Infragistics NetAdvantage 2006 Vol2 CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159814
Infragistics NetAdvantage 2006 Vol2 for CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159854
Infragistics NetAdvantage 2006 Vol2 CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159814
ComponentOne Studio 2007 v1.5 for ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159753
ComponentOne Studio 2007 v1.5 for ASP.NET 1.x
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159745
ComponentOne Studio 2006 v2 for ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159739
ComponentOne Studio 2006 v2 for ASP.NET 1.x
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159734
ComponentOne Studio for Mobile Devices 2007 v1.5 CLR1x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159812
ComponentOne Studio for Mobile Devices 2006 v2 CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159811
ComponentOne Studio for Mobile Devices 2006 v2 CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159807
ComponentOne Studio 2007 v1.5 for .NET CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159802
ComponentOne Studio 2007 v1.5 for .NET CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159798
ComponentOne Studio for .NET 2006 v2 CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159780
ComponentOne Studio for .NET 2006 v2 CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159777
ComponentOne Studio for ActiveX 2007 v1.5
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159727
ComponentOne Studio for ActiveX 2006 v2 CLR2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159725
ComponentOne Studio for ActiveX 2006 v2 CLR1.x
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159724
Telerik RadWindow for ASP.NET 2.0 v1.8.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156136
Telerik RadUpload for ASP.NET 2.0 v2.3.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156135
Telerik RadTreeView for ASP.NET 2.0 v6.2.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156134
Telerik RadTabStrip for ASP.NET 2.0 v3.5.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156132
Telerik RadToolbar for ASP.NET 2.0 v1.5.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156133
Telerik RadSplitter for ASP.NET 2.0 v1.2.2.1
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156131
Telerik RadSpell for ASP.NET 2.0 v3.1.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156130
Telerik RadRotator for ASP.NET 2.0 v2.6.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156129
Telerik RadPanelbar for ASP.NET 2.0 v4.2.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156128
Telerik RadMenu for ASP.NET 2.0 v4.2.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156127
Telerik RadInput for ASP.NET 2.0 v2.0.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156126
Telerik RadGrid for ASP.NET 2.0 v4.6.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156125
Telerik RadEditor for ASP.NET 2.0 v7.1.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156124
Telerik RadComboBox for ASP.NET 2.0 v2.7.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156122
Telerik RadDock for ASP.NET 2.0 v1.8.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156123
Telerik RadChart for ASP.NET 2.0 v3.2.1.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156121
Telerik RadCalendar for ASP.NET 2.0 v2.1.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156120
Telerik RadAjax for ASP.NET 2.0 v1.7.2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=156119
telerik r.a.d.upload
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143044
telerik r.a.d.window
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=142980
telerik r.a.d ToolBar
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143077
telerik r.a.d.Chart
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143075
telerik r.a.d.combobox
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143048
DotNetBar for VS2005 6.8.0.1
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=144521
DotnetCharting 4.3破解DLL
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=144388
DotNET Charting WebForms
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144461
dotnetCharting.WinForms
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=144455
TeeChart for .NET 3.2.2763.26084 完美DLL
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=165932
TeeChart for .NET 3.2.2699.17379 完美DLL
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=165929
Godiagram Web 2.6.2 for .NET 2.0
http://bbs.topsage.com/dispbbs_123_177906_1.html
Godiagram Win 2.6.2 for .NET 2.0
http://bbs.topsage.com/dispbbs_121_177907_1.html
DevExpress 7.3.4 完美破解DLL
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=165852
Dxperience 7.3.5 完美破解DLL
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=165851
DevExpress.LocalizationCHS.Dll
http://bbs.topsage.com/dispbbs_121_169983_1.html
NickLee.Web.UI
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=143052
SolpartWebControls
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=142981
AspNetPager 6.0 for ASP.NET 1.x 自定义分页控件
http://bbs.topsage.com/dispbbs_123_161692_1.html
AspNetPager 6.0 for ASP.NET 2.0 自定义分页控件
http://bbs.topsage.com/dispbbs_123_161696_1.html
数据操作类 Socut.Data.dll for .NET 2.0 v3.1
http://bbs.topsage.com/dispbbs_123_161867_1.html
数据操作类 Socut.Data.dll for .NET 1.x v3.1
http://bbs.topsage.com/dispbbs_123_161866_1.html
Developer Express for .NET v7.3.5.0全套完美无限制版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=166512 参考技术A 把你的panel1.Controls.Remove("cbl" + i);
改成
string name = "cbl" + i.ToString();
panel1.Controls.RemoveByKey(name);
这样就可以了!本回答被提问者采纳 参考技术B 1.思路说明:遍历所有控件后,查询需要操作的,然后进行删除;
参考代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
public partial class Form1 : Form
public Form1()
InitializeComponent();
Queue<Control> query = new Queue<Control>();//临时存储获取到的control控件
/// <summary>
/// 递归获取panel1上的所有控件,并临时存储到一个队列中
/// </summary>
/// <param name="item"></param>
void DS(Control item)
for (int i = 0; i < item.Controls.Count;i++ )
if (item.Controls[i].HasChildren)
DS(item.Controls[i]);
else
query.Enqueue(item.Controls[i]);
private void button1_Click(object sender, EventArgs e)
DS(panel1);
//删除遍历到的控件
while (query.Count != 0)
query.Dequeue().Dispose();
参考技术C Remove()用这个方法
ComboBox combo = new ComboBox() ;
combo.Name = "cbl" + y;
panel1.Controls.Add(combo);
panel1.Controls.Remove(combo)
Remove这个方法参数是控件的实例,而不名字
比如,combo.Name = "cbl001";
Remove("cbl001")错
Remove(cbl001)对
WPF (C#) 用户控件删除元素
【中文标题】WPF (C#) 用户控件删除元素【英文标题】:WPF (C#) Usercontrol remove elements 【发布时间】:2021-11-26 22:03:18 【问题描述】:我正在使用 WPF(C#) 中的 Usercontrol 制作模板。
但是,在应用此用户控件时,是否可以减去特定部分?例如,移除一个按钮?
【问题讨论】:
隐藏多余的部分 我怎样才能让它隐藏,我怎样才能让它发生? 【参考方案1】:要从 UserControl 中减去特定部分,可以使用可见性(折叠、隐藏)选项。 确保在 UserControl 中添加依赖属性以显示和隐藏特定部分。
【讨论】:
我不认为向用户控件添加依赖属性来控制可见性是一个好建议。 WPF 是围绕 MVVM 构建的,MVVM 解决方案是通过绑定到视图模型来控制可见性。 @NigelBess 我不同意你的观点,用户控件必须公开必要的功能才能与之交互,.. 必要的功能已经通过绑定暴露了。无需通过为您想要控制的所有内容添加依赖属性来使事情复杂化。此外,您仍然需要访问依赖属性,无论哪种方式都应该通过绑定来完成【参考方案2】:听起来您只是想隐藏现有按钮,您应该通过将Visibility
设置为Visibility.Collapsed
或Visibility.Hidden
来做到这一点。这应该通过绑定到用户控件的 ViewModel 来完成。
如果您需要纯 XAML 解决方案:不,这样是不可能的。但是,反过来也是可能的:you can add content to a user control,它有效地提供了相同的功能。
您可以做的是制作一个不包含按钮的基本用户控件,而是有一个内容呈现器。第二个用户控件可以包装基本用户控件并将一个按钮定义为其内容。然后,当您不想将用户控件与按钮一起使用时,您可以简单地创建一个基本用户控件的实例。
【讨论】:
以上是关于C#代码删除控件的主要内容,如果未能解决你的问题,请参考以下文章
vs写c#时,自动根据控件生成的代码不小心被删除了,窗口设计还在,如何重新自动生成?
在 Form.cs 中生成自定义 C# 代码,同时在设计时在 Form1.cs 中删除用户控件
C# winform中dataGridView控件如何插入、删除、更新记录,操作完成后怎样写回数并保存据库,怎么写代码?