无法将自定义控件点击事件发送到主机表单 [英] Cant ripple a custom control click event up to the host form

查看:56
本文介绍了无法将自定义控件点击事件发送到主机表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个AForge PictureBox和一个ListBox创建一个自定义控件(称为视口)。这三个控件填充UserControl。



我希望主机表单上的事件处理程序响应鼠标单击任何视口(每个网络摄像头1个)在计算机上,最多六个)在运行时添加到主机表单。



我已经阅读了几天的论坛,并尝试了不同的方式结束。我得到的最接近的如下所示。我在vpClick对象上得到一个null对象异常。

I am creating a custom control (called a viewport) with two AForge PictureBox’s and one ListBox. The three controls fill the UserControl.

I want an event handler on the host form to respond to a mouse click on any of the viewports (1 per each web cam attached to the computer, up to six) added to the host form at runtime.

I have been reading forums for several days and tried no end of different ways. The closest I have got is shown below. I get a null object exception on the vpClick object.

vpClick(103, this.Name);





我尝试了什么:



在自定义控件中



What I have tried:

In the Custom Control

<pre>using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlowerControls
{
    // Add delegate for all control click events
    public delegate void VPClick(int Code, string Title);

    public partial class ViewPort: UserControl
    {
        // Define event from delegate
        public event VPClick vpClick;



:::::


:::::

<pre>        private void PictureFlower_Click(object sender, EventArgs e)
        {
            vpClick(101, this.Name);
        }

        private void ListBoxViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            vpClick(102, this.Name);
        }

        private void KeyIcon_Click(object sender, EventArgs e)
        {
            vpClick(103, this.Name);
        }







在房东形式




In The host form

// Declare a delegate
public delegate void ViewPort_click(object sender, MouseEventArgs e);





::::



::::

FlowerControls.ViewPort VP01;
int a = 1;
while (a < 7)
{
    VP01 = new FlowerControls.ViewPort();
    //VP01.ContextMenuStrip = contextMenuStripViewPort;
    VP01.Height = viewPortHeight;
    VP01.Width = viewPortHeight;



    //VP01.SizeMode = PictureBoxSizeMode.Zoom;
    VP01.BloomPhoto = Properties.Resources.testpattern; VP01.BorderStyle = BorderStyle.FixedSingle;

    VP01.Location = new Point(I * viewPortHeight, 0);
    I += 1;
    viewports.Add(a, VP01);
    splitContainer2.Panel1.Controls.Add(viewports[a]);

    //viewports[a].Click += new ViewPort_click(object sender, MouseEventArgs e);
    a++;
}





::::



::::

public void ViewPort_click(object sender, MouseEventArgs e)
{
    //do stuff here
    MessageBox.Show("Click");
}

public void vpClick(int Code, string Name) { MessageBox.Show(Name); }

推荐答案

请看这里:在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ] - 它显示了如何将事件传递到父表单但是它与UserControl的代码完全相同(部分原因是Form派生自Control)。
See here: Transferring information between two forms, Part 2: Child to Parent[^] - it shows how to pass an event up to a "parent form" but it's exactly the same code for a UserControl (partly because a Form is derived from Control).


这篇关于无法将自定义控件点击事件发送到主机表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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