一个Form理解加入了一个自定义控件Netchart4,NetChart4里面有一个pictureBox;就这,我就想实现窗口NetChart4和Form的传值,用委托实现的。各位帮忙找找原因,我找了很久了我新建两个窗口试验这样的委托没有问题,可是在这个程序中就是不行,求解!
首先在NetChart4中我建立委托事件:
namespace Demo.MyControler
{
    public partial class NetChart4 : UserControl
    {
        public NetChart4()
        {
            InitializeComponent();
           
        }
        public delegate void ShowSelectedPoint(string str);
        public event ShowSelectedPoint SelectedPoint;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (SelectedPoint != null)
                SelectedPoint("500");
        }
     }
}
然后我在Form1中绑定方法,监听委托:
namespace Demo
{
    public partial class Form1 : Form
    {
        NetChart4 netChart;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
  
            netChart = new NetChart4();
            netChart.SelectedPoint+=new NetChart4.ShowSelectedPoint(change);
        }
        public void change(string str)
        {
            int a = 0;
            int.TryParse(str, out a);
            this.Location = new System.Drawing.Point(a, 600);
            this.Refresh();
        } 
}或者我尝试与另一个窗口DeveiceList传值也是不行,比如,我在DeveiceList中绑定方法如下:
namespace Demo.MyControler
{
   
    public partial class DeviceList : UserControl
    {
       NetChart4 netChart4;
        public DeviceList()
        {
            InitializeComponent();
            netChart4 = new NetChart4();     
            netChart4.SelectedPoint+=new NetChart4.ShowSelectedPoint(selecedjpg);
            
        }
        private void DeviceList_Load(object sender, EventArgs e)
        {
            netChart4 = new NetChart4();
            //监听事件
            netChart4.SelectedPoint += new NetChart4.ShowSelectedPoint(change);
            
        }
        public void change(string str)
        {
            int a = 0;
            int.TryParse(str, out a);
            this.Location = new System.Drawing.Point(a, 600);
            this.Refresh();
        } 
}
对此困扰良久,寻求高手们帮忙,或者一起探讨也行!

解决方案 »

  1.   

    你是点击了pictureBox1吗,如果不是,肯定触发不了,你调试下看看吧
      

  2.   

     private void Form1_Load(object sender, EventArgs e)
      {
       
      netChart = new NetChart4();
      netChart.SelectedPoint+=new NetChart4.ShowSelectedPoint(change);
    netChart.Show();
      }
      

  3.   

    本来就是显示着的,就是form1里面的一个控件,用不用show()没区别吧
      

  4.   

    委托还不太懂,楼主试试
    将参数类型改为string->int
      

  5.   

    我调试发现,当鼠标点击pictureBox的时候,委托为null,不能触发委托传值,为什么呢?
      

  6.   


    那就是你netChart.SelectedPoint+=new NetChart4.ShowSelectedPoint(change);
    这个事件挂载有问题
      

  7.   

     netChart = new NetChart4();
      netChart.SelectedPoint+=new NetChart4.ShowSelectedPoint(change);你这是无效,,你这个控件是在窗体上面,,窗体和自定义控件已经加载完了,再去new一个给它加载事件有用么?如果你NetChart4是动态加载的,然后加载事件说不定可以,,
     NetChart4 netChart = new NetChart4();
      netChart.SelectedPoint+=new NetChart4.ShowSelectedPoint(change);
    this.Controls.Add(netChart);