Click here to Skip to main content
15,796,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I need to create a sort of grid (with table, line shapes or whatever) that the number of rows and columns in it changes according to user input.
Then, I need to create and move several picture box above the grid lines (also created according to user demands).

I tried to use table and above picture box but its not possible because I cant put the picture box and move them to which direction I want. also, I cant put 2 picture box at the same cell.

* the grid only serve as a kind of route for the picture boxes to move in.

can you help me find other way to do that?


I used TalbeLayoutPanel - anyway as I wrote - I don't care which object to use becuse it only serves as a background.
the picturebox need to be able to move in the form so I can't use panel because the panel in a table is used in each cell separately so I won't be able to create a movement..
Updated 29-Aug-12 21:38pm
lukeer 30-Aug-12 2:26am    
Please use the "Improve question" link to provide further information.

You tried to use "table". What is that? Could it be TalbeLayoutPanel? Or DataGridView?

Two PictureBoxes in one cell usually can achieved by putting them on one Panel and putting that into the targeted cell.

For the other points, please see my comment and elaborate on your question.

You can move pictureboxes around in a TableLayoutPanel just by defining the target column and row. For several of them in one cell, though, you will have to implement some logic to place them on a shared panel within the cell.

Since you have to implement custom logic anyway, you can also throw the whole talbe stuff away, derive a custom control from System.Windows.Forms.Control, override its OnPaint() method and draw every picture wherever you need it at the moment.

This gives you more freedom in picture placement and the ability to move pictures in an animation-ish way.

In Project explorer, you right-click on your project and select "New Class". Call it something meaningful, "PictureBoard.cs", for example.
Visual Studio will create the code file for you and integrate it with your solution.

class PictureBoard

public class PictureBoard : System.Windows.Forms.Control

and voilà: you just derived your custom new PictureBoard class from all control's base class: Control.

Now create your OnPaint() overload. Start typing
"override" plus a spacebar hit and Intellisense should come up with a list of methods you could possibly want to override. Choose OnPaint().
Visual Studio will create a standard OnPaint() override for you:
protected override void OnPaint(PaintEventArgs e)

You can leave base.OnPaint(e); in place. Put your custom painting code after it.
The key to custom painting is the PaintEventArgs e parameter. It holds, among others, an instance of System.Drawing.Graphics called e.Graphics. This is used to draw whatever you need to. Look it up on MSDN[^]

First, you will want to use one of the overloads of DrawRectangle(), DrawLine() and DrawImage().

Build your OnPaint() method in a way that it can be called at any moment since it is unpredictable when that will be the case.
You need to access your images along with positioning data from OnPaint(). Check all data for availability before using it.

Finally, whenever you want your control to update itself, call Invalidate() on it. It's a method inherited from Control that will tell the OS that the control needs to be re-drawn. Whenever Windows finds it suitable, it will then call OnPaint().
Share this answer
danait25 30-Aug-12 2:39am    
answered through the "improve question, thanx
lukeer 30-Aug-12 2:54am    
I updated my solution.
danait25 30-Aug-12 2:57am    
I'm not that familiar with that - how can I override the onpaint method?
I need to do it in a separate class or in the form page?
and if it's possible how can I draw a net or lines with it?
thank you
Could this maybe ListView[^] serve your needs?
Share this answer
danait25 30-Aug-12 3:01am    
I think its the same problem as using a table ..thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900