hank kelley
hank kelley

Reputation: 13

How to construct a picture box in C#?

So im creating a flappy bird program, and all i want to do is call this method to create a pipe on the screen. I am having trouble actually making it appear. How do i call my method create pipe, will my method actually create a pipe?

    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 FlappyBird
{
    public partial class Form1 : Form
    {

        int yspeed = 0;
        int xspeed = 1;

        PictureBox pipe = new PictureBox();


        private void CreatePipe()
        {

            this.pipe = new PictureBox();

            this.pipe.Location = new Point(2, 2);
            this.pipe.Name = "pipe";
            this.pipe.Size = new Size(200, 200);
            this.pipe.TabIndex = 0;
            this.pipe.TabStop = false;
            this.pipe.BackColor = Color.Red;
            this.pipe.Visible = true;


        }





        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            MessageBox.Show("Press okay to start.");
            timer1.Enabled = true;
            CreatePipe();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            yspeed += xspeed;
            bird.Top += yspeed;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Space) {
                yspeed = -15;
            }
        }




        private void fileToolStripMenuItem_Click(object sender, EventArgs e)//new game
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)//about
        {

        }



    }

    }

Upvotes: 0

Views: 637

Answers (1)

Den
Den

Reputation: 16826

You need to place the PictureBox on the form, otherwise it stays in memory but is not visualized since it does not have a parent - call out:

this.Controls.Add(pipe);

Upvotes: 2

Related Questions