已有56人关注
第13题,窗体随机浮动有以下问题,请教下老师
发表在C#图书答疑 2021-09-02 悬赏:2 学分 《C#编程入门指南》第9章 Windows 控件—C/S 程序的基础
是否精华
版块置顶:

为什么我用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); 

        }

    }

}


分享到:
精彩评论 1
小禾斗
学分:7303 LV13
TA的每日心情
奋斗
2017-04-21 14:00:25
2021-09-08
沙发

因为你是新创建了一个窗体对象,不是当前的窗体。

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照