Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C UserControl-单击事件时获取UserControl中每个控件的属性值_C#_Winforms_User Controls - Fatal编程技术网

C# C UserControl-单击事件时获取UserControl中每个控件的属性值

C# C UserControl-单击事件时获取UserControl中每个控件的属性值,c#,winforms,user-controls,C#,Winforms,User Controls,我有一个自定义UserControl,它的名称是TP1CustomListView: 我使用了一个有两列的TableLayoutPanel,在第一列上保存图片,在第二列上保存另一个TableLayoutPanel。第二列中的TableLayoutPanel有3行可容纳3个文本框 我在UserControl中为PictureBox编写了一个单击事件,如下所示: //events public new event EventHandler Click {

我有一个自定义UserControl,它的名称是TP1CustomListView:

我使用了一个有两列的TableLayoutPanel,在第一列上保存图片,在第二列上保存另一个TableLayoutPanel。第二列中的TableLayoutPanel有3行可容纳3个文本框

我在UserControl中为PictureBox编写了一个单击事件,如下所示:

 //events
        public new event EventHandler Click
        {
            add { picbox.Click += value; }
            remove { picbox.Click -= value; }
        }
picturebox.Click += picturebox_click;


private void picturebox_click(object sender, EventArgs e)
{
    var handler = this.Click;
    if(handler != null)
    {
        handler(this, e);
    }
}
在Form1中,我有10个用户控件,比如我贴在主题顶部的图片。我已经为Form1中的所有UserControl编写了一个单击事件。我试图在单击时强制转换每个UserControl的发送者,以在该UserControl内获得3个文本框的3值。但是我得到的结果是空的

这是我的点击事件代码:

  public Form1()
        {
            InitializeComponent();
            foreach (Control c in this.Controls)
            {               
                TP1CustomListView ctLV = c as TP1CustomListView;
                if (ctLV != null)
                {
                    ctLV.Click += ctLV_Click;

                }
            }
        } 

  void ctLV_Click(object sender, EventArgs e)
        {

            TP1CustomListView customView = sender as TP1CustomListView;

              if(customView!=null)
              {
                  MessageBox.Show(customView.subtbTitle.Text + customView.subtbContent.Text + customView.subtbFooter.Text);
              }
        }
这是我的TP1CustomListView用户控件中的构造函数和子控件:

 //sub controls
        public PictureBox subPicbox;
        public TextBox subtbTitle;
        public TextBox subtbContent;
        public TextBox subtbFooter;
        public TP1CustomListView()
        {
            InitializeComponent();
            subtbTitle = txtTitle;
            subtbContent = txtContent;
            subtbFooter = txtFooter;
            subPicbox = picbox;
            tableLayoutPanel1.Dock = DockStyle.Fill;
        }
希望大家能给我一些建议或任何解决我的问题。
谢谢大家!

您应该在用户控件内处理PictureBox的click事件,并在发生这种情况时从用户控件引发click事件

在用户控件中,您应该有如下内容:

 //events
        public new event EventHandler Click
        {
            add { picbox.Click += value; }
            remove { picbox.Click -= value; }
        }
picturebox.Click += picturebox_click;


private void picturebox_click(object sender, EventArgs e)
{
    var handler = this.Click;
    if(handler != null)
    {
        handler(this, e);
    }
}

这样,单击picturebox会触发对用户控件的单击,而该单击就是您在表单中实际听到的内容。

我要做的第一件事是在ifcustomView上设置一个断点=null,并查看发送方实际是什么类型。一旦你知道你可能知道下一步该做什么。谢谢你的回复。customView为空,但发件人为Picturebox。如果我将发件人强制转换为PictureBox,则无法获取第二列中3个文本框的属性值。您应该在用户控件内处理PictureBox的单击事件,并在发生这种情况时从用户控件引发单击事件。您能给我举个例子吗。我是用户控制的新手。非常感谢Zohar Peled。在你给我一些建议来解决我的问题之后。我在谷歌上搜索了一下,找到了解决方案。事实上,我一直在寻找你告诉我的东西两个星期了,但似乎没有什么改变。在我将关键字handle click event of control写入UserControl之前,我在Microsoft中找到了该主题,我的问题已经解决。很高兴能提供帮助:-