C# - Creating PNG PictureBoxes function problem

Hi guys 'n gals,

I need some help with my C# function....

It is meant to:

connect to connect to MySQL (this works)
query a table (this works)
loop through the results and create picture boxes from *.png files located in a subfolder in the bin\ directory titled "node_graphics" (does not work)

No errors are thrown, it merely does not work. Could somebody take a look and tell me what I am doing wrong please?


Cheers!



CODE:



private void PopulateNodeCat()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            DataSet ds = new DataSet();
            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblNodeCat", conn);
            adapter.Fill(ds);
            conn.Close();

            int intX = panel2.Left + 2;
            int intY = panel2.Top + 2;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                PictureBox pb;
                pb = new PictureBox();
                pb.Name = "NodeCatPic_" + dr["UID"].ToString();
                pb.Height = 100;
                pb.Width = 100;
                pb.SizeMode = PictureBoxSizeMode.StretchImage;
                pb.Location = new Point(intX, intY);
                pb.Image = Image.FromFile("node_graphics\\" + dr["Filename"]);

                this.panel2.Controls.Add(pb);

                if (intY == panel2.Top + 2)
                {
                    intY = intY + 105;
                }
                else
                {
                    intY = panel2.Top + 2;
                    intX = intX + 105;
                }
            }
        }
LVL 4
Cyber-DrugsAsked:
Who is Participating?
 
YttribiumConnect With a Mentor Commented:
No problem, I was in a rush when I wrote the answer, so in benefit to future readers that may look at this:

 The Location(x,y) coordinates of the pictureboxes he attached to the Panel, is relative to the panel.  So 0,0 would be top left, inside the panel, not of the main form.
0
 
YttribiumCommented:
Can elaborate what you mean by "does not work"?  I tried your code and it shows the images just fine.  It appears to work pretty good.
What are you using?

I used VS 2005, .NET 2.0.  
0
 
Cyber-DrugsAuthor Commented:
Sorry for not being specific enough, basically I meant the images do not display...

I am using VS 2005, .NET 2.0

Same as yourself, I am a bit lost on what to try now that you said it works for you...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
YttribiumCommented:
Do the images actually work in say, paint or I.E?
0
 
Cyber-DrugsAuthor Commented:
They do, yes. I am basically porting over an application I wrote in HTML/JavaScript/AJAX into a C# application, and these images are a copy of the ones used in my existing system, which work perfectly.


Just to confirm, this is what the directory structure is at the moment:


bin\myApp.exe
bin\node_graphics\pic1.png
bin\node_graphics\pic2.png
0
 
AlexNekCommented:
Did you try to use jpg or gif format?
0
 
Cyber-DrugsAuthor Commented:
I just tried with JPG, and it still gave the same result. Just added a line to that code to add the filepaths to a multiline textbox, and got the following results:

node_graphics\workstation_network.png
node_graphics\PCI-card_network.png
node_graphics\laptop.png
node_graphics\office-building.png
node_graphics\houses.png
node_graphics\printer2.png
node_graphics\printer_network.png
node_graphics\workstation2.png
node_graphics\pda_write.png
node_graphics\modem_earth.png
node_graphics\cubes.png
node_graphics\server_network.png
node_graphics\cubes.png
node_graphics\test.jpg


Any other ideas?
0
 
YttribiumCommented:
Did you override the on_paint function at any point?
0
 
AlexNekCommented:
It is possible that you have a troube in other part of code. Try to create a sample from scratch with your written /here/ functrion.
0
 
Cyber-DrugsAuthor Commented:
I've not overwritten the function as it's a fresh project, this is the only bit of code inside it at the moment.


I have althought just tried something, which displayed the problem (now I need to know how to fix it). If you change:

this.panel2.Controls.Add(pb);

for

this.Controls.Add(pb);

Then it all works, so any ideas on how to fix it so that the images are rendered inside panel2, AND display?

Cheers!
0
 
YttribiumCommented:
What are the properties of the panel?
0
 
Cyber-DrugsAuthor Commented:
           //
            // panel2
            //
            this.panel2.Location = new System.Drawing.Point(620, 27);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(160, 250);
            this.panel2.TabIndex = 2;


Pretty much standard from what I can see...
0
 
YttribiumCommented:
Is this in the code as well?
this.Controls.Add(this.panel2);
I'm sure it would be, but your problem is extraordinary... so anything is possible.
0
 
Cyber-DrugsAuthor Commented:
It is indeed, it's really baffling... I don't see why it would be hidden...

Can I add scrollbars to my panel to make sure that the images are not maybe displayed off the screen?
0
 
YttribiumCommented:
Try do this code on a new form, with a single panel, see if it's even possible.  If this works, you can compare.  If it still doesn't work, then possibly you may not have the latest .NET 2.0 patches...
0
 
YttribiumCommented:
I found your problem, I'm working on a solution quick.  Give me a few minutes please.
0
 
YttribiumCommented:
It's your positioning of X,Y inside the panel, it's relative to the panel, not the screen or form.
0
 
Cyber-DrugsAuthor Commented:
AHA! That would explain everything, cheers Yttribium!!

Problem solved!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.