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() 特定的用户控件?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01