LowTds 0 Newbie Poster

Hello, I have been trying to get this robot to work out and be able to move around. I am just so confused as to how to get my arrow to get even displayed and moved when pressed by one of the directions to go.

One area that I struggle with is the classes and event handlers as has probably been my biggest issue facing this assignment.
Anyways this is what I have so far and thanks in advance for any help.

Robot.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;

namespace Robot
{

    public enum Direction {North,East,South,West }

    public delegate void RangeLimitEventHandler(object source, RangeLimitArgs re);

    public class RangeLimitArgs { }



   public class robot
    {
        private int range;
        public Direction orientation;

        public Direction direction;
        public robot() 
        {
            location = new Point();           
            direction = Direction.North;
        }

        public Point location { get; set; }
        public void Move(int unitToMove)
        {
            Point P = new Point();
            switch (direction)
            {
                case Direction.North:
                    P.X = location.X;
                    P.Y = location.Y - unitToMove;
                    break;
                case Direction.West:
                    P.X = location.X - unitToMove;
                    P.Y = location.Y;
                    break;
                case Direction.South:
                    P.X = location.X;
                    P.Y = location.Y + unitToMove;
                    break;
                case Direction.East:
                    P.X = location.X + unitToMove;
                    P.Y = location.Y;
                    break;
                default:
                    break;
            }
            location = P;
        }

        public void Draw(Graphics G)
        {

            SolidBrush redBrush = new SolidBrush(Color.Red);
            Size S = new Size(40, 40);
            Rectangle R = new Rectangle(location, S);

            G.FillEllipse(redBrush, R);
        }

        public override string ToString()
        {

            return ((char)direction).ToString();
        }
    }
}

form.cs

using Robot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Robot5

{
    public enum Arrow { Up = 233, Right = 232, Down =234, Left = 231}
    public partial class Form1 : Form
    {
        private Robot robot;
        private Point center;
        public Form1()
        {
            InitializeComponent();
        }
       //private void DirectionButton_Click(object sender,EventArgs e)

        private void HandelRangeExceeded(object sender, ArgumentOutOfRangeException re)
        {
            MessageBox.Show("The robot tried to move too far" + re.GetInfo()));

        }

        private void btnNorth_Click(object sender, EventArgs e)
        {

        }

        private void btnEast_Click(object sender, EventArgs e)
        {

        }

        private void btnSouth_Click(object sender, EventArgs e)
        {

        }

        private void btnWest_Click(object sender, EventArgs e)
        {

        }

        private void btnGo1_Click(object sender, EventArgs e)
        {

        }

        private void btnGo10_Click(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

}

Dani AI

Generated

A few likely causes and a short fix list for .

The arrow not appearing or moving is almost always a drawing/refresh or wiring problem rather than math. Keep one Robot instance at form scope, draw it from the Paint event, and invalidate the drawing surface after any state change. Also check event-handler signatures: the robot class should raise a typed EventArgs and the form must subscribe with a matching method.

Practical checklist:

  • Create the Robot once (form field) and initialize it in the constructor.
  • Put all rendering in the control's Paint handler and use the supplied Graphics object. Do not create temporary Graphics objects for persistent drawing.
  • In each direction or Go button handler change the robot state (facing, attempted move) and then call Invalidate() on the panel/form so Paint runs.
  • Do bounds checking in the robot Move method. If the move would go outside allowed bounds, raise a well-typed event (EventHandler<T>) instead of relying on mismatched exception-handler signatures.
  • Turn on double buffering for the drawing surface to avoid flicker.
  • Add quick debug output (Debug.WriteLine) inside button handlers and the Paint handler to confirm state and that Paint is being called.

Example of a safe event pattern (different names than in the original post):

public class RangeExceededEventArgs : EventArgs
{
    public Point Attempted { get; }
    public RangeExceededEventArgs(Point attempted) { Attempted = attempted; }
}

public class Robot
{
    public event EventHandler<RangeExceededEventArgs> RangeExceeded;
    public Point Position { get; private set; }
    public void Move(int units, Rectangle allowed)
    {
        Point candidate = /* compute from Position and facing */;
        if (!allowed.Contains(new Rectangle(candidate, new Size(1,1))))
            RangeExceeded?.Invoke(this, new RangeExceededEventArgs(candidate));
        else
            Position = candidate;
    }
}

Quick troubleshooting if it still fails: set breakpoints in the button handlers and in Paint to verify the same Robot instance is used; log Position after Move; ensure the drawing rectangle size is considered when checking bounds; and fix any misspelled handler names when subscribing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.