CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Hi, noob question

    Thank you in advance,
    My problem with the code is that i want to swap p1 which is a picturebox for a different picture box as it loops. I have 100 pictureboxes that i need to loop throughm but i can't figure out how to swap p1 for p2 and so on..


    Code:
       public Form1()
            {
                InitializeComponent();
    
                int row = 0, column = 0; // used for 2d array
    
                for (int count = 1; count < 101; count++)
                {
                    boxes[row, column] = p1;
    
                    if (column == 10)
                    {
                        row++;
                        column = 0;
                    }
                }
                
            }
            PictureBox[,] boxes = new PictureBox[10,10];
    Last edited by DataMiser; April 30th, 2012 at 04:54 PM. Reason: added code tags

  2. #2
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Re: Hi, noob question

    Do use code tags to post your code and keep it's formatting.
    e.g [code] your code goes here &#91;/code&#93;

    My First suggestion is that you put your implementation in a method.

    If you intend randomly swapping your pictures then use a random number generator in your implementation.

    Will BRB to explain better.
    Last edited by chillaxzino; May 3rd, 2012 at 09:29 PM.

  3. #3
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Resolved Re: Hi, noob question

    Sorry I've been away for a while and couldn't respond to your post early enough.

    Here's what I've to say. If you want to loop through a multidimensional array of the rectangular type, that is an array where each row has the same number of columns.

    e.g:
    Code:
     PictureBox[,] myPictireBoxes = new PictureBox[10,10];
    You'll have to loop through each row and column respectively. To do this use the Array.GetLength(int dimension) method in your for statement. Where parameter dimension is zero based, 0 for row and 1 for your column.

    See an example below.

    Code:
                 // creating a picture box array
                PictureBox[,] myPictureBoxes = new PictureBox[2,2];
    
                // initializing myPictureBoxes
                for (int rowCounter = 0; rowCounter < myPictureBoxes.GetLength(0); rowCounter++)
                {
                    for (int columCounter = 0; columCounter < myPictureBoxes.GetLength(1); columCounter++)
                    {
                        myPictureBoxes[rowCounter, columCounter] = new PictureBox();
                    }
                }

    If I'm to randomly assign pictures to this boxes. I'd do this:

    Code:
                 // add images randomly to these boxes
                foreach (var pictureBox in myPictureBoxes)
                {
                    pictureBox.Image = GetRandomImage();
                }
            
    
            /// <summary>
            /// Randomly get images from the resource manger
            /// </summary>
            /// <returns>A random image.</returns>
            private Image GetRandomImage()
            {
                string randomImage = string.Format("picture{0}", randonNumber.Next(4));
                Image newImage = (Image)Properties.Resources.ResourceManager.GetObject(randomImage);
                return newImage;
            }
    Where the pictures in my resource manager are named picture1, picture2, picture3 and picture4 respectively.
    Please note that all other code blocks except the GetRandomImage() code block should be in a method.
    Last edited by chillaxzino; May 3rd, 2012 at 10:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured