Need help with for and pictureboxes

I'm making a map editor to make maps
Every tile is a picurebox. In the menu there is a command "New" wich will load all the default pics into the picturebozes, so you can make a new map
i created this for statement

dim i as integer
For i = 0 To 391

PictureBox(i).image = My.Resources.Image_default

Next i

But this doesn't work. There is something wrong with the PictureBox(i).image
Please help me. I'm using vb2005

Thanks in advance,
Radexxion


Answer this question

Need help with for and pictureboxes

  • Karsten Januszewski MSFT

    Let's assume you have 10 picture boxes named PictureBox1, PictureBox2 etc.

    First of all you need to create an array to hold the picture boxes.

    Dim PictureBoxes(10) As PictureBox

    This actually creates an 11 element array, element 0 will be left empty unless you first picture box is called PictureBox0

    Then you add each of your boxes to the array (or strictly speaking you add a reference to each box to the array)

    For Each ctrl As Control In Me.Controls

    If TypeOf ctrl Is PictureBox Then

    PictureBoxes(CInt(Mid(ctrl.Name, 11))) = CType(ctrl, PictureBox)

    End If

    Next

    Then you can reference any of the boxes through the array, e.g. PictureBox8 is referenced by PictureBoxes(8).

    For counter As Integer = 1 To 10

    PictureBoxes(counter).BackColor = Color.Yellow

    Next

    The thought occurs that you may wish to create a two dimensional array to reference the boxes as I presume they will be laid out in a rectangular format on screen. You could then use the array indeces to reference the map as X and Y co-ordinates.

    Dave


  • Heatmeista

    Dave, I'm not VB programmer, but some things looks really strange to me, can you explain them a little more

    1. why declaration of 10 elements array create 11 elements In other .NET languages creating 10 elements array will create 10 elements array with indices 0..9
    2. what is the meaning of CInt(Mid(ctrl.Name, 11)) I think you try to find in control Name property it's index. But, why 11 here Typical control naming (if created by designer) is "pictureBoxN", so you need to skip 10 chars and since in .NET strings and other arrays indexed from 0 - you must point to index 10 (N). And after all - where is guarantee that this controls will be named so
    3. why not use List<> or ArrayList collection classes to hold picture boxes They have features to Add() items to them.


  • Nokas

    Your picture box must be named in designer, typically this is pictureBox1 or something alike. If you create own picture boxes - you should use them. Also PictureBox do not have image property, it have Image property.


  • Carpe Noctem

    It works fine because you can't see that whole PC stops responding. The more controls you have - the more resources Windows & .NET use. This is performance & quality questions that I want to raise on forums (check this thread http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=329219&SiteID=1), but the problem is that many people do not understand importance of this questions .

    That code was in C#, but it's easy to translate to VB (I'm not very familiar with VB syntax)

    There is problems with making editors like you doing now, try answer this questions:
    1. what you will do when you need to change map size
    2. can you scroll your map
    3. how your code detect mouse clicks and change map
    4. can you zoom
    5. can you add layers (ground, trees, buildings...) or draw things across cells



  • Jean-Marie Epitalon

    "There is something wrong with the PictureBox(i).image" doesn't really give much idea of the problem you are having.

    Have you created an Array called PictureBox. Have you added all your picture boxes to it. What error message are you getting.

    Dave


  • Kasracer

    Hmmmmmmm
    You're right......

    1
    2 nope
    3 don't know it yet, friend already did that, try to use his code as basic for mine
    4 nope
    5 you can make something solid, so player can't walk over it

    I guess i really need to translate the C# code.....

    Thanks.....

  • n_sateesh

    i just make them in designer mode, but because i'm making a map editor, i changed some settings so that i can put the pictureboxes side by side
    The settings i changed:
    tools / options / windows form designer
    set layoutmode to snaptogrid, and showgrid to true
    I just copy and paste the boxes
    This is how i work

    I hope this is what you want to know S.G.

    Radexxion

    Btw, thanks for your reply Dave
    It was very usefull, and it works (of course) :P



  • Nova SS

    Radexxion, can you post code or description how you create your picture boxes - in designer or in code


  • micca

    Creating 400 controls in designer Hmm... Really not good. Because Windows use much resources for each control, your application is longer to start and need more CPU/Memory.

    Typically to create map editor you need to create own control based on ScrollableControl, then override OnPaint() method and draw images by yourself:

    for (int x = 0; x < 20; x++)
    {
    for (int y = 0; y < 30; y++)
    {
    e.Graphics.DrawImage(Map[x, y], x * CellSize.Width, y * CellSize.Height);
    }
    }

    This sample draw 30 lines of 20 cells. Map is matrix with Image objects, CellSize is Size structure with size of one cell. Of course you need add here scrolling support code line and visibility check code line. This will work much faster than creating so many controls.


  • SUpton

    It works fine for as far is i know
    A friend of mine created the same thing, but then with vb6
    Because of that i know it will work and wont take a lot of CPU/Memory
    By the way, the code you posted is a C/C++ and i'm using VB


  • David Bachy

    Sure. You will need "Windows Application" project. Then you will need to design your application and implement. What kind of editor you have I mean your map is matrix with WxH cells What is inside each matrix cell

    Generally you will need create MapEditorControl, add new CustomControl (Ctrl+Shift+A), it will open in designer, press F7 to view code. There you will find Paint() method in which you will need to paint your map. Just add this line for now to see all works:

    pe.Graphics.DrawEllipse(Pens.Red, ClientRectangle);

    Next step is to drop MapEditorControl on MainForm. Build project, open MainForm in designer, then in Toolbox find MapEditorControl and drop on the form and set Dock = Fill in properties. You should see red ellipse.

    If you want you may contact me with Messenger (see my profile) so I can help you.


  • WoZoI

    First off all thanks for your reply
    It says: Picurebox is a type and cannot be used as an expresion
    I don't know exactly what an array is, so i guess i don't have one.
    And adding all my picboxes to it will take a lot of time, there are almost 400 picboxes

    I hope you can help me now,

    Radexxion






  • David Homer

    (3) is possible to answer generally - must trap events in each picture box related to mouse (400 event handlers - again waste of resource) and must translate from picture box to map you have. But this is again not a solution, because you can't allow user to press mouse button, then move across few cells and release it (to draw wall or road for example).

    It's not so hard to create own control, draw there own map (including multiple layers and lines across cells) and handle mouse events. If you need - I can help, but all my samples in C# .


  • derryckjw

    i just downloaded visual C# 2005 and i'll try to learn C#
    I guess it's not difficult
    Can you plz post some usefull codes with description, so i can make a mapeditor and learn C#
    Thanks in forward,

    Radexxion


  • Need help with for and pictureboxes