CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2008
    Posts
    10

    [RESOLVED] how to move an image ont the form

    Hi everyone

    I've a simple question, I want to put an image and a buton on a form. When the button is depressed the image moves right or left or up or down. maybe 4 buttons for each way.

    Could someone please help me to develop this at C# 2005

    Thanks in advance...

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to move an image ont the form

    Homework ?
    We dont offer solutions. If you are totally new; i would suggest to read a beginners book. Otherwise do what you are able to do and show where you are running nto troubles. You need a form, 4 Buttons, 4 Arrow pictures for the buttons (up, down, left, right ) a Timer to set pulses for movement as long as a button is pressed, a picturebox and the picture in it which you want to move.
    Add the Mouse Hover delegates to the buttons and put in the enabled statement for the timer and an enumeration for the direction which you define like
    Code:
    Private enum Direction {Up,Down,Left,Right};
    private Direction _direction;
    in the timers Tick delegate depending on the direction move the pictureboxes position. Each Tick some pxels. Thats one possible concept. the picture will move as long as you hover your mouse overone of the buttons. Now do it, showyour code, whenproblems occure.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Dec 2008
    Posts
    10

    Re: how to move an image on the form

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication8
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private int x = 0;

    private void button1_Click(object sender, EventArgs e)
    {
    timer1.Start();
    x = 0;
    }

    private void button2_Click(object sender, EventArgs e)
    {
    x = 1;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    if (x == 1)
    label1.Left = label1.Left - 5;
    pictureBox1.Left = pictureBox1.Left - 5;

    if (x == 0)
    label1.Left = label1.Left + 5;
    pictureBox1.Left = pictureBox1.Left + 5;
    }


    private void Form1_Load(object sender, EventArgs e)
    {
    pictureBox1.Image = System.Drawing.Image.FromFile("C:\\Documents and Settings\\Oz\\Desktop\\araba.jpg");
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    }
    }



    Question 1 - When I write label1.Left = label1.Left - 5; it is ok. But if I write label1.Right = label1.Right - 5; error happens.

    it says : Error 1 Property or indexer 'System.Windows.Forms.Control.Right' cannot be assigned to -- it is read only C:\Documents and Settings\Oz\My Documents\Visual Studio 2005\Projects\WindowsApplication8\WindowsApplication8\Form1.cs 38 13 WindowsApplication8


    Question 2- Label moves but picture box doesn't move. Why?

    By the way, Yes this is a homework. I used to use VB6 (last time i coded something was 2 years ago). And as a master student I get this visual programming lecture, but since I'm working at the same time I couldn't find time to read a book for it. and this homework due for next tuesday. so if I can give this one I'll have some more time to read a book and get familiar with C#.

    Thanks for paying attention.

    Regards...
    Last edited by zorspas; December 21st, 2008 at 07:50 AM.

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: how to move an image ont the form

    Regarding Error No.1: i don't think it is necessary that some one come here and say why error happened better than Visual Studio has prompted you.

    BTW; in General in the situations (in every programming languages) when you recieved cryptic error the best way - in addition to refering to documents - is to copy & paste the text of the error (and error number if it was provided by compiler) to google then at the very begning pages you will get information regarding what the error realy is and how to solve it.
    Last edited by toraj58; December 21st, 2008 at 08:16 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to move an image ont the form

    The answer to your error is: Label's Right Propery is Read Only !!
    Why do you need Right ? Do you want to stretch the label ? For Moving always use the Left and Top property if you want to size then use Width and Height. Right and Bottom are read only !!!!

    What should the Labels do there ? Have I written you will need any labels ?
    Code:
    private void tmMovementTicks_Tick(object sender, EventArgs e) {
        switch (_dir){
            case Direction.Right:
                pictureBox1.Left += 3;
                break;
            case Direction.Left:
                break;
            case Direction.Up:
                break;
            case Direction.Down:
                break;
            default:
                break;
        
        }
    }
    
    private void btStop_Click(object sender, EventArgs e) {
        _dir = Direction.None;
        tmMovementTicks.Enabled = false;
    }
    
    private void btRight_Click(object sender, EventArgs e) {
        tmMovementTicks.Enabled = true;
        _dir = Direction.Right;
    }
    In this example I have centered a Stop Button in the middle of your direction buttons. I only show to do one direction. Do the others yourself.
    And Please use CodeTags. How to do you can see in the bottom of my post in my signature
    Last edited by JonnyPoet; December 21st, 2008 at 09:33 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Dec 2008
    Posts
    10

    Re: how to move an image ont the form

    ...
    Last edited by zorspas; December 21st, 2008 at 10:03 AM.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to move an image ont the form

    Quote Originally Posted by zorspas View Post
    ...
    ??? And what you are wanting to express by that ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Dec 2008
    Posts
    10

    Re: how to move an image ont the form

    I've solved what I had asked, so I deleted it. When I got a new question I'll ask.

    (h)

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to move an image ont the form

    Quote Originally Posted by zorspas View Post
    I've solved what I had asked, so I deleted it. When I got a new question I'll ask.

    (h)
    Please reread Forum rules, its realy necessary for having an easy communication. Also please use the thread tools on top of the post and sign it as 'resolved'. Additional Hint: Whats against telling that your problem was solved just in the first place instead of setting three dots only. And its also nothing in our Forum rules against simple saying 'thx' if a question was solved successfully.

    You have to understand, its no help to do all the work for you, much better to bring you through your problems, so you yourself are able to solve them.
    Last edited by JonnyPoet; December 21st, 2008 at 04:30 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how to move an image ont the form

    Not to mention the fact that the forums actually have very little to do with solving the priginal posters question. The real value, is in having a repository that can be searched by millions over the coming years.

    This fact should be considered by all posters. When you post a question (and as the threead evloves), always consider if your current action is going to help those people. Deleting content, leaving threads hanging, saying I solved it without providing the details of the solution, are all poor manners.

    Remember that everyone answering posts here is volunteering their time. It is perfectly natural that many will invest their time helping people who show consideration in the above matters, and possibly ignoring those who have a history of not doing so.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Dec 2008
    Posts
    10

    Re: how to move an image ont the form

    got it. thanks
    Last edited by zorspas; December 21st, 2008 at 05:22 PM.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how to move an image ont the form

    Quote Originally Posted by zorspas View Post
    How can I use a number that has been entered in a Textbox,

    I want to add the number to some other value in the code.

    I tried to use Convert.ToInteger32(textbox1.text), but couldnt avoid having errors.
    Please go back and RE-READ the FAQ's AGAIN....

    You STILL do NOT have private messaging enabled....

    You have still left this thread in a sloppy state. And this post has absolutely nothing to do with the original question...

    For the last time...
    It is perfectly natural that many will invest their time helping people who show consideration in the above matters, and possibly ignoring those who have a history of not doing so.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Dec 2008
    Posts
    10

    Re: how to move an image ont the form

    Quote Originally Posted by TheCPUWizard View Post

    Deleting content, leaving threads hanging, saying I solved it without providing the details of the solution, are all poor manners.
    Sorry about that. I just didn't want other people to deal with an already solved problem.

    I was kind of hurry, so I have to admit that I couldn't pay enough attention to the rules of the forum.

    I'll be more careful to come. Thanks for your patience and understanding...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured