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() 特定的用户控件?
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
