现在的情况是:父窗体上有一个点,子窗体上有两个numericUpDown,里面记录的是父窗体中这个点的坐标位置X,Y.我现在需要实现的是在父窗体上拖动这个点,子窗体的
两个numericUpDown里的坐标值相应改变,而改变两个numericUpDown里的坐标值,父窗体里的点的位置也相应的移动.不知道怎么实现?请高手支招。谢谢
两个numericUpDown里的坐标值相应改变,而改变两个numericUpDown里的坐标值,父窗体里的点的位置也相应的移动.不知道怎么实现?请高手支招。谢谢
public static class ChangePoint{ //改变点的位置后激发MumericUpDown 的事件
public static PointClass ThePoint = null;
private static Point position;//Point是位置
public staic Point Position{
set{
position= value;
theUpDown.OnChangeUpDown(null,null);
}
}
//设置MumericUpDown 控制时绑定MumericUpDown 的事件
public static MumericUpDown theUpDownCtl = null;
public staic MumericUpDown TheUpDownCtl{
set{
theUpDownCtl = value;
theUpDownCtl.OnChange = new EventHandler(ChangePoint.OnChangeUpDown);//注册事件代理
}
}
//事件代理
private static void OnChangeUpDown
{
Point newPoint = Point(theUpDown.x,theUpDown.y);//把TheUpDownCtl的值转换成Point
ChangePoint.Position = newPoint;
}
}
2:应用,绑定到ChangePoint就可以了
父窗体: ChangePoint.ThePoint = 你的点对象
子窗体:ChangePoint.TheUpDownCtl= 你的MumericUpDown控件
1 : 用最简单方式做个工具类去实现就可以了,以下是伪代码,仅说明思路而已。
public static class ChangePoint{ //改变点的位置后激发MumericUpDown 的事件
public static PointClass ThePoint = null;//PointClass 你的点类型
private static Point position;//Point是位置
public staic Point Position{
set{
position= value;
theUpDown.OnChangeUpDown(null,null);
}
}
//设置MumericUpDown 控制时绑定MumericUpDown 的事件
public static MumericUpDown theUpDownCtl = null;
public staic MumericUpDown TheUpDownCtl{
set{
theUpDownCtl = value;
theUpDownCtl.OnChange = new EventHandler(ChangePoint.OnChangeUpDown);//注册事件代理
}
}
//事件代理
private static void OnChangeUpDown
{
Point newPoint = Point(theUpDown.x,theUpDown.y);//把TheUpDownCtl的值转换成Point
ChangePoint.Position = newPoint;
}
}
2:应用,绑定到ChangePoint就可以了,工具类是个中间节,父窗体与子窗体不直接关联。
父窗体: ChangePoint.ThePoint = 你的点对象(PointClass )
子窗体:ChangePoint.TheUpDownCtl= 你的MumericUpDown控件
1 : 用最简单方式做个工具类去实现就可以了,以下是伪代码,仅说明思路而已。
public static class ChangePoint{ //改变点的位置后激发MumericUpDown 的事件
public static PointClass ThePoint = null;//PointClass 你的点类型
private static Point position;//Point是位置
public staic Point Position{
set{
position= value;
ThePoint.Move(position);//改变你的PointClass对象 的位置
}
}
//设置MumericUpDown 控制时绑定MumericUpDown 的事件
public static MumericUpDown theUpDownCtl = null;
public staic MumericUpDown TheUpDownCtl{
set{
theUpDownCtl = value;
theUpDownCtl.OnChange = new EventHandler(ChangePoint.OnChangeUpDown);//注册事件代理
}
}
//事件代理
private static void OnChangeUpDown
{
Point newPoint = Point(theUpDown.x,theUpDown.y);//把TheUpDownCtl的值转换成Point
ChangePoint.Position = newPoint;
}
}
2:应用,绑定到ChangePoint就可以了,工具类是个中间节,父窗体与子窗体不直接关联。
父窗体: ChangePoint.ThePoint = 你的点对象(PointClass )
子窗体:ChangePoint.TheUpDownCtl= 你的MumericUpDown控件
//program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new parent());
}
}
}//parent.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public class parent : Form
{
public parent()
{
this.Load += new EventHandler(parent_Load);
}
child child = new child();
void parent_Load(object sender, EventArgs e)
{
child.DragPointChanged += new child.DragPointChangedEventHandler(child_DragPointChanged);
child.Show();
} Point dragPoint = new Point(10, 10); public Point DragPoint
{
get { return dragPoint; }
set
{
if (dragPoint != value)
{
dragPoint = value;
child.SetDragPointLocation(dragPoint);
}
}
}
bool isDragging = false; void child_DragPointChanged(object send, DragPointChangedEventArgvs e)
{
dragPoint = e.CurrentPoint;
this.Invalidate();
} protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillEllipse(new SolidBrush(Color.Red), DragPoint.X - 5, DragPoint.Y - 5, 10, 10); base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{ if (e.Button == MouseButtons.Left && isDragging == false)
{
isDragging = true;
DragPoint = e.Location;
this.Invalidate();
}
else
{
isDragging = false;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && isDragging == true)
{
DragPoint = e.Location;
this.Invalidate();
}
else if (e.Button != MouseButtons.Left)
{
isDragging = false;
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
isDragging = false;
base.OnMouseUp(e);
}
}}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public class child : Form
{
NumericUpDown numericUpDownX = new NumericUpDown();
NumericUpDown numericUpDownY = new NumericUpDown();
public child()
{
this.Controls.Add(numericUpDownX);
this.Controls.Add(numericUpDownY); this.numericUpDownX.Location = new Point(10, 10);
this.numericUpDownY.Location = new Point(10, 50); this.numericUpDownX.Maximum = 100000;
this.numericUpDownX.Minimum = -100000; this.numericUpDownY.Maximum = 100000;
this.numericUpDownY.Minimum = -100000; this.numericUpDownX.ValueChanged += new EventHandler(numericUpDownX_ValueChanged);
this.numericUpDownY.ValueChanged += new EventHandler(numericUpDownY_ValueChanged); this.numericUpDownX.Value = 10;
this.numericUpDownY.Value = 10;
} public delegate void DragPointChangedEventHandler(object send, DragPointChangedEventArgvs e);
public event DragPointChangedEventHandler DragPointChanged;
private void OnDragPointChanged(DragPointChangedEventArgvs e)
{
if (DragPointChanged != null)
DragPointChanged(this, e);
}
void numericUpDownY_ValueChanged(object sender, EventArgs e)
{
OnDragPointChanged(new DragPointChangedEventArgvs(new Point((int)numericUpDownX.Value, (int)numericUpDownY.Value)));
} void numericUpDownX_ValueChanged(object sender, EventArgs e)
{
OnDragPointChanged(new DragPointChangedEventArgvs(new Point((int)numericUpDownX.Value, (int)numericUpDownY.Value)));
}
public void SetDragPointLocation(Point location)
{
this.numericUpDownX.Value = location.X;
this.numericUpDownY.Value = location.Y;
}
}
public class DragPointChangedEventArgvs
{
public Point CurrentPoint { get { return _currentPoint; } }
private Point _currentPoint;
public DragPointChangedEventArgvs(Point pt)
{
_currentPoint = pt;
}
}
}
{
form2 frm=new form2(); private void tag_MouseDown_Event(object sender,event e)
{
frm.ChangeValue(x,y); //x,y 為點的坐標
}
public form1()
{
frm.valueChangeEvent+=new EventHandle(set);
} private void set(object sender,event e)
{
tag.x=frm.x;
tag.y=frm.y;
}
}form2:
{
public event eventHandle ValueChangeEvent;
public int x;
public int y; public void ChangeValue(int x,int y)
{
numericUpDownX.value=x;
numericUpDownY.value=y;
} private void ValueChange()
{
if(valueChangeEvent!=null)
valueChangeEvent(null,null);
}
}這樣就行了