9 Replies - 404 Views - Last Post: 16 June 2013 - 02:36 PM Rate Topic: -----

#1 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Find Control Name

Posted 16 June 2013 - 11:53 AM

So I created a test program where when user click somewhere on form, a picturebox will pop up with specified image on the clicked location.

And when the user clicked that generated picturebox, a messagebox will appear containing the component name and the coordinate.

This is what I have so far

public partial class Form1 : Form
    {
        bool selected = false;
        int x = 0; int y = 0;

        private PictureBox PicBox;

        protected override void onmousemove(MouseEventArgs e)
        {
            x = e.X; y = e.Y;
        }

        public Form1()
        {
            InitializeComponent();
      
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (selected == false)
            {
                selected = true;
            }
            else
            {
                selected = false;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            
        }

        private void PictureBox_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Name + ", X : " + this.x + "| Y : " + this.y);            
        }
        private void Form1_Click(object sender, EventArgs e)
        {
            EventHandler handler = new EventHandler(PictureBox_Click);
            PicBox = new PictureBox();
            PicBox.Image = Image.FromFile(@"C:\id_flag.png");
            PicBox.Top = y;
            PicBox.Width = 16;
            PicBox.Height = 16;
            PicBox.Left = x;
            PicBox.Click += handler;
            //ADD CONTROLS 
            Controls.Add(PicBox);
        }
    }



It works fine, whenever I clicked the form, an image will pop there. But when I clicked that image, the messagebox shows like this

Posted Image

How to make it to show the current name of the clicked component?
like

PicBox1, X : 12 | Y : 13
PicBox2, X : 24 | Y : 112


Thankn you!

Is This A Good Question/Topic? 0
  • +

Replies To: Find Control Name

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Find Control Name

Posted 16 June 2013 - 12:20 PM

You aren't assigning a name when you make the new picturebox.
Re-read the method starting at line 45. You never set the .Name property.

Next... Line line 43... this is the form, not the picturebox so it will read "form1". The object that raised the event is what you want. That would be "sender". Cast it to a picturebox then get its name.

MessageBox.Show(this.Name + ", X : " + this.x + "| Y : " + this.y); 

//becomes

PictureBox temp = (PictureBox)sender;

MessageBox.Show(temp.Name + ", X : " + temp.x + "| Y : " + temp.y); 

This post has been edited by tlhIn`toq: 16 June 2013 - 12:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Find Control Name

Posted 16 June 2013 - 12:21 PM

And to add onto what tlhIn`toq has said, once you add the name all you will have to do is use the .Name property to get at the name. In your picturebox_click, "sender" is the control that caused the event. So that is what you are going to use to get access to the picturebox that was clicked and thus the .Name property of sender is going to give you the name.

:)

P.S you can shorten this code...

if (selected == false)
            {
                selected = true;
            }
            else
            {
                selected = false;
            }



to something like this...

selected = !selected



Might make things a bit simpler since you are just flipping the state of a boolean. :)

This post has been edited by Martyr2: 16 June 2013 - 12:25 PM

Was This Post Helpful? 2
  • +
  • -

#4 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Re: Find Control Name

Posted 16 June 2013 - 12:55 PM

Thank you :sweatdrop: it's good now

int varname = 1;

------
PicBox.Name = "PixBox" + varname.ToString();


And also, I remembered component can't have same name, but in this code I just put this PicBox.Name = "PixBox"; and every pictureboxes I generated has same name
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Find Control Name

Posted 16 June 2013 - 01:27 PM

Well the solution you have come up with isn't the ideal solution and a bit of a hack. Using the sender object passed into your click would be a better solution since it wouldn't rely on you hardcoding any kind of name in. Leaving you free to name the pictureboxes any way you want in the future.

In other words...

// Would show a messagebox containing the name of the picturebox clicked 
// (granted it was given a name when created)
MessageBox.Show(((PictureBox)sender).Name); 



Just to give you another tip. :)

This post has been edited by Martyr2: 16 June 2013 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Find Control Name

Posted 16 June 2013 - 01:40 PM

There are plenty of design issues here that could be cleaned up.
For one, having the form handle the response to a click on the object is just plain wrong. The object (picturebox in this case) should take care of itself and not burden the form with responding.

The pictureboxes shouldn't *have* to have names at all. Its a sign of bad OOP.

I'm not a fan of hardcoding the width and height like that. It doesn't make the form scalable for monitor size, or upsizing for visually impaired users.

Line 49 using Image.FromFile() like that is just bad juju.

But I got the impression this is just R&D code and not code from anything 'genuine' that will become released.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Find Control Name

Posted 16 June 2013 - 01:51 PM

Quote

For one, having the form handle the response to a click on the object is just plain wrong. The object (picturebox in this case) should take care of itself and not burden the form with responding.

Unless I've mis-interpreted, the form-click event creates the pictures, which are each assigned their own click-event to produce the messagebox.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Find Control Name

Posted 16 June 2013 - 01:56 PM

Right - and that messagebox is being produced by the Form instead of being handled by the PictureBox object.

Personally I'd make a "Target" class or UserControl that includes the PictureBox, any other items its should have, and the methods for doing its own handling. So if its a game target to be clicked on, it should have its own .Selected property, its own .Score value, its own method for .Explode() and so on. One would start with a .Clicked() method that would open this MessageBox

Then you can place as many of those as you like, and have them each respond to their own click instead of making the form do it for them.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Find Control Name

Posted 16 June 2013 - 02:17 PM

@ tlhIn`toq He, he! I was just wondering how easy it would be to create a custom control for this ;)
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Find Control Name

Posted 16 June 2013 - 02:36 PM

10 minutes or less for the base.
Then add more time as you add more features.

Look at my tutorials for "Making your first application" parts 1 and 2 to get started.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1