7 Replies - 3010 Views - Last Post: 01 June 2011 - 02:48 PM Rate Topic: -----

#1 gonzalezerik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 09

Using picturebox with the arrow keys

Posted 01 June 2011 - 10:22 AM

I am new to C# programming, so I need some help to get me in the right direction. I am designing a GUI with 5 pictureboxes with images in them, which are arrows. What I am trying to do is when I press one of the arrow keys, it will change the color of the picture for which the arrow it was assigned to, to indicate that is the direction I have pressed. I am using visual studio 2005, any help would be much appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: Using picturebox with the arrow keys

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 10:39 AM

Have you investigated the 'keydown' event?

You know you can make the picture boxes as buttons (not text) and set the background image to the arrows as well, right? Then you can click on them in the screen too!
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 11:32 AM

And you're using a 6 year old version of Visual Studio because.... Why?
You can download a free version of VS2010 for free, you know?
The link is in the segment below.

Modi already mentioned the KeyDown event for detecting keypresses.
But just to be clear, you do realize you have to change the image in the picturebox in order to make it change color. You did realize that, right? There is no method to call that will swap all the red pixels for blue.

Have you coded anything toward this problem that you can show us?
Was This Post Helpful? 0
  • +
  • -

#4 gonzalezerik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 09

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 01:40 PM

View PosttlhIn`toq, on 01 June 2011 - 12:32 PM, said:

And you're using a 6 year old version of Visual Studio because.... Why?
You can download a free version of VS2010 for free, you know?
The link is in the segment below.

Modi already mentioned the KeyDown event for detecting keypresses.
But just to be clear, you do realize you have to change the image in the picturebox in order to make it change color. You did realize that, right? There is no method to call that will swap all the red pixels for blue.

Have you coded anything toward this problem that you can show us?


I am using it at this time because that is all I have, I am looking for the link you stated and cannot find it.
Here is what I have so far, what it is doing is every time I press one of the arrow keys, a message box pops up and says which button you pushed. What my ultimate goal for this GUI is that I am going to control an RC car with the arrow keys from the computer, and just want something to show which buttons are being pressed.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up)
            {
                MessageBox.Show("Up button was pressed");
            }

            if (e.KeyData == Keys.Right)
            {
                MessageBox.Show("Right button was pressed");
            }

            if (e.KeyData == Keys.Down)
            {
                MessageBox.Show("Down button was pressed");
            }

            if (e.KeyData == Keys.Left)
            {
                MessageBox.Show("Left button was pressed");
            }
        }

        private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            

        }

        private void pictureBox4_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            

        }

        private void pictureBox2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            

        }

        private void pictureBox3_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            

        }

       

        
    }
}

This post has been edited by modi123_1: 01 June 2011 - 01:41 PM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4525
  • View blog
  • Posts: 7,893
  • Joined: 08-June 10

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 01:45 PM

View Postgonzalezerik, on 01 June 2011 - 03:40 PM, said:

I am using it at this time because that is all I have, I am looking for the link you stated and cannot find it.


http://www.microsoft...ress/Downloads/

Well, you can get rid of all the PreviewKeyDown handlers (and subscriptions) since you're not doing anything with them. You've got all the logic you need in the Form1_KeyDown event. Of course, you should extract that into it's own method and call it from there.

Anyway, you're already pretty close. Instead of popping up a message box, change the proper picture box's picture to a different picture. You'll need one pic of the normal arrow, and one of the green arrow. Just swap between the two.

Also, you'll need to handle the Form's KeyUp event as well, and change the pic back to the normal colored one in that handler.
Was This Post Helpful? 1
  • +
  • -

#6 gonzalezerik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 09

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 02:22 PM

I understand what you are saying now, thank you for the help. So instead of showing the message box for the keydown event, I want to change the image in the picturebox, and for the keyup it goes back to the original image. Do you have any examples of this in C#, it would be greatly appreciated.

This post has been edited by Curtis Rutland: 01 June 2011 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4525
  • View blog
  • Posts: 7,893
  • Joined: 08-June 10

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 02:37 PM

Well, you can always check the MSDN.

PictureBox

By the way, in that example, it uses a Bitmap for setting the image. You can use this code to load the image.

Image img = Image.FromFile("full/file/path/here.jpg");

Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: Using picturebox with the arrow keys

Posted 01 June 2011 - 02:48 PM

There is one problem with Image.FromFile() though.
It maintains a link to the image on the hard drive for as long as your application is using the file.

Personally, I would put your two arrows as application resources and reference them that way.

Sorry about forgetting the link. I see Curtis was good enough to provide it. I had meant to paste in this set of links and resources for you.


Standard resources, references and suggestions for new programmers.


I would recommend you start with "Hello World" just like the other million+ coders out there. Then work your way up to the more advanced tasks like this.

The problem with taking on large, complex tasks like this when you are new to coding is that
  • it will frustrate you to the point of quitting,
  • you don't know enough about coding to know where to start or in what direction to design your program
  • You risk learning via the 'Swiss cheese' method where you only learn certain bits and pieces for the one project but have huge holes in your education.


I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

Free editions of Visual Studio 2010

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading
MSDN Top guideline violations, know what to avoid before you do it.
Design patterns as diagrams

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Passing values between forms/classes

Working with environmental variables
'Why do we use delegates?' thread

Debugging tutorial
Debugging tips
Great debugging tips
It still doesn't work, article

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.

And everyone always wants to connect to a database, right out of the gate so
Database tutorials right here on DIC

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    Form1_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

This post has been edited by tlhIn`toq: 01 June 2011 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1