I am having some trouble figuring out how to Validate data on my robot project so that the robot stays in the range of say 100-150. Also my Go 10 button is only going 1 space even though I have it labled as

private void Go10_Click(object sender, EventArgs e)
            {

                Robby.Move(10);
                positionLbl.Text = Robby.location.ToString();
                Robby.Draw(pnlRobot); 


            }

and my Go 1 button is labled as Robby.Move(1);

My main form code:

using System;


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

    namespace RobotCS
    {
        public partial class Form1 : Form
        {
            private Robot Robby = new Robot();
            public Form1()
            {

                InitializeComponent();

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                robotLbl.Text = Robby.ToString();
                Robby.Draw(pnlRobot);
                positionLbl.Text = Robby.location.ToString();

            }

            private void Exit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }


            private void North_Click(object sender, EventArgs e)
            {
                // set direction of the robot to north
                Robby.direction = Direction.North;
                robotLbl.Text = Robby.ToString();   
            }

            private void East_Click(object sender, EventArgs e)
            {
                Robby.direction = Direction.East;
                robotLbl.Text = Robby.ToString();   
            }

            private void South_Click(object sender, EventArgs e)
            {
                Robby.direction = Direction.South;
                robotLbl.Text = Robby.ToString();   
            }

            private void West_Click(object sender, EventArgs e)
            {
                Robby.direction = Direction.West;
                robotLbl.Text = Robby.ToString();  
            }

            private void Go1_Click(object sender, EventArgs e)
            {

                Robby.Move(1);
                positionLbl.Text = Robby.location.ToString();
                Robby.Draw(pnlRobot); 


            }


            private void Go10_Click(object sender, EventArgs e)
            {

                Robby.Move(10);
                positionLbl.Text = Robby.location.ToString();
                Robby.Draw(pnlRobot); 


            }
        }
    }




Class code: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace RobotCS
{
    public enum Direction
    {

        North = 233,
        West = 231,
        South = 234,
        East = 232
    }
    class Robot
    {
        public Direction direction;

        public Robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = Direction.North;
        }
    //   public Direction direction { get; set; }

        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(Panel P)
        {
            Point L = new Point(location.X + P.Width / 2, location.Y + P.Height / 2);
            P.Controls[0].Location = L;
        }

             public override string ToString()
        {

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

Any and all help would be great! =)

-Crima

Dani AI

Generated

Two likely root causes explain the "Go 10" behavior: the Go10 button might still be wired to the Go1 handler (label changed but event not), or the UI/coordinate math makes a 10‑unit move look like a single step. 's breakpoint advice is solid — confirm the Click handler in the Properties window or Form1.Designer.cs (look for this.Go10.Click += ...) and put a breakpoint in Go10_Click to observe the argument passed and Robby.location before/after the call.

A more robust design is to keep boundary checks inside Robot and make movement return success/failure so the UI can react. Example pattern: give the Robot a Bounds (or Min/Max) and a TryMove method that computes the candidate location, tests it, then commits only if legal.

public Rectangle Bounds { get; set; } = new Rectangle(100, 100, 51, 51); // enforces 100..150

public bool TryMove(int units)
{
    Point candidate = location;
    switch (direction)
    {
        case Direction.North: candidate.Y -= units; break;
        case Direction.South: candidate.Y += units; break;
        case Direction.West:  candidate.X -= units; break;
        default:              candidate.X += units; break;
    }
    if (!Bounds.Contains(candidate)) return false;
    location = candidate;
    return true;
}

Then call TryMove from the button handler and update the UI only on success. Extra checks: reference the robot control by name (not P.Controls[0]), verify its Anchor/Dock (anchoring can make small moves look strange), remember Draw offsets location by panel center so visual delta can be misleading, and consider using direction.ToString() (rather than casting to char) for readable labels. These changes make validation explicit and debugging much simpler.

Between lines 144 and 145 you should test if the robot is still in the range, if not, then don't change the location property.

And I don't see anything obviously wrong with the move code. Put a breakpoint in the Go10_Click event and see if it is making it into that code (and make sure you have the right event tied to the button).

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.