How to Dispose() a specific User control from panel control in C#?(如何从 C# 中的面板控件 Dispose() 特定的用户控件?)
问题描述
我想从面板处理特定类型的用户控件.现在我正在使用 foreach 循环来处理用户控件.
I want to dispose a specific type of User control from panel. Right now i am using foreach loop to dispose the User control.
foreach (CTRL.box bx in RightPanel.Controls.OfType<CTRL.box>())
{
bx.Dispose();
}
但它不能正常工作.在谷歌中检查时,我找到了以下代码.
But it is not working properly. while checking in google i find the below code.
while(tabControlToClear.Controls.Count > 0)
{
var tabPage = tabControlToClear.Controls[0];
tabControlToClear.Controls.RemoveAt(0);
tabPage.Dispose();
// Clear out events.
foreach (EventHandler subscriber in tabPage.Click.GetInvocationList())
{
tabPage.Click -= subscriber;
}
}
我正在尝试这样做,但对我来说,这是我需要处理的特定用户控件.它们是我的表单中需要的其他用户控件.总的来说,我想从我的表单中处理 box 用户控件.
I am trying to do this, But for me it is a specific User control i need to dispose. they are other User controls which should be required in my form. Overall i want to dispose box User control from my form.
while (RightPanel.Controls.OfType<CTRL.box>().Count() > 0)
{
var panel = RightPanel.Controls.OfType<CTRL.box>()[0];//Here i am getting error "Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<Project_Server.CTRL.box>'"
}
谁能帮我解决这个错误.
Can anyone help me to fix this error.
推荐答案
错误很明显,你不能在 IEnumerable
Error is pretty clear, you cannot apply indexing on IEnumerable
我建议使用 First
或 FirstOrDefault
扩展方法来检索第一个元素并将其删除.
I would suggest use First
or FirstOrDefault
extension method to retrieve first element and delete it.
var panel = RightPanel.Controls.OfType<CTRL.box>().FirstOrDefault();
if(panel != null)
{
//logic
}
如果您想删除所有 CTRL.box
类型的控件,请使用它.
In case, if you would like to remove all controls of type CTRL.box
use this.
List<Control> controls= RightPanel.Controls.OfType<CTRL.box>().ToList();
foreach(Control c in controls)
{
RightPanel.Controls.Remove(c);
c.Dispose();
}
这篇关于如何从 C# 中的面板控件 Dispose() 特定的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 C# 中的面板控件 Dispose() 特定的用户控件?


- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01