C#新手,想实现一个窗口不断移动程序(下面的代码),但是运行出来,窗口一直未移动过,这是为什么?求教
System.Timers.TimerMyTimer=newSystem.Timers.Timer();privatevoidForm1_Load(objectsende...
System.Timers.Timer MyTimer = new System.Timers.Timer();
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private int x = 100, y = 100;
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Location = new Point(x++, y++);
} 展开
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private int x = 100, y = 100;
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Location = new Point(x++, y++);
} 展开
4个回答
展开全部
//因为Timers.Timer在一个新的线程运行,直接访问form1会报错的。
System.Timers.Timer MyTimer = new System.Timers.Timer();
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.SynchronizingObject = this;
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private int x = 100, y = 100;
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
((Form1)MyTimer.SynchronizingObject).Location = new Point(x++, y++);
}
展开全部
public delegate void formWalk();
public formWalk myDelegate;
System.Timers.Timer MyTimer = new System.Timers.Timer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
myDelegate = new formWalk(formgo);
this.Invoke(myDelegate);
}
private int x = 100, y = 100;
private void formgo()
{
this.Location = new Point(x++, y++);
}
多线程调用问题,你用invoke去调用主窗体,或者主窗体控件
public formWalk myDelegate;
System.Timers.Timer MyTimer = new System.Timers.Timer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
myDelegate = new formWalk(formgo);
this.Invoke(myDelegate);
}
private int x = 100, y = 100;
private void formgo()
{
this.Location = new Point(x++, y++);
}
多线程调用问题,你用invoke去调用主窗体,或者主窗体控件
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MyTimer.Start();改成
MyTimer.Enabled=true;试试
MyTimer.Enabled=true;试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.Timers.Timer 看MyTimer看 = new System.Timers.Timer();
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private int x = 100, y = 100;
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Location = new Point(x++, y++);
}
这里
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 1000;
MyTimer.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
MyTimer.Start();
}
private int x = 100, y = 100;
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Location = new Point(x++, y++);
}
这里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询