Hey there guys, this is my first post and yes it's because I need help.

I am not straight out asking for someone to do my work for me, I can get my work done for me at any-time no problem but then I won't learn and learn is something I want to do so badly!
I'm a first year computing student and I passed my introductory programming last semester, and this semester we continue using windows forms and creating applications to do certain things. It is not a programming heavy module but I'm still too much of a novice to sit down and write code, and that's why I joined here. I am wasting time and getting very frustrated trying to figure out things I want to do and I know when I see the answer later on that I would never have gotten there myself, so I need to gain experience to be able to just write code.

I am currently doing an application that controls cameras through ip addresses and I have made all the buttons that moves it around which I can see in a picture box, but to get top marks I must add a little extra to my program. What I am trying to do is use a mouse up inside the picture box that is 250 x 250, where if I click in a region that is not central the camera will move to centralize the mouse pointer. I think I need to do a series of loops and use X and Y axis > < but I am no use at putting it all together and I am reading books and using google to try and figure this out without much luck.

Would anyone here be willing enough to show me how to do this, with an explanation as to HOW your brain got the idea to write that specific piece of code. I am using visual basic 2008 express edition by the way!

I do appreciate the help here and I have tried so hard to do this on my own, many thanks to anyone who can give me any advice!

Cheers,

Ray

Recommended Answers

All 9 Replies

private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            int i = e.X;
            int j = e.Y;
       
            
        }

Hi there many thanks for the help, that code did help me!

I will explain a little more about what I am trying to achieve. I want to split the picture box into nine segments using the x and y axis, so that's 0 to 252 / 3 = segments 84 X 84 wide.

Here is the code (below) I used to split up the box and you will see why its doesn't work. What I cannot get to work in the code is when a segment is more than 84 but less than 252 to select the central part of the box, I tried (h > 84 && < 252 && v < 84) and a few other ways but I am stuck after a good while trying this, what is the way to do less than and greater than in code?

Thanks again for your help!

Cheers!


Ray

{
            int h = e.X;
            int v = e.Y;

            if (h < 84 && v < 84)
                MessageBox.Show("ONE");
            //move camera down and right

            if (h > 84 && v < 84)
                MessageBox.Show("TWO");
            //move camera down

            if (h > 168 && v < 84)
                MessageBox.Show("THREE");
            //move camera down and left

            if (h > 84 && v < 84)
                MessageBox.Show("FOUR");
            //move camera right

            if (h > 84 && v > 84)
                MessageBox.Show("CENTER");
            //camera is centered do not move

            if (h > 84 && v > 168)
                MessageBox.Show("SIX");
            //move camera left

            if (h > 168 && v < 84)
                MessageBox.Show("SEVEN");
            //move camera up and right

            if (h > 168 && v > 84)
                MessageBox.Show("EIGHT");
            //move camera up

            if (h > 168 && v > 168)
                MessageBox.Show("NINE");
            //move camera up and left


        }

try "flag" and "nested if"

My advise would be to utilise the Rectangle.Contains method.
You can create a rectangle and then use .Contains to see if a point falls inside the rectangle.
The following example has a Grid Class to store the rectangle and name for each area of the picturebox (i made it 252 x 252 to fit the 3 grid squares exactly). When the mouse moves over the picturebox it uses a delegate search to find the picturebox that contains the mouses current location and prints the name of the grid to a label

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

        private void Form1_Load(object sender, EventArgs e)
        {
            grids.Add(new Grid(new Point(0, 0), gridSize, "ONE"));
            grids.Add(new Grid(new Point(84, 0), gridSize, "TWO"));
            grids.Add(new Grid(new Point(168, 0), gridSize, "THREE"));
            grids.Add(new Grid(new Point(0,84), gridSize, "FOUR"));
            grids.Add(new Grid(new Point(84,84), gridSize, "CENTER"));
            grids.Add(new Grid(new Point(168,84), gridSize, "SIX"));
            grids.Add(new Grid(new Point(0,168), gridSize, "SEVEN"));
            grids.Add(new Grid(new Point(84,168), gridSize, "EIGHT"));
            grids.Add(new Grid(new Point(168,168), gridSize, "NINE"));
        }

        //list to store each grid
        List<Grid> grids = new List<Grid>();

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            //get location of mouse
            Point mouse = new Point(e.X, e.Y);

            //search grids to find one the one containing the mouses location
            Grid CurrentGrid = grids.Find(delegate(Grid g) { return g.Rectangle.Contains(mouse) == true; });

            if (CurrentGrid != null)
                //print grid name to a label
                label1.Text = CurrentGrid.GridName;
        }


        class Grid
        {
            public Rectangle Rectangle { get; set; }
            public string GridName { get; set; }

            public Grid(Point location, Size size, string name)
            {
                GridName = name;
                Rectangle = new Rectangle(location, size);
            }
        }
    }

My advise would be to utilise the Rectangle.Contains method.
You can create a rectangle and then use .Contains to see if a point falls inside the rectangle.
The following example has a Grid Class to store the rectangle and name for each area of the picturebox (i made it 252 x 252 to fit the 3 grid squares exactly). When the mouse moves over the picturebox it uses a delegate search to find the picturebox that contains the mouses current location and prints the name of the grid to a label

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

        private void Form1_Load(object sender, EventArgs e)
        {
            grids.Add(new Grid(new Point(0, 0), gridSize, "ONE"));
            grids.Add(new Grid(new Point(84, 0), gridSize, "TWO"));
            grids.Add(new Grid(new Point(168, 0), gridSize, "THREE"));
            grids.Add(new Grid(new Point(0,84), gridSize, "FOUR"));
            grids.Add(new Grid(new Point(84,84), gridSize, "CENTER"));
            grids.Add(new Grid(new Point(168,84), gridSize, "SIX"));
            grids.Add(new Grid(new Point(0,168), gridSize, "SEVEN"));
            grids.Add(new Grid(new Point(84,168), gridSize, "EIGHT"));
            grids.Add(new Grid(new Point(168,168), gridSize, "NINE"));
        }

        //list to store each grid
        List<Grid> grids = new List<Grid>();

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            //get location of mouse
            Point mouse = new Point(e.X, e.Y);

            //search grids to find one the one containing the mouses location
            Grid CurrentGrid = grids.Find(delegate(Grid g) { return g.Rectangle.Contains(mouse) == true; });

            if (CurrentGrid != null)
                //print grid name to a label
                label1.Text = CurrentGrid.GridName;
        }


        class Grid
        {
            public Rectangle Rectangle { get; set; }
            public string GridName { get; set; }

            public Grid(Point location, Size size, string name)
            {
                GridName = name;
                Rectangle = new Rectangle(location, size);
            }
        }
    }

great idea. it is a good piece of work

Thank you very much for your efforts I really do appreciate it!

I spent some time trying the code that you had written, but I cannot get it to work, I started fresh and created a form with a picture box at 255 X 255 and a label, that was all. I copied the code which i think is the correct places and I managed to get the errors down to nine all the same saying this:

Error 1 The name 'gridSize' does not exist in the current context

I tried initialising gridSize to 0 and other things that I tried never worked either. This is why programming is very frustrating, as I feel its difficult to figure out by myself.

I do appreciate all this help, and trust me it is all worth your time as I do want to learn.

Hopefully one day I can return the favour.

Cheers,

Ray

i am so sorry, i deleted a line by mistake when i was cleanign up the code to post. Directly above the grids.Add() lines should be Size gridSize = new Size(84, 84); .

Three posts i wrote this afternoon and all three i have had to say this in...sorry..my bad : /

Thats what i get for trying to post on a busy work day lol

For future reference, in Visual Studio, if you hover your mouse over the word Grid in "= new Grid(" it will bring up a tooltip to show you what the input parameters are. Here you will see that a new Grid requires a Point, a Size and a String.

Ryshad, thank you! no worries with the missing code I'm just relieved that it was missing code and not missing brain cells at my end ! That worked perfectly! Now all I need to do is get your code written into another way that kind of works the same and test it in the lab and hopefully I'll be good to go!

I really do appreciate it though, I have no idea how you came up with all that but hopefully one day I'll be able to do it myself!

Many thanks!

Ray

I've faced similar problems myself. I wrote an app that allowed the user to draw shapes on the form which then acted as buttons to turn parts of an external output board on and off. I played around with a couple of methods before i nailed it :p

As for being able to write it yourself, its easy if you have the patience to learn. My advise is always the same: learn from the ground up and dont be shy about doing the work. Everything you learn in C# is going to be some new twist on the key principles. If you can get a solid grasp on the basics then the rest is just a matter of discovering the new methods/implementations. Best way to learn is to experiment. When you find a new concept, play around with it. Change something and see what happens...figure out how it works and why it works the way it does :) Mostly, just have fun with it.

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.