Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在加载事件后的某个时间,在我的窗体上显示图片。_C#_Winforms - Fatal编程技术网

C# 在加载事件后的某个时间,在我的窗体上显示图片。

C# 在加载事件后的某个时间,在我的窗体上显示图片。,c#,winforms,C#,Winforms,我想让图片在加载后的一定时间内出现在表单上,然后让图片在表单边界内以受控方式移动。如果能给我一个代码示例,让我开始计时事件,我将不胜感激 public partial class Form1 : Form { int horiz, vert, step; public Form1() { InitializeComponent(); } private void timer1_

我想让图片在加载后的一定时间内出现在表单上,然后让图片在表单边界内以受控方式移动。如果能给我一个代码示例,让我开始计时事件,我将不胜感激

public partial class Form1 : Form
    {

        int horiz, vert, step;


        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {


            //image is moved at each interval of the timer

            goblin.Left = goblin.Left + (horiz * step);
            goblin.Top = goblin.Top + (vert * step);


            // if goblin has hit the RHS edge, if so change direction left
            if ((goblin.Left + goblin.Width) >= (Form1.ActiveForm.Width - step))
                horiz = -1;

            // if goblin has hit the LHS edge, if so change direction right
            if (goblin.Left <= step)
                horiz = 1;

            // if goblin has hit the bottom edge, if so change direction upwards
            if ((goblin.Top + goblin.Height) >= (Form1.ActiveForm.Height - step))
                vert = -1;

            // if goblin has hit the top edge, if so change direction downwards
            if (goblin.Top < step)
                vert = 1;
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            //Soon as the forms loads activate the goblin to start moving 
            //set the intial direction
            horiz = 1;  //start going right
            vert = 1;   //start going down
            step = 5;   //moves goblin 5 pixels
            timer1.Enabled = true;

        }

    }
}
公共部分类表单1:表单
{
内水平、垂直、台阶;
公共表格1()
{
初始化组件();
}
私有无效计时器1_Tick_1(对象发送方,事件参数e)
{
//图像按计时器的每个间隔移动
妖精左=妖精左+(水平*步);
goblin.Top=goblin.Top+(垂直*步);
//如果地精击中了右边缘,则向左改变方向
如果((goblin.Left+goblin.Width)>=(Form1.ActiveForm.Width-步长))
水平=-1;
//如果地精击中了左侧边缘,则向右改变方向
if(goblin.Left=(Form1.ActiveForm.Height-step))
垂直=-1;
//如果地精击中了顶部边缘,则向下改变方向
if(goblin.Top<步骤)
垂直=1;
}
私有void Form1_Load_1(对象发送方,事件参数e)
{
//一旦表单加载,地精就会开始移动
//设定初始方向
horiz=1;//开始向右转
vert=1;//开始下降
步长=5;//移动妖精5像素
timer1.Enabled=true;
}
}
}

根据您目前向我们展示的内容,最简单的解决方案是使用您已经在使用的计时器,基本上跳过几次计时。让我们假设您当前的计时器发生在
100ms
上,即每秒10个计时器(10hz)

如果要将此活动延迟5秒,则需要跳过第一个刻度的
5*10
(50)。创建一个新的整数成员变量以存储已处理的刻度数:

private int ticks = 0;
每次计时器过期/滴答声时,请先执行以下操作:

ticks++;

if (ticks < 50) {
     // Don't do anything just skip
     return;
}
ticks++;
如果(刻度<50){
//什么都不要做,跳过就行了
返回;
}

您可以提供第二个“临时”计时器(timer2)


为什么不起作用?这确实起作用,我的照片在屏幕上来回跳动。但是我想要的是当表单加载时,图片在5秒钟后出现。这意味着图片将不可见,5秒钟后,图片将可见。这个问题似乎离题了,因为它属于“Plz snd me teh codez”类型。做个好人,试着记住你是新程序员的时候以及帮助你学习的东西。当我12岁的时候,我不得不请人向我解释一个球在乒乓球中是如何弹起的,因为我想不出来。请你把这个代码放到我的代码里好吗?因为我不知道放在哪里,如果你能谢谢我,我将非常感激。是的,计时器的间隔是100ms:(哈哈,但是我试过了,但是没有效果,因为我把
ticks++
放在条件块中。否则你会得到溢出异常并崩溃。是的,大约2年后它会溢出
private void Form1_Load_1(object sender, EventArgs e)
{
    //Soon as the forms loads activate the goblin to start moving 
    //set the intial direction
    horiz = 1;  //start going right
    vert = 1;   //start going down
    step = 5;   //moves goblin 5 pixels
    timer1.Tick += timer1_Tick_1;

    // temporary timer
    Timer timer2 = new Timer();
    timer2.Interval = 5000;
    timer2.Tick += delegate
    {
        // activate goblin timer
        timer1.Enabled = true;

        // deactivate 5s temp timer
        timer2.Enabled = false;
        timer2.Dispose();
    };
    timer2.Enabled = true;
}