Click to See Complete Forum and Search --> : [RESOLVED] how to move an image ont the form
zorspas
December 20th, 2008, 03:18 PM
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...
JonnyPoet
December 20th, 2008, 04:09 PM
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
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.
zorspas
December 21st, 2008, 06:43 AM
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...
toraj58
December 21st, 2008, 07:03 AM
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.
JonnyPoet
December 21st, 2008, 08:24 AM
The answer to your error is: Label's Right Propery is Read Only !!:D
Why do you need Right ? Do you want to stretch the label ? :lol: 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 ?
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
zorspas
December 21st, 2008, 08:33 AM
...
JonnyPoet
December 21st, 2008, 09:09 AM
...??? And what you are wanting to express by that ?
zorspas
December 21st, 2008, 02:34 PM
I've solved what I had asked, so I deleted it. When I got a new question I'll ask.
(h)
JonnyPoet
December 21st, 2008, 03:24 PM
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.
TheCPUWizard
December 21st, 2008, 03:34 PM
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.
zorspas
December 21st, 2008, 04:04 PM
got it. thanks
TheCPUWizard
December 21st, 2008, 04:13 PM
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.
zorspas
December 21st, 2008, 05:37 PM
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...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.