locked
C++/CLI - Get current control by name RRS feed

  • Question

  • Hi,

    Forgive me if the title doesn't make sense. I do not know how to title my post any better.

    In my form, I have picturebox controls "pbox01" to "pbox10".

    In my program, I want to dynamically get hold of the picturebox control so that I can display image in it.

    The reason i want to do this way is because I have a list of picturebox and i want to display image on it by using loop.

     

    Currently, I try to do it like this.

     

    PictureBox^ pbox=safe_cast<PictureBox^>(this->Controls[String::Format("pboxIO{0:00}",BitNo)]);   // BitNo is a counter which increase from 1 to 10

    After the first line of code is executed, pbox is still "undefine".

    Please advice how I can get it work.

    Thanks.


    • Edited by ejtay Tuesday, January 10, 2012 10:04 AM
    Tuesday, January 10, 2012 10:02 AM

Answers

  • Why dont you try to get through all controls, check if its PictureBox and then draw on it. You can also assign all picture bow on click to 1 procedure and draw on them with sender.

    safe_cast<PictureBox^>(Sender)

     

    • Proposed as answer by Helen Zhao Thursday, January 12, 2012 3:21 AM
    • Marked as answer by Helen Zhao Thursday, January 19, 2012 1:29 AM
    Wednesday, January 11, 2012 2:31 PM
  • Check if BitNo contains a correct value and String::Format constructs a right name. Then make sure the picture boxes are children of the form, not of a panel or other container. Otherwise use the Controls collection of the container.

     

    • Edited by Viorel_MVP Wednesday, January 11, 2012 9:02 PM
    • Proposed as answer by Helen Zhao Thursday, January 12, 2012 3:21 AM
    • Marked as answer by Helen Zhao Thursday, January 19, 2012 1:29 AM
    Wednesday, January 11, 2012 9:01 PM

All replies

  • PictureBox^ pbox=safe_cast<PictureBox^>this->Controls[String::Format("pboxIO{0:00}",BitNo)]);
    That looks wrong. Shouldn't it be "pbox{0:00}" ?
    Tuesday, January 10, 2012 4:09 PM
  • >In my form, I have picturebox controls "pbox01" to "pbox10".

    >In my program, I want to dynamically get hold of the
    >picturebox control so that I can display image in it.

    You know the Name of each PictureBox. You can change the
    image it displays by changing the Image property.

    pbox01->Image = Image::FromFile("MyNewPicture.bmp");
    pbox01->Refresh();

    See:

    How to: Set Pictures at Run Time (Windows Forms)
    http://msdn.microsoft.com/en-us/library/t94wdca5%28v=VS.90%29.aspx

    - Wayne
    Tuesday, January 10, 2012 9:06 PM
  • Wayne:

    That is what I don't want to do and I did said it in the post. The reason is because I want to assign pictures to it in a loop. Now I have 10 controls, in future I may have 20 or 100.

    Thanks anyway.

     

    Brian Muth:

    Sorry, my picturebox control is pboxIO00. I mistype it in the first post.

    Wednesday, January 11, 2012 12:43 AM
  • Why dont you try to get through all controls, check if its PictureBox and then draw on it. You can also assign all picture bow on click to 1 procedure and draw on them with sender.

    safe_cast<PictureBox^>(Sender)

     

    • Proposed as answer by Helen Zhao Thursday, January 12, 2012 3:21 AM
    • Marked as answer by Helen Zhao Thursday, January 19, 2012 1:29 AM
    Wednesday, January 11, 2012 2:31 PM
  • Check if BitNo contains a correct value and String::Format constructs a right name. Then make sure the picture boxes are children of the form, not of a panel or other container. Otherwise use the Controls collection of the container.

     

    • Edited by Viorel_MVP Wednesday, January 11, 2012 9:02 PM
    • Proposed as answer by Helen Zhao Thursday, January 12, 2012 3:21 AM
    • Marked as answer by Helen Zhao Thursday, January 19, 2012 1:29 AM
    Wednesday, January 11, 2012 9:01 PM
  • Hi ejtay,

    Would you mind letting me know the result of the suggestions? If you need further help, please feel free to let me know, we'll be more than happy to be of your assistance.

    Best regards,

    Helen


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 17, 2012 1:31 AM