子窗体和父窗体的互动问题

ybgiser 2009-04-25 09:24:54
现在的情况是:父窗体上有一个点,子窗体上有两个numericUpDown,里面记录的是父窗体中这个点的坐标位置X,Y.我现在需要实现的是在父窗体上拖动这个点,子窗体的
两个numericUpDown里的坐标值相应改变,而改变两个numericUpDown里的坐标值,父窗体里的点的位置也相应的移动.不知道怎么实现?请高手支招。谢谢
...全文
184 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhantianyou 2009-04-25
  • 打赏
  • 举报
回复
form1:
{
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);
}
}

這樣就行了
chen_de_sheng 2009-04-25
  • 打赏
  • 举报
回复

//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;
}
}
}

Sysping1 2009-04-25
  • 打赏
  • 举报
回复
再改正下,不然会死循环...
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控件
Sysping1 2009-04-25
  • 打赏
  • 举报
回复
改正下...
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控件
yabo300 2009-04-25
  • 打赏
  • 举报
回复
........
Sysping1 2009-04-25
  • 打赏
  • 举报
回复
1 : 用最简单方式去实现就可以了,以下是伪代码,仅说明思路而已。
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控件
红三天 2009-04-25
  • 打赏
  • 举报
回复
第一个更容易啊,在子窗体里设两个public变量,在主窗体的事件中给它们赋值就可以了!
fanbo 2009-04-25
  • 打赏
  • 举报
回复
同意
ybgiser 2009-04-25
  • 打赏
  • 举报
回复
第二个过程用委托事件可以实现,但是第一个过程怎么实现呢?
deyter 2009-04-25
  • 打赏
  • 举报
回复
这个用委托能办到的
zhang_zhen_biao_ 2009-04-25
  • 打赏
  • 举报
回复
在父窗体中定义静态公有变量来存该窗体的点坐标,在子窗体中加一timer控件,每隔一秒中给子窗体显示坐标的控件赋值。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧