窗口间委托传值,困扰两天的问题,求解!
一个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();
}
}
对此困扰良久,寻求高手们帮忙,或者一起探讨也行!
------解决方案--------------------
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);