c ++ \ cli访问面板中的所有按钮 [英] c++\cli accessing all the Buttons in a panel

查看:95
本文介绍了c ++ \ cli访问面板中的所有按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为每个循环编写一个代码,如下面的代码

I'm trying to write a for each loop like the below code

private: System::Void MenuButtons (System::Object^  sender, System::EventArgs^  e)
	{
		for each (Button^ B in PNL_SideMenu->Controls)
			{
			if( B == ((Button^)sender) )
				B->BackColor = System::Drawing::Color::SlateGray;
			else
				B->BackColor = System::Drawing::Color::Transparent;
			}
			
	}

但面板上还有一个标签,我可以将标签放在另一个面板中,但我想知道如何直接访问面板中的按钮。

But there is also a label in the panel, i can put the label in another panel but i wanna know how i can directly access the buttons in the panel.

推荐答案

试试这个:

for each ( Control ^ c in PNL_SideMenu->Controls )
{
   bool is_button = dynamic_cast<Button^>( c ) != nullptr;

   if( is_button )
      c->BackColor = Color::SlateGray;
   else
      c->BackColor = Color::Transparent;
}



为了只获得按钮,忽略其他控件,你可以这样做:


In order to get the buttons only, ignoring other controls, you can do this:

for each ( Button ^ b in Enumerable::OfType<Button^>( PNL_SideMenu->Controls ) )
{
   b->BackColor = Color::SlateGray;
}




这需要引用System.Linq或System.Core。


This requires a reference to System.Linq or System.Core.


这篇关于c ++ \ cli访问面板中的所有按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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