首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Access FlowLayoutPanels‘controls’方法

Access FlowLayoutPanels‘controls’方法
EN

Stack Overflow用户
提问于 2020-03-12 08:48:19
回答 1查看 37关注 0票数 0

假设我有一个名为FlowLayoutPanelpanel1,其中包含control1control2control3。每个控件都有一个方法.doSomething()或存储的变量.name。我怎样才能访问这些?

类似于:

代码语言:javascript
复制
panel1.Controls[0].doSomething();
string name = panel1.Controls[0].name;

我的代码:

代码语言:javascript
复制
public partial class ChatItem : UserControl
    {
        public string username, description, mainUser; /// <summary>
        /// mainUser este utilizatorul care foloseste in prezent aplicatia
        /// </summary>
        MySqlConnection basicConnection, chatUpdater, msgSender;

        #region Properties

        [Category("Custom Properties")]
        public string user
        {
            get { return label1.Text; }
            set { label1.Text = username = value; }
        }

        [Category("Custom Properties")]
        public Image profilePicture
        {
            get { return pictureBox1.Image; }
            set { pictureBox1.Image = value; }
        }

        public Image dot
        {
            get { return pictureBox2.Image; }
            set { pictureBox2.Image = value; }
        }

        #endregion

        public void doSomething()
        {
            MessageBox.Show("something");
        }

    }

我想访问.doSomething()description.How,我能这样做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 10:00:25

您应该在控件属性上迭代,但是控件应该转换为您的类型,以使用特定于您的类的字段/方法。

代码语言:javascript
复制
foreach(var control in panel1.Controls) 
{
   if (control is ChatItem chatItem) // to ensure the casting can be done
   { 
       string description= chatItem.description;
       chatItem.doSomething();
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60650441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档