Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I was trying to learn to program space invaders by viewing a tutorial on YT following this; Visual Basic 2010 - Space Invaders - part 2 - moving the picturebox with keyboard events - YouTube[^]
In the tutorial it was used with Visual Basic and I tried to set it in c#.

In that tutorial I am trying to make my spaceship (when start playing the game) go left and right. But then there is an error
C#
Severity	Code	Description	Project	File	Line
Error	CS0200	Property or indexer 'KeyEventArgs.KeyValue' cannot be assigned to -- it is read only	Space Invaders	C:\Users\******\Documents\Visual Studio 2015\Projects\Space Invaders\Space Invaders\Form1.cs	61


Please can I have some advise with this? I would be glad if that happens. :)

What I have tried:

C#
namespace Space_Invaders
{
    public partial class parentform : Form
    {
        #region variables
        Boolean left;
        Boolean rig;
        #endregion

        #region button

        public parentform()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Hide();
            pictureBox1.Hide();
            pictureBox2.Hide();
            button1.Hide();
            button2.Hide();
            panel1.Show();
            Movecomp.Enabled = true;
            Movecomp.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
            this.Close();
        }
        #endregion

        #region timers

        private void Movecomp_Tick(object sender, EventArgs e)
        {
            if (left == true)
            {
                pictureBox3.Left = -3;
            }
        }
        #endregion

        #region keypresses

        private void movecomptleft(object sender, KeyEventArgs e)
        {
            if (e.KeyValue = Keys.A)
            {
                left = true;
                
            }


        }

        private void movecomptstop(object sender, KeyEventArgs e)
        {



        }

        #endregion
    }
}
Posted
Updated 20-Mar-16 6:48am
v2
Comments
BillWoodruff 20-Mar-16 20:11pm    
In WinForms, the PictureBox, dragged on to a Form, like the Panel Control, does not receive events like Focus, and Key Events of any type.

But, be aware that by sub-classing the PictureBox, and defining some overrides for certain events, you can get keystrokes, including the arrow keys, get the Focus event, etc.

One reason to do this is to not mix code for the Form with code for the PictureBox: with the standard PictureBox you can only get the Key events by setting the Form 'KeyPreview Property to 'true and handling Form Key Events.

If you want to see an example of this, ask another question, and I'll reply with some code.

1 solution

The problem is:
C#
if (e.KeyValue = Keys.A)

You want to check equality. For this, you have to use two = signs, not one (one = sign is for assignment). Additionally, KeyValue contains an int, but you check it against a value of the Keys enum, so you have to replace "KeyValue" with "KeyCode" or "KeyData" (KeyCode when you don't care about modifiers, KeyData when you do care about modifiers).
C#
if (e.KeyCode == Keys.A)
 
Share this answer
 
v2
Comments
Andy Janssens 20-Mar-16 12:54pm    
Yeah thanks, I just tried this solution but than this comes up.

Severity Code Description Project File Line
Error CS0019 Operator '==' cannot be applied to operands of type 'int' and 'Keys' Space Invaders C:\Users\*******\Documents\Visual Studio 2015\Projects\Space Invaders\Space Invaders\Form1.cs 61

Any ideas?
Thomas Daniels 20-Mar-16 13:10pm    
I updated my answer.
[no name] 20-Mar-16 12:54pm    
A 5.

And BTW for OP: In case left is boolean, "if (left == true)" simply use "if (left)"... booleans do not become more true than true ;)
Andy Janssens 20-Mar-16 13:01pm    
Excuse me, but I do not understand the part "A 5.", what do you mean with that? :O Also thanks for the other tip. :D
[no name] 20-Mar-16 13:05pm    
"A 5" means only, that i voted a "5" for the answer (These five stars right up). A Thing you can also do beyond accepting the answer ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900