为什么我用this.Location = new Point(x, y); 可以正常运行及随机位置。
我用 Form1 fom1 = new Form1();fom1.Location = new Point(x, y); 然后运行,程序不出错,但是窗口不随机位置。
源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _9713
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
}
private void timer1_Tick(object sender, EventArgs e)
{
int x, y;//可以运行
Random rd = new Random();
x = rd.Next(0, 600);
y = rd.Next(0, 600);
this.Location = new Point(x, y);
//Form1 fom1 = new Form1();//不能运行
//int x, y;
//Random rd = new Random();
//x = rd.Next(0, 600);
//y = rd.Next(0, 600);
//fom1.Location = new Point(x, y);
}
}
}