Jump to content

Picturebox Yukarı, Aşağı, Sol, Sağ Hareket Ettirme


mertugruul
 Share

Recommended Posts

Merhaba.
 
http://prntscr.com/6sxau4
 
Resimde de görüldüğü gibi Form'da ki pictureBox'ı yukarı, aşağı, sol ve sağa doğru hareket ettirmek istiyorum. Location özelliğini kullanarak pictureBox'ı sağ ve sola hareket ettirebiliyorum onda sıkıntı yok. Ama Location özelliği ile yukarı ve aşağı olmuyor. Araştırıyorum Left ve Right metodlarını kullanamadım bir türlü.
 
Location'lu olan kodlarım bunlar bunlarda herhangi bir sıkıntı yok.

 

 

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int xKonumu = pictureBox1.Location.X;
            int yKonumu = pictureBox1.Location.Y;
 
            if (e.KeyCode == Keys.Right)
                pictureBox1.Location = new Point(xKonumu + 1, yKonumu);
            else if (e.KeyCode == Keys.Left)
                pictureBox1.Location = new Point(xKonumu - 1, yKonumu);
        }
 
if(e.KeyCode == Keys.Right)
    pictureBox1.Right +=1;
 
yapıyorum olmuyor. Yanlışmı kullanıyorum acaba? sade ve anlaşılır şekilde anlatırsanız sevinirim.
Link to comment
Share on other sites

Kodlarını aşağıdaki şekilde yazarsan çalışacaktır.

    private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            

            Point location = pictureBox1.Location;
            if (e.KeyCode == Keys.Up)
            {
                location.Y -= 4;
                pictureBox1.Location = location;
            }
            else if (e.KeyCode == Keys.Left)
            {
                location.X -= 4;
                pictureBox1.Location = location;
            }
            else if (e.KeyCode == Keys.Down)
            {
                location.Y += 4;
                pictureBox1.Location = location;
            }
            else if (e.KeyCode == Keys.Right)
            {
                location.X += 4;
                pictureBox1.Location = location;
            }


        }
Link to comment
Share on other sites

https://drive.google.com/file/d/0BxNqA5wo55BnR0E4cFA4WjJoUTA/

Form 2 nin kodlarına bak.. tavuk dediği picturebox

 

private void kontrol()
        {
            if (tavuk.Left < (sol.Left + sol.Width))
            {
                tavuk.Left = sol.Left + sol.Width;
            }
            if ((tavuk.Left + tavuk.Width) > sag.Left)
            {
                tavuk.Left = sag.Left - tavuk.Width;
            }
            if (tavuk.Top < (ust.Bottom))
            {
                tavuk.Top = ust.Bottom;
            }
            if ((tavuk.Top + tavuk.Height) > (alt.Bottom) -10)
            {
                tavuk.Top = alt.Top - tavuk.Height;
            }
        }
 
        int adim = 10;
        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                   // tavuk.Image = Image.FromFile(@"C:\Documents and Settings\All Users\Belgeler\Resimlerim\Örnek Resimler\gun.jpg");                   
                    tavuk.Left -= adim;
                     break;
 
                case Keys.Right:
                    //tavuk.Image=Image.FromFile(@"C:\Documents and Settings\All Users\Belgeler\Resimlerim\Örnek Resimler\kıs.jpg");
                     tavuk.Left += adim;
                     break;
 
                case Keys.Up:
                     tavuk.Top -= adim;
                     break;
 
                case Keys.Down:
                     tavuk.Top += adim;
                     break;
             default:
             break;
            }
            kontrol();
    }
 
        int artiX = 10, artiY = 10;
        private void timer1_Tick(object sender, EventArgs e)
        {
            tavuk.Left += artiX;
            tavuk.Top += artiY;
            if (tavuk.Right > sag.Left - 10)
            {
                artiX *= -1;
            }
 
            if (tavuk.Bottom > alt.Top - 10)
            {
                artiY *= -1;
 
            }
            if (tavuk.Left < sol.Right + 10)
            {
                artiX *= -1;
            }
            if (tavuk.Top < ust.Bottom + 10)
            {
                artiY *= -1;
            }
        }
 
        private void tavuk_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == true)
            {
                timer1.Enabled = false;
            }
            else
                timer1.Enabled = true;
        }

https://drive.google.com/file/d/0BxNqA5wo55BnZGRXNmdIY2VpckE/

Bu da işine yarabilir.

Link to comment
Share on other sites

Yardımlarınız için teşekkür ederim. Kodlarınızdan yola çıkarak kendime göre düzenledim kodları. İşe yarayan olur belki ileride öğrenim için.

            //Sağ tuş için + , Sol tuş için - değer veriyoruz Location özelliğinin X koordinatına.
 

            if (e.KeyCode == Keys.Right)
                pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
            else if (e.KeyCode == Keys.Left)
                pictureBox1.Location = new Point(pictureBox1.Location.X - 1, pictureBox1.Location.Y);
 
            //Aşağı tuş için + , Yukarı tuş için - değer veriyoruz Location özelliğinin Y koordinatına.
            else if (e.KeyCode == Keys.Down)
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + 1);
 
            else if (e.KeyCode == Keys.Up)
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - 1);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...