Connecting Tech Pros Worldwide Help | Site Map

Form Objects in Arrays

Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#1: Sep 13 '09
Hello all, Im quite new to this language, and Im trying to develop a form application, and Im kinda running into a kinda an attempt to bloat the heck out of the code, and trying to find the easiest way to do this.

So this is what I've got. 3 sets of 44 Picture Boxes.
I have 10 different options. Each with an Image array with pre-designated images.
All the Picture Boxes will be setup in the right positions. Just the images with be dynamicly changed based on user selection.

Basicly what I want to do is something like this:
Expand|Select|Wrap|Line Numbers
  1. foreach (int i IN FormPicBox) {
    if ( ImgArray[i] != null ) {
    FormPicBox[i].Image = ImgArray[i];
    }
  2. }
Is the Form Object arrays something just as simple as this:
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.PictureBox[XX]
best answer - posted by GaryTexmo
Quote:

Originally Posted by tlhintoq View Post

Just plain bad. First of all "image.jpg" isn't in any way a valid path to a file. All it can possibly do is fail. If it were a valid path, Bitmap(string Path) keeps a link open to the hard drive file until the application closes. This will cause all kinds of issues if you try to move/rename/delete the file.

Secondly it doesn't account for any time of error compensation. If the file is damage then this entire function breaks.

You should have a dedicated image loading method that can recover from problems, If that method returns an image then you can load it into your list.

Not sure I appreciate your comments here. It was just a snippet of code to demonstrate an example. You didn't need to pick it apart and you certainly didn't need to be so insulting, especially when what you said is not completely true. The following I'll say in my defense, but then I'm bowing out of this thread. Good luck, Samishii.

1. The file path is valid. It will look in the application directory for that file. A more complete solution may include the file path, but for testing purposes this is sufficient. I've just verified that this is the case. It's easy to set up, feel free to try it.

2. I was not aware that Bitmap left an open file handle... fair enough, but allow me to make a simple correction to my code to allow for a copy of the image data to be created.

Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. Bitmap bitmap = new Bitmap("image.jpg");
  3. Image bitmapDataCopy = Image.FromHbitmap(bitmap.GetHbitmap();
  4. imageList.Add(bitmapDataCopy);
I've tested this and no file handles appear to be left.

3. As it was just a snippet of code, I didn't include any try/catch blocks. It seems odd that you even bring this up.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: Sep 13 '09

re: Form Objects in Arrays


Looks about right. You could do with List as well

Expand|Select|Wrap|Line Numbers
  1. List<Image> MyImageList = new List<Image>();
  2. List<PictureBox> MyPictureBoxes = new List<PictureBox>();
  3.  
Lists have a little more brains that an array. They have useful methods that keep you from doing a lot of the grunt work yourself.

Expand|Select|Wrap|Line Numbers
  1. MyImageList.Add(SomeNewImage);
  2. MyImageList.RemoveAt(5);
  3.  
Is a lot easier than the array equivilents where you have to resize the array to add, or worse yet, where you have to loop through the elements moving item 6 into position 5, 7 into 6, 8 into 7 and so on just to take out element 5.

You still reference the elements with braces just as in the array

Expand|Select|Wrap|Line Numbers
  1. MyPictureBoxes[i].image = MyImageList[i];
  2.  
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#3: Sep 16 '09

re: Form Objects in Arrays


>> tlhintoq
Thank you for your post.

I wish it was that simple for my own example. I can't get the variable to hold the form item. This is the only way I can think to do it.
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.PictureBox[] i_pbox = new System.Windows.Forms.PictureBox[] = { };
  2.  
The compiler throws an error saying it not a Type or something.

Also. Haven't a clue how to work with Image Lists. I did a little research. Seems like it doesn't work with Picture Boxes. I'll continue to look. Thank you ahead of time.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#4: Sep 16 '09

re: Form Objects in Arrays


You were close:
Expand|Select|Wrap|Line Numbers
  1.             PictureBox[] MyPictureBoxes = new PictureBox[] { };
  2.  
Or you can make a List of PictureBoxes instead of an array of them
Expand|Select|Wrap|Line Numbers
  1.  List<PictureBox> = new List<PictureBox>();
  2.  
I prefer List<>s to arrays because they dynamically resize as well as having methods for .Add .AddRange .AddAt .Remove .RemoveAt
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#5: Sep 16 '09

re: Form Objects in Arrays


For what it's worth, I'll also throw my lot in for List<>. It's just so much simpler... obviously it has a little more overhead than a straight up array, but 9 times out of 10 (hell, 9.99 times out of 10) it's the better choice.

That and the collections are my favourite classes in .NET, they're just so damn useful! :)
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#6: Sep 16 '09

re: Form Objects in Arrays


Well so far ty for the help on the Image Arrays, I think i've got the handle on the ImageList now. Though now that I look at the original reply, I'm a bit confused
Expand|Select|Wrap|Line Numbers
  1. List<PictureBox> MyPictureBoxes = new List<PictureBox>();
So as well as Image objects, I can put PictureBox objects in the ImageList too? Or am I miss reading this?

Also what <i>using</i> do I need for the List call?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#7: Sep 16 '09

re: Form Objects in Arrays


YOu are misreading it.
You can have a List of images
You can have a list of Pictureboxes
You can have a list of Buttons
They are all separate lists.

If you want to put MyImageList[6] into MyPictureBoxList[4] you can do that.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#8: Sep 16 '09

re: Form Objects in Arrays


If you want to have multiple types in your list (PictureBox objects and Image objects) you can make your list hold objects. Then when you use the list, check the type and act accordingly.

For example...

Expand|Select|Wrap|Line Numbers
  1.             List<object> myList = new List<object>();
  2.  
  3.             myList.Add(new PictureBox());
  4.             myList.Add(new Bitmap(32, 32));
  5.             myList.Add(new List<string>());
  6.  
  7.             foreach (object obj in myList)
  8.             {
  9.                 if (obj is PictureBox) Console.WriteLine("Object is a PictureBox!");
  10.                 else if (obj is Bitmap) Console.WriteLine("Object is a Bitmap!");
  11.                 else Console.WriteLine("Object not recognized, ignoring!");
  12.             }
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#9: Sep 16 '09

re: Form Objects in Arrays


Quote:

Originally Posted by GaryTexmo View Post

If you want to have multiple types in your list (PictureBox objects and Image objects) you can make your list hold objects. Then when you use the list, check the type and act accordingly.

For example...
(note, assuming there are blank constructors for PictureBox and Image)

Expand|Select|Wrap|Line Numbers
  1. List<object> myList = new List<object>();
  2.  
  3. myList.Add(new PictureBox());
  4. myList.Add(new Image());
  5.  
  6. foreach (object obj in myList)
  7. {
  8.   PictureBox objAsPictureBox = obj as PictureBox;
  9.   if (objAsPictureBox != null) { /* do something */ }
  10.  
  11.   Image objAsImage = obj as Image;
  12.   if (objAsImage != null) { /* do something */ }
  13. }


Is there anything to be gained by combining two different object types into one list?

I guess I like things tidy.
I can have a list of 10 picture boxes
I can have a list of 20 "user" images
I can have a list of 30 "Supervisor" images
I can have a list of 5 "Administrator" images

If I want to know how many Supervisor images I can just check that list:
SupervisorImageList.Count

If you put everything into one massive list it seems like a nightmare to manage
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#10: Sep 16 '09

re: Form Objects in Arrays


Wow. Thanks for the quick replys. Now....
Quote:
Also what using do I need for the List call?
PS THANKS FOR THE HELP!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#11: Sep 16 '09

re: Form Objects in Arrays


List on MSDN

List is part of the System.Colllections.Generic namespace
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#12: Sep 16 '09

re: Form Objects in Arrays


Quote:

Originally Posted by tlhintoq View Post

Is there anything to be gained by combining two different object types into one list?

I guess I like things tidy.
I can have a list of 10 picture boxes
I can have a list of 20 "user" images
I can have a list of 30 "Supervisor" images
I can have a list of 5 "Administrator" images

If I want to know how many Supervisor images I can just check that list:
SupervisorImageList.Count

If you put everything into one massive list it seems like a nightmare to manage

Yes and no... it depends on the application. I figured I'd throw it out there. I like to use generically typed lists when I don't always know what I'm going to have, but want to process it all at the same time. With several list objects I need to keep track of those variables and process them separately. If I want to add a new "supported type" I need to add a new list, as well as the logic to loop through the list and use it wherever needed. With the generic list, I can just put it in and add a new clause to my if statement, or case to my switch statement (however I implement it).

I think the difference is if you care about what's in your list. If you do, you're best off keeping separate lists (so you can do exactly as you've suggested, get the number of supervisor images for example). But if you don't care, and just want a list of a bunch of different image types (ie, a photo album containing different image types, but logically grouped as one album) then a generic list tends to work out better.

Another example is how a Form keeps a list of Controls, but in that list you can have buttons, labels, etc... I just picked object since it's easiest, there might be a higher level common class for image types, at least built in .NET ones.

That was a fun digression ;)
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#13: Sep 22 '09

re: Form Objects in Arrays


Hello again guys. Thanks for the above help.
But alas, Im back again, and again in need of help.
tlhintoq I've been around the MSDN for the List<> feature a few times now, and I've tried to use the Examples giving to write proper code. In the example though they were using a Console application rather then Forms application. Though I don't know if that would make a difference, but I do know this...

I made sure I have using System.Collections.Generic; in the header.

Expand|Select|Wrap|Line Numbers
  1. List<Image> ImgList = new List<Image>();
  2. ImgList.Add("image.jpg");
But I can not seem to get the List to do the Add() function.
Quote:
Invalid token '(' in class, strut, or interface deceleration
. The Program will build without errors with the List deceleration just fine. Its errors at the first ( in the Add function.

When I try to use the AddRange() in the initial deceleration like this...
Expand|Select|Wrap|Line Numbers
  1. Image[] tempIconArray = { "image1.jpg","image2.jpg" };
  2. List<Image> ImgList = new List<Image>(tempIconArray);
Quote:
A field initializer cannot reference the nonstatic field, method, or property 'ImageData.PublicData.tempIconArray'
The code bit is exactly the same as the MSDN example shows for the AddRange().
From MSDN on List<T>.AddRange()
Expand|Select|Wrap|Line Numbers
  1. string[] input = {"Brachiosaurus","Amargasaurus","Mamenchisaurus" };
  2. List<string> dinosaurs = new List<string>(input);
Unless its the Image type thats making the problem. Or my compiler... At which point I don't know what to do...
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#14: Sep 22 '09

re: Form Objects in Arrays


Quote:
Image[] tempIconArray = { "image1.jpg","image2.jpg" };
Image[] would have to be an array of images. You've given it an array of strings.

Quote:
List<Image> ImgList = new List<Image>();
ImgList.Add("image.jpg");
Again, you have a List of images, but you are trying to add a string.

You either need to a List<string> to add the string to, or add an image to your List<image>
Keep in mind if you create a List<Image> with a lot of images you eat your memory pretty quickly. Most times you want to keep a list of paths to the images and only load as needed.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#15: Sep 22 '09

re: Form Objects in Arrays


The string reference was only from the MSDN reference. I wasn't planing on using strings here. I was just mentioning that I for some reason the List<> wasn't working for me for w/e reason... I may have to completly start a new Project. sigh.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#16: Sep 22 '09

re: Form Objects in Arrays


***
After studying the auto generated code of the Designer. I got past my own problem. For now...
***
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#17: Sep 22 '09

re: Form Objects in Arrays


Quote:
The string reference was only from the MSDN reference. I wasn't planing on using strings here
If you want help with your code that isn't working it would help if you would supply your code that isn't working.

Saying your code doesn't work, the supply bits of code from MSDN doesn't help anyone here to help you.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#18: Sep 22 '09

re: Form Objects in Arrays


Well, the MSDN example is using a list of strings, you're using a list of images, that's the difference :)

Since Image is an abstract class (I think... a base type at any rate), you can't instantiate a new object of type Image. But you can put Bitmaps in there, which is just an Image storage class.

Instead of...

Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. imageList.Add("image.jpg");
... try ...

Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. imageList.Add(new Bitmap("image.jpg"));
As tlhintoq mentioned though, this will eat up your memory. There are alternatives, but see if you can get it working for now, and we can talk optimizations after.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#19: Sep 22 '09

re: Form Objects in Arrays


Quote:

Originally Posted by Samishii23 View Post

Off subject since the problem just came up for me...

I have a win app, which is pretty much brand new. In the Main() function we make a panel object named "FPan", and Main() can do with it whatever it wants...
Have a picturebox which were generated from the Main() function.

But I have an Event Function in the same class. A mouse hover event catcher "PBImg_MouseHover(object sender, System.EventArgs e)"
In this function I want to play with the properties of the FPan from the Main() function. Compiler says FPan isn't an object... What do I do.

Don't muddy up the thread with two completely different problems which even you say "is completely off topic." You need to start a new thread with the new question/issue.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#20: Sep 22 '09

re: Form Objects in Arrays


I'm going to start fresh with new project since the List<> works in a fresh project and not the original one.

Feel free to close this thread.
Thanks for the help of you both. I'll probally see you around later with a more Q's in another thread with more details of the problems and problematic code.
Sorry to be such a n00b tlhintoq.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#21: Sep 22 '09

re: Form Objects in Arrays


Quote:
Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. imageList.Add(new Bitmap("image.jpg"));
Just plain bad. First of all "image.jpg" isn't in any way a valid path to a file. All it can possibly do is fail. If it were a valid path, Bitmap(string Path) keeps a link open to the hard drive file until the application closes. This will cause all kinds of issues if you try to move/rename/delete the file.

Secondly it doesn't account for any time of error compensation. If the file is damage then this entire function breaks.

You should have a dedicated image loading method that can recover from problems, If that method returns an image then you can load it into your list.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#22: Sep 22 '09

re: Form Objects in Arrays


Quote:

Originally Posted by Samishii23 View Post

I'm going to start fresh with new project since the List<> works in a fresh project and not the original one.

Feel free to close this thread.
Thanks for the help of you both. I'll probally see you around later with a more Q's in another thread with more details of the problems and problematic code.
Sorry to be such a n00b tlhintoq.

We are all noobies helping each other out. That's why it is called a 'community'. Don't apologize.

If you choose to start a new project that's up to you. If you want to actually *understand* why it doesn't work in your current project, feel free to post the actual code that's failing and we will see if we can find a reason.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#23: Sep 22 '09

re: Form Objects in Arrays


Good point... Lol. Here goes nothing. One reason I would like to start over, is all that is from when I first started learning the language, and it seems to me is over-bloated, and repeative... Might just be me.

Also all the objects in the Form were generated using the Designer...

Main.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Windows.Forms;
  7. using TCImg = TalentCalc.Properties.Resources;
  8. using DLLClass;
  9.  
  10. namespace TalentCalc
  11. {
  12.     public partial class TalentCalc : Form
  13.     {
  14.         private void TalentCalc_Load(object sender, EventArgs e)
  15.         {
  16.             load_loadicons();
  17.             load_hideall();
  18.             //TalentCalc.ActiveForm.Icon = new Icon(ClassDLL.GetProjectIcon());
  19.  
  20.             // PictureBox "FrontDisplay" Will be the "Opaque" BAckset for the icons.
  21.             // Need to re-size to (895, 583) and probally will need to Re-Draw
  22.         }
  23.         List<PictureBox> L_IconBox = new List<PictureBox>();
  24.  
  25.  
  26.  
  27.  
  28.         #region Probally won't work
  29.         string[] Tree1Icons = new string[] {
  30.             "icon_1_1_1","icon_1_1_2","icon_1_1_3","icon_1_1_4",
  31.             "icon_1_2_1","icon_1_2_2","icon_1_2_3","icon_1_2_4",
  32.             "icon_1_3_1","icon_1_3_2","icon_1_3_3","icon_1_3_4",
  33.             "icon_1_4_1","icon_1_4_2","icon_1_4_3","icon_1_4_4",
  34.             "icon_1_5_1","icon_1_5_2","icon_1_5_3","icon_1_5_4",
  35.             "icon_1_6_1","icon_1_6_2","icon_1_6_3","icon_1_6_4",
  36.             "icon_1_7_1","icon_1_7_2","icon_1_7_3","icon_1_7_4",
  37.             "icon_1_8_1","icon_1_8_2","icon_1_8_3","icon_1_8_4",
  38.             "icon_1_9_1","icon_1_9_2","icon_1_9_3","icon_1_9_4",
  39.             "icon_1_10_1","icon_1_10_2","icon_1_10_3","icon_1_10_4",
  40.             "icon_1_11_1","icon_1_11_2","icon_1_11_3","icon_1_11_4"
  41.             };
  42.         string[] Tree2Icons = new string[] {
  43.             "icon_2_1_1","icon_2_1_2","icon_2_1_3","icon_2_1_4",
  44.             "icon_2_2_1","icon_2_2_2","icon_2_2_3","icon_2_2_4",
  45.             "icon_2_3_1","icon_2_3_2","icon_2_3_3","icon_2_3_4",
  46.             "icon_2_4_1","icon_2_4_2","icon_2_4_3","icon_2_4_4",
  47.             "icon_2_5_1","icon_2_5_2","icon_2_5_3","icon_2_5_4",
  48.             "icon_2_6_1","icon_2_6_2","icon_2_6_3","icon_2_6_4",
  49.             "icon_2_7_1","icon_2_7_2","icon_2_7_3","icon_2_7_4",
  50.             "icon_2_8_1","icon_2_8_2","icon_2_8_3","icon_2_8_4",
  51.             "icon_2_9_1","icon_2_9_2","icon_2_9_3","icon_2_9_4",
  52.             "icon_2_10_1","icon_2_10_2","icon_2_10_3","icon_2_10_4",
  53.             "icon_2_11_1","icon_2_11_2","icon_2_11_3","icon_2_11_4"
  54.         };
  55.         #endregion
  56.  
  57.         System.Windows.Forms.PictureBox[] pBox = new PictureBox[1];
  58.  
  59.  
  60.         public TalentCalc()
  61.         {
  62.             InitializeComponent();
  63.         }
  64.  
  65.         #region Intialize Druid Trees
  66.  
  67.         #endregion
  68.  
  69.         public void load_loadicons()
  70.         {
  71.             FrontIcon01.Image = ClassDLL.GetClassIcon(0);
  72.             FrontIcon02.Image = ClassDLL.GetClassIcon(1);
  73.             FrontIcon03.Image = ClassDLL.GetClassIcon(2);
  74.             FrontIcon04.Image = ClassDLL.GetClassIcon(3);
  75.             FrontIcon05.Image = ClassDLL.GetClassIcon(4);
  76.             FrontIcon06.Image = ClassDLL.GetClassIcon(5);
  77.             FrontIcon07.Image = ClassDLL.GetClassIcon(6);
  78.             FrontIcon08.Image = ClassDLL.GetClassIcon(7);
  79.             FrontIcon09.Image = ClassDLL.GetClassIcon(8);
  80.             FrontIcon10.Image = ClassDLL.GetClassIcon(9);
  81.  
  82.             classicon1.Image = ClassDLL.GetClassIcon(0);
  83.             classicon2.Image = ClassDLL.GetClassIcon(1);
  84.             classicon3.Image = ClassDLL.GetClassIcon(2);
  85.             classicon4.Image = ClassDLL.GetClassIcon(3);
  86.             classicon5.Image = ClassDLL.GetClassIcon(4);
  87.             classicon6.Image = ClassDLL.GetClassIcon(5);
  88.             classicon7.Image = ClassDLL.GetClassIcon(6);
  89.             classicon8.Image = ClassDLL.GetClassIcon(7);
  90.             classicon9.Image = ClassDLL.GetClassIcon(8);
  91.             classicon10.Image = ClassDLL.GetClassIcon(9);
  92.  
  93.         }
  94.         public void load_hideall()
  95.         {
  96.             tree1_bg.Visible = false;
  97.             icon_1_1_1.Visible = false; icon_1_1_2.Visible = false; icon_1_1_3.Visible = false; icon_1_1_4.Visible = false;
  98.             icon_1_2_1.Visible = false; icon_1_2_2.Visible = false; icon_1_2_3.Visible = false; icon_1_2_4.Visible = false;
  99.             icon_1_3_1.Visible = false; icon_1_3_2.Visible = false; icon_1_3_3.Visible = false; icon_1_3_4.Visible = false;
  100.             icon_1_4_1.Visible = false; icon_1_4_2.Visible = false; icon_1_4_3.Visible = false; icon_1_4_4.Visible = false;
  101.             icon_1_5_1.Visible = false; icon_1_5_2.Visible = false; icon_1_5_3.Visible = false; icon_1_5_4.Visible = false;
  102.             icon_1_6_1.Visible = false; icon_1_6_2.Visible = false; icon_1_6_3.Visible = false; icon_1_6_4.Visible = false;
  103.             icon_1_7_1.Visible = false; icon_1_7_2.Visible = false; icon_1_7_3.Visible = false; icon_1_7_4.Visible = false;
  104.             icon_1_8_1.Visible = false; icon_1_8_2.Visible = false; icon_1_8_3.Visible = false; icon_1_8_4.Visible = false;
  105.             icon_1_9_1.Visible = false; icon_1_9_2.Visible = false; icon_1_9_3.Visible = false; icon_1_9_4.Visible = false;
  106.             icon_1_10_1.Visible = false; icon_1_10_2.Visible = false; icon_1_10_3.Visible = false; icon_1_10_4.Visible = false;
  107.             icon_1_11_2.Visible = false; icon_1_11_3.Visible = false; icon_1_11_4.Visible = false; icon_1_11_1.Visible = false;
  108.  
  109.             icontxt_1_1_1.Visible = false; icontxt_1_1_2.Visible = false; icontxt_1_1_3.Visible = false; icontxt_1_1_4.Visible = false;
  110.             icontxt_1_2_1.Visible = false; icontxt_1_2_2.Visible = false; icontxt_1_2_3.Visible = false; icontxt_1_2_4.Visible = false;
  111.             icontxt_1_3_1.Visible = false; icontxt_1_3_2.Visible = false; icontxt_1_3_3.Visible = false; icontxt_1_3_4.Visible = false;
  112.             icontxt_1_4_1.Visible = false; icontxt_1_4_2.Visible = false; icontxt_1_4_3.Visible = false; icontxt_1_4_4.Visible = false;
  113.             icontxt_1_5_1.Visible = false; icontxt_1_5_2.Visible = false; icontxt_1_5_3.Visible = false; icontxt_1_5_4.Visible = false;
  114.             icontxt_1_6_1.Visible = false; icontxt_1_6_2.Visible = false; icontxt_1_6_3.Visible = false; icontxt_1_6_4.Visible = false;
  115.             icontxt_1_7_1.Visible = false; icontxt_1_7_2.Visible = false; icontxt_1_7_3.Visible = false; icontxt_1_7_4.Visible = false;
  116.             icontxt_1_8_1.Visible = false; icontxt_1_8_2.Visible = false; icontxt_1_8_3.Visible = false; icontxt_1_8_4.Visible = false;
  117.             icontxt_1_9_1.Visible = false; icontxt_1_9_2.Visible = false; icontxt_1_9_3.Visible = false; icontxt_1_9_4.Visible = false;
  118.             icontxt_1_10_1.Visible = false; icontxt_1_10_2.Visible = false; icontxt_1_10_3.Visible = false; icontxt_1_10_4.Visible = false;
  119.             icontxt_1_11_1.Visible = false; icontxt_1_11_2.Visible = false; icontxt_1_11_3.Visible = false; icontxt_1_11_4.Visible = false;
  120.  
  121.             tree2_bg.Visible = false;
  122.             icon_2_1_1.Visible = false; icon_2_1_2.Visible = false; icon_2_1_3.Visible = false; icon_2_1_4.Visible = false;
  123.             icon_2_2_1.Visible = false; icon_2_2_2.Visible = false; icon_2_2_3.Visible = false; icon_2_2_4.Visible = false;
  124.             icon_2_3_1.Visible = false; icon_2_3_2.Visible = false; icon_2_3_3.Visible = false; icon_2_3_4.Visible = false;
  125.             icon_2_4_1.Visible = false; icon_2_4_2.Visible = false; icon_2_4_3.Visible = false; icon_2_4_4.Visible = false;
  126.             icon_2_5_1.Visible = false; icon_2_5_2.Visible = false; icon_2_5_3.Visible = false; icon_2_5_4.Visible = false;
  127.             icon_2_6_1.Visible = false; icon_2_6_2.Visible = false; icon_2_6_3.Visible = false; icon_2_6_4.Visible = false;
  128.             icon_2_7_1.Visible = false; icon_2_7_2.Visible = false; icon_2_7_3.Visible = false; icon_2_7_4.Visible = false;
  129.             icon_2_8_1.Visible = false; icon_2_8_2.Visible = false; icon_2_8_3.Visible = false; icon_2_8_4.Visible = false;
  130.             icon_2_9_1.Visible = false; icon_2_9_2.Visible = false; icon_2_9_3.Visible = false; icon_2_9_4.Visible = false;
  131.             icon_2_10_1.Visible = false; icon_2_10_2.Visible = false; icon_2_10_3.Visible = false; icon_2_10_4.Visible = false;
  132.             icon_2_11_2.Visible = false; icon_2_11_3.Visible = false; icon_2_11_4.Visible = false; icon_2_11_1.Visible = false;
  133.  
  134.             icontxt_2_1_1.Visible = false; icontxt_2_1_2.Visible = false; icontxt_2_1_3.Visible = false; icontxt_2_1_4.Visible = false;
  135.             icontxt_2_2_1.Visible = false; icontxt_2_2_2.Visible = false; icontxt_2_2_3.Visible = false; icontxt_2_2_4.Visible = false;
  136.             icontxt_2_3_1.Visible = false; icontxt_2_3_2.Visible = false; icontxt_2_3_3.Visible = false; icontxt_2_3_4.Visible = false;
  137.             icontxt_2_4_1.Visible = false; icontxt_2_4_2.Visible = false; icontxt_2_4_3.Visible = false; icontxt_2_4_4.Visible = false;
  138.             icontxt_2_5_1.Visible = false; icontxt_2_5_2.Visible = false; icontxt_2_5_3.Visible = false; icontxt_2_5_4.Visible = false;
  139.             icontxt_2_6_1.Visible = false; icontxt_2_6_2.Visible = false; icontxt_2_6_3.Visible = false; icontxt_2_6_4.Visible = false;
  140.             icontxt_2_7_1.Visible = false; icontxt_2_7_2.Visible = false; icontxt_2_7_3.Visible = false; icontxt_2_7_4.Visible = false;
  141.             icontxt_2_8_1.Visible = false; icontxt_2_8_2.Visible = false; icontxt_2_8_3.Visible = false; icontxt_2_8_4.Visible = false;
  142.             icontxt_2_9_1.Visible = false; icontxt_2_9_2.Visible = false; icontxt_2_9_3.Visible = false; icontxt_2_9_4.Visible = false;
  143.             icontxt_2_10_1.Visible = false; icontxt_2_10_2.Visible = false; icontxt_2_10_3.Visible = false; icontxt_2_10_4.Visible = false;
  144.             icontxt_2_11_1.Visible = false; icontxt_2_11_2.Visible = false; icontxt_2_11_3.Visible = false; icontxt_2_11_4.Visible = false;
  145.  
  146.             tree3_bg.Visible = false;
  147.             icon_3_1_1.Visible = false; icon_3_1_2.Visible = false; icon_3_1_3.Visible = false; icon_3_1_4.Visible = false;
  148.             icon_3_2_1.Visible = false; icon_3_2_2.Visible = false; icon_3_2_3.Visible = false; icon_3_2_4.Visible = false;
  149.             icon_3_3_1.Visible = false; icon_3_3_2.Visible = false; icon_3_3_3.Visible = false; icon_3_3_4.Visible = false;
  150.             icon_3_4_1.Visible = false; icon_3_4_2.Visible = false; icon_3_4_3.Visible = false; icon_3_4_4.Visible = false;
  151.             icon_3_5_1.Visible = false; icon_3_5_2.Visible = false; icon_3_5_3.Visible = false; icon_3_5_4.Visible = false;
  152.             icon_3_6_1.Visible = false; icon_3_6_2.Visible = false; icon_3_6_3.Visible = false; icon_3_6_4.Visible = false;
  153.             icon_3_7_1.Visible = false; icon_3_7_2.Visible = false; icon_3_7_3.Visible = false; icon_3_7_4.Visible = false;
  154.             icon_3_8_1.Visible = false; icon_3_8_2.Visible = false; icon_3_8_3.Visible = false; icon_3_8_4.Visible = false;
  155.             icon_3_9_1.Visible = false; icon_3_9_2.Visible = false; icon_3_9_3.Visible = false; icon_3_9_4.Visible = false;
  156.             icon_3_10_1.Visible = false; icon_3_10_2.Visible = false; icon_3_10_3.Visible = false; icon_3_10_4.Visible = false;
  157.             icon_3_11_2.Visible = false; icon_3_11_3.Visible = false; icon_3_11_4.Visible = false; icon_3_11_1.Visible = false;
  158.  
  159.             icontxt_3_1_1.Visible = false; icontxt_3_1_2.Visible = false; icontxt_3_1_3.Visible = false; icontxt_3_1_4.Visible = false;
  160.             icontxt_3_2_1.Visible = false; icontxt_3_2_2.Visible = false; icontxt_3_2_3.Visible = false; icontxt_3_2_4.Visible = false;
  161.             icontxt_3_3_1.Visible = false; icontxt_3_3_2.Visible = false; icontxt_3_3_3.Visible = false; icontxt_3_3_4.Visible = false;
  162.             icontxt_3_4_1.Visible = false; icontxt_3_4_2.Visible = false; icontxt_3_4_3.Visible = false; icontxt_3_4_4.Visible = false;
  163.             icontxt_3_5_1.Visible = false; icontxt_3_5_2.Visible = false; icontxt_3_5_3.Visible = false; icontxt_3_5_4.Visible = false;
  164.             icontxt_3_6_1.Visible = false; icontxt_3_6_2.Visible = false; icontxt_3_6_3.Visible = false; icontxt_3_6_4.Visible = false;
  165.             icontxt_3_7_1.Visible = false; icontxt_3_7_2.Visible = false; icontxt_3_7_3.Visible = false; icontxt_3_7_4.Visible = false;
  166.             icontxt_3_8_1.Visible = false; icontxt_3_8_2.Visible = false; icontxt_3_8_3.Visible = false; icontxt_3_8_4.Visible = false;
  167.             icontxt_3_9_1.Visible = false; icontxt_3_9_2.Visible = false; icontxt_3_9_3.Visible = false; icontxt_3_9_4.Visible = false;
  168.             icontxt_3_10_1.Visible = false; icontxt_3_10_2.Visible = false; icontxt_3_10_3.Visible = false; icontxt_3_10_4.Visible = false;
  169.             icontxt_3_11_1.Visible = false; icontxt_3_11_2.Visible = false; icontxt_3_11_3.Visible = false; icontxt_3_11_4.Visible = false;
  170.         }
  171.         public void select_hidefront()
  172.         {
  173.             // Move and Hide all of the "Front" images that are displayed at program startup
  174.  
  175.             // Hide
  176.             FrontIcon01.Visible = false;
  177.             FrontIcon02.Visible = false;
  178.             FrontIcon03.Visible = false;
  179.             FrontIcon04.Visible = false;
  180.             FrontIcon05.Visible = false;
  181.             FrontIcon06.Visible = false;
  182.             FrontIcon07.Visible = false;
  183.             FrontIcon08.Visible = false;
  184.             FrontIcon09.Visible = false;
  185.             FrontIcon10.Visible = false;
  186.             FrontLabel.Visible = false;
  187.             FrontDisplay.Visible = false;
  188.  
  189.             // Resize
  190.             FrontIcon01.Size = new System.Drawing.Size(1, 1);
  191.             FrontIcon02.Size = new System.Drawing.Size(1, 1);
  192.             FrontIcon03.Size = new System.Drawing.Size(1, 1);
  193.             FrontIcon04.Size = new System.Drawing.Size(1, 1);
  194.             FrontIcon05.Size = new System.Drawing.Size(1, 1);
  195.             FrontIcon06.Size = new System.Drawing.Size(1, 1);
  196.             FrontIcon07.Size = new System.Drawing.Size(1, 1);
  197.             FrontIcon08.Size = new System.Drawing.Size(1, 1);
  198.             FrontIcon09.Size = new System.Drawing.Size(1, 1);
  199.             FrontIcon10.Size = new System.Drawing.Size(1, 1);
  200.             FrontLabel.Size = new System.Drawing.Size(1, 1);
  201.             FrontDisplay.Size = new System.Drawing.Size(1, 1);
  202.  
  203.             // Move
  204.             FrontIcon01.Location = new System.Drawing.Point(0, 0);
  205.             FrontIcon02.Location = new System.Drawing.Point(0, 0);
  206.             FrontIcon03.Location = new System.Drawing.Point(0, 0);
  207.             FrontIcon04.Location = new System.Drawing.Point(0, 0);
  208.             FrontIcon05.Location = new System.Drawing.Point(0, 0);
  209.             FrontIcon06.Location = new System.Drawing.Point(0, 0);
  210.             FrontIcon07.Location = new System.Drawing.Point(0, 0);
  211.             FrontIcon08.Location = new System.Drawing.Point(0, 0);
  212.             FrontIcon09.Location = new System.Drawing.Point(0, 0);
  213.             FrontIcon10.Location = new System.Drawing.Point(0, 0);
  214.             FrontLabel.Location = new System.Drawing.Point(0, 0);
  215.             FrontDisplay.Location = new System.Drawing.Point(0, 0);
  216.         }
  217.  
  218.         // Display Death Knight Class
  219.         private void FrontIcon01_Click(object sender, EventArgs e)
  220.         {
  221.             select_hidefront();
  222.         }
  223.  
  224.         // Display Druid Class
  225.         private void FrontIcon02_Click(object sender, EventArgs e)
  226.         {
  227.             select_hidefront();
  228.             DruidNS.DruidClass.InitDruid();
  229.         }
  230.     }
  231. }
  232.  
  233. namespace ImageData
  234. {
  235.     class PublicData
  236.     {
  237.  
  238.         Image[] tempIconArray = {
  239.             new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\druid_1_1_2.png"),
  240.             new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\druid_1_1_3.png"),
  241.             new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\druid_1_2_1.png"),
  242.             new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\druid_1_2_2.png"),
  243.             new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\druid_1_2_4.png")
  244.         };
  245.  
  246.         List<Image> DruidIconList = new List<Image>(tempIconArray);
  247.         //DruidIconList.AddRange(tempIconArray);
  248.  
  249.  
  250.  
  251.         public static Bitmap GrayImg(Bitmap original)
  252.         {
  253.             Bitmap newBitmap = new Bitmap(original.Width, original.Height);
  254.             Graphics g = Graphics.FromImage(newBitmap);
  255.             ColorMatrix colorMatrix = new ColorMatrix(
  256.                 new float[][]
  257.                 {
  258.                     new float[] {.3f, .3f, .3f, 0, 0},
  259.                     new float[] {.59f, .59f, .59f, 0, 0},
  260.                     new float[] {.11f, .11f, .11f, 0, 0},
  261.                     new float[] {0, 0, 0, 1, 0},
  262.                     new float[] {0, 0, 0, 0, 1}
  263.                 });
  264.             ImageAttributes attributes = new ImageAttributes();
  265.             attributes.SetColorMatrix(colorMatrix);
  266.             g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
  267.             g.Dispose();
  268.             return newBitmap;
  269.         }
  270.     }
  271. }
ClassLib.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace DLLClass
  8. {
  9.     public class ClassDLL
  10.     {
  11.         List<Image> DruidTalentIcons = new List<Image>();
  12.         List<PictureBox> DruidTalentPB = new List<PictureBox>();
  13.  
  14.  
  15.  
  16.  
  17.  
  18.         public static Icon GetProjectIcon()
  19.         {
  20.             Icon ico = new Icon(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img\warcraft2.ico");
  21.             return ico;
  22.         }
  23.  
  24.         public static Image GetClassIcon( int CIco )
  25.         {
  26.             string[] ImgLoc = new string[] { "DeathKnight", "Druid", "Hunter", "Mage", "Paladin", "Priest", "Rogue", "Shaman", "Warlock", "Warrior" };
  27.             Image ReturnIcon = new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img\Icon"+ImgLoc[CIco]+".png");
  28.             return ReturnIcon;
  29.         }
  30.  
  31.         public Image GetClassBG(int C, int CBg)
  32.         {
  33.             Image[] ReturnDruid = new Image[] {
  34.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\DruidBalance.jpg"),
  35.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\DruidFeralCombat.jpg"),
  36.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\DruidRestoration.jpg")
  37.             };
  38.             Image[] ReturnDK = new Image[] {
  39.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-dk\DeathKnightBlood.jpg"),
  40.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-dk\DeathKnightFrost.jpg"),
  41.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-dk\DeathKnightUnholy.jpg")
  42.             };
  43.             switch (C)
  44.             {
  45.                 case 0: return ReturnDK[CBg];
  46.                 case 1: return ReturnDruid[CBg];
  47.                 default: return null;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#24: Sep 22 '09

re: Form Objects in Arrays


Quote:
Good point... Lol. Here goes nothing. One reason I would like to start over, is all that is from when I first started learning the language, and it seems to me is over-bloated, and repeative... Might just be me.

Also all the objects in the Form were generated using the Designer...
Sometimes starting over is a good thing for exactly the reasons you mention. Sometimes its faster/easier/better to start over so you can start a clean project.

But starting new project because you can't find the problem in a current project is bad habit. What happens when you have a very elaborate project with a problem you can't find?

For your new project here are a few tips:

Quote:
Expand|Select|Wrap|Line Numbers
  1. classicon1.Image = ClassDLL.GetClassIcon(0);
  2.             classicon2.Image = ClassDLL.GetClassIcon(1);
  3.             classicon3.Image = ClassDLL.GetClassIcon(2);
  4.             classicon4.Image = ClassDLL.GetClassIcon(3);
  5.             classicon5.Image = ClassDLL.GetClassIcon(4);
  6.             classicon6.Image = ClassDLL.GetClassIcon(5);
  7.             classicon7.Image = ClassDLL.GetClassIcon(6);
  8.             classicon8.Image = ClassDLL.GetClassIcon(7);
  9.             classicon9.Image = ClassDLL.GetClassIcon(8);
  10.             classicon10.Image = ClassDLL.GetClassIcon(9);
  11.  
These long lists of things really should be lists and loops

Expand|Select|Wrap|Line Numbers
  1. for (int Index = 0; Index< ClassIconList.Count; Index ++)
  2. {
  3.    ClassIconList[Index].Image = ClassDll.GetClassIcon(Index);
  4. }
  5.  
Stuff like this
Quote:
classicon1.Image = ClassDLL.GetClassIcon(0)
is going to confuse the heck out of you. Try to stick with everything indexing the same way. If your list is zero indexed, then stick with that, but item 1 of one list to item 0 of another is a painful way to go.

Quote:
Expand|Select|Wrap|Line Numbers
  1.                 new Bitmap(@"C:\Documents and Settings\Samishii\Desktop\WoW Talent Calc\TalentCalc\ClassDLL\img-druid\DruidBalance.jpg"),
  2.  
A) Will keep an open link to file on harddrive
B) What happens when you load this on another machine, path's change etc?
You should either make these images part of the resources of the application, or part of a folder in the installed application path. That way you can always reference that known location.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#25: Sep 22 '09

re: Form Objects in Arrays


Quote:

Originally Posted by tlhintoq View Post

These long lists of things really should be lists and loops

That is the EXACT reason for me starting this thread on this website. =P
Because when I originally started I didn't know jack about C#. Also I needed help with it. Here I am pretty well learned after all the Q & A going on.

Quote:

Originally Posted by tlhintoq View Post

A) Will keep an open link to file on harddrive
B) What happens when you load this on another machine, path's change etc?
You should either make these images part of the resources of the application, or part of a folder in the installed application path. That way you can always reference that known location.

I have already done the "Embedded Resource" with all my images, it took a while but I managed to get it into a seperate .dll with the help of GaryTexomo on another thread. (Also my friend is eagerly awaiting the deployment of a usable version of this so hes already seen a few copies of the .exe and hasn't had a problem with the images)

I've wanted to have basicly a massive loop for the deployment of these images, since there will be 10 sets of images, over 132 PictureBoxes. While the processing of the loop for the exchange will be slow because of all the images but it'll be easier work on my side.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#26: Sep 22 '09

re: Form Objects in Arrays


Quote:

Originally Posted by tlhintoq View Post

Just plain bad. First of all "image.jpg" isn't in any way a valid path to a file. All it can possibly do is fail. If it were a valid path, Bitmap(string Path) keeps a link open to the hard drive file until the application closes. This will cause all kinds of issues if you try to move/rename/delete the file.

Secondly it doesn't account for any time of error compensation. If the file is damage then this entire function breaks.

You should have a dedicated image loading method that can recover from problems, If that method returns an image then you can load it into your list.

Not sure I appreciate your comments here. It was just a snippet of code to demonstrate an example. You didn't need to pick it apart and you certainly didn't need to be so insulting, especially when what you said is not completely true. The following I'll say in my defense, but then I'm bowing out of this thread. Good luck, Samishii.

1. The file path is valid. It will look in the application directory for that file. A more complete solution may include the file path, but for testing purposes this is sufficient. I've just verified that this is the case. It's easy to set up, feel free to try it.

2. I was not aware that Bitmap left an open file handle... fair enough, but allow me to make a simple correction to my code to allow for a copy of the image data to be created.

Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. Bitmap bitmap = new Bitmap("image.jpg");
  3. Image bitmapDataCopy = Image.FromHbitmap(bitmap.GetHbitmap();
  4. imageList.Add(bitmapDataCopy);
I've tested this and no file handles appear to be left.

3. As it was just a snippet of code, I didn't include any try/catch blocks. It seems odd that you even bring this up.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#27: 3 Weeks Ago

re: Form Objects in Arrays


I hate to bring this thread but up from the depths of the last 20 pages. lol. But...

Problem... No compiler errors. No run time errors. But, no images are being seen on the form.

_gd is an instance of a text / int32 data class holding default values for the programs usage.

Expand|Select|Wrap|Line Numbers
  1. public void StartUp() {
  2.   // StartUp() - Shows the 10 Class Icons for user to select
  3.   PictureBox[] TempPB = new PictureBox[10];
  4.   int i = 0;
  5.  
  6.   foreach (int TempY in _gd.SUIconY)
  7.     foreach (int TempX in _gd.SUIconX) {
  8.       TempPB[i] = new PictureBox();
  9.       TempPB[i].Location = new Point(TempX, TempY);
  10.       TempPB[i].Size = new Size(64, 64);
  11.       TempPB[i].Image = new Bitmap(_gd.DirOther + _gd.ClassIcon[i]);
  12.       TempPB[i].Visible = true;
  13.       TempPB[i].BringToFront();
  14.       Controls.Add(TempPB[i]);
  15.       i++;
  16.       }
  17.     }
  18.   }
I've ran most of the code through a MessageBox to see if any of the dynamic data being seen by the PictureBox code was wrong in anyway. So far it hasn't been. So at this point. I'm lost. =\

Suggestions or questions?
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#28: 3 Weeks Ago

re: Form Objects in Arrays


Hmmm, have you tried throwing a Console.WriteLine(_gd.DirOther + _gd.ClassIcon[i]) in there to see if the path is correct? Maybe it's not finding an image? I'm not sure offhand (you can test) what the Bitmap constructor returns when it can't make an image... I'd guess null but I'm not sure. If it is null, does a PictureBox with a null image work and just not show anything? (again, you can test that fairly quickly)
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#29: 3 Weeks Ago

re: Form Objects in Arrays


I looked through all the File Paths. Nothing was out of place.

Just tried to implement a <List> to do the image storage instead. Same thing. Maybe it is the Bitmap call... Hmm. =\
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#30: 3 Weeks Ago

re: Form Objects in Arrays


The paths on disk might be right, but is the file path going into the Bitmap class correct, or is that what you meant?

Also, you could try verifying the return of the Bitmap class itself. Is it coming back as null?

I definitely think the problem isn't whether or not it's an array or a List here... if no image shows up the best culprit would be the actual image. You're explicitly setting visible to true, so it's not that.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#31: 3 Weeks Ago

re: Form Objects in Arrays


I think I know what the problem is, give me a bit to work it out.

Nevermind... I'm not sure what the problem is, but I can reproduce your issue. It looks like it has to do with creating a PictureBox programatically. I'll have to look into this, but here's what I'm seeing.

If I drop a PictureBox onto my form (VS auto-names it pictureBox1) and set it's Image property to something, it shows up. However, if I create a new PictureBox (say, in my constructor), put an image in it, and add it to the form's controls, it shows up blank. Well actually, it's black... the same colour as the background of my image. Very strange.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#32: 3 Weeks Ago

re: Form Objects in Arrays


Hmmm ok, in my case the issue was related to SizeMode. The reason it was black is because it was showing the upper left portion of my image (lol, duh *sigh*). I don't know if that's your issue or not. Anyway, at it's core I'm reproducing what you've got and it's working.

Just for kicks, try changing the SizeMode on your picture box to PictureBoxSizeMode.StretchImage and see if anything shows up.

If not, I'm not sure what your problem is... try to boil it down to a simpler case and debug. Maybe try loading a single image and seeing if it will show up. I did confirm that you should be getting an exception if your path is wrong. So either your images are actually being loaded, or you've got a try/catch block in there somewhere that's handling the exception and not doing anything about it.

Try a bit more debugging :)

Last thing, on the note of issue 2 that thlintoq pointed out above. This is a valid issue, the Bitmap class will leave an open file handle to your hard drive. The update I posted in my last message there resolves the issue... you may want to consider it if that file handle will cause any problems (it prevents you from doing any read operations, so it's possible this may not be a problem for you).

(Hmm, sorry for the post-spam there... I suppose I could have put them all in one since the edit button happens to still be active at the moment. My bad.)
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#33: 3 Weeks Ago

re: Form Objects in Arrays


From that, and this I was hoping wasn't the case, it sounds like its a problem with the Form Drawing thing. Like the window needs to be refreshed like a webpage kinda... Hmm
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#34: 3 Weeks Ago

re: Form Objects in Arrays


Sorry, there were a lot of posts and edits in there. Can you just confirm you got all of the last post there?

Did the SizeMode do anything for you?
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#35: 3 Weeks Ago

re: Form Objects in Arrays


This is my current code:
Expand|Select|Wrap|Line Numbers
  1.  public void StartUp() {
  2. /*List<Image> TempImg = new List<Image>();
  3. for ( int I=0; I < _gd.ClassIcon.Length; I++ ) {
  4. Bitmap tmp = new Bitmap(_gd.DirOther + _gd.ClassIcon[i]);
  5. TempImg.Add(tmp); }*/
  6.  
  7. // StartUp() - Shows the 10 Class Icons for user to select
  8. System.Windows.Forms.PictureBox[] TempPB = new PictureBox[10];
  9. int i = 0;
  10.  
  11. foreach (int TempY in _gd.SUIconY)
  12. foreach (int TempX in _gd.SUIconX) {
  13. TempPB[i] = new PictureBox();
  14. TempPB[i].Location = new Point(TempX, TempY);
  15. TempPB[i].SizeMode = PictureBoxSizeMode.AutoSize;
  16. TempPB[i].Size = new Size(64, 64);
  17. TempPB[i].Image = new Bitmap(_gd.DirOther + _gd.ClassIcon[i]);
  18. TempPB[i].Visible = true;
  19. TempPB[i].BringToFront();
  20.  
  21. Controls.Add(TempPB[i]);
  22. //MessageBox.Show(_gd.DirOther + _gd.ClassIcon[i]);
  23. i++;
  24. } }
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#36: 3 Weeks Ago

re: Form Objects in Arrays


I just tried:
Expand|Select|Wrap|Line Numbers
  1. PictureBox ttmp = new PictureBox();
  2. ttmp.Location = new Point(20, 20);
  3. ttmp.SizeMode = PictureBoxSizeMode.StretchImage;
  4. ttmp.Image = new Bitmap(_gd.DirOther + _gd.ClassIcon[0]);
  5. ttmp.Visible = true;
  6. Controls.Add(ttmp);
No luck on showing up. =\

I have the main Main() class seperate from the class that this method is in...
Expand|Select|Wrap|Line Numbers
  1. public partial class WinCalc : Form
Which is the class the editor created. The class I have my functions in is...
Expand|Select|Wrap|Line Numbers
  1. public class Func : Form
Could this be a reason for the non working?
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#37: 3 Weeks Ago

re: Form Objects in Arrays


Maybe... it depends on which form you want the icons to show up in. They're getting added to whatever owns Controls (line 6 of your code above). If you need them to be in another class, that would explain things.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#38: 3 Weeks Ago

re: Form Objects in Arrays


Update...

The images are appearing correctly. I ditched the different classes. Lame. But oh well. It works for now...

How do I handle the open file handle?
Use dispose on the PictureBox array?
Dispose anywhere? Lol
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#39: 3 Weeks Ago

re: Form Objects in Arrays


Quote:

Originally Posted by Samishii23 View Post

How do I handle the open file handle?
Use dispose on the PictureBox array?
Dispose anywhere? Lol

It's farther up in this thread, have a look :)
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#40: 3 Weeks Ago

re: Form Objects in Arrays


Quote:

Originally Posted by GaryTexmo View Post

Expand|Select|Wrap|Line Numbers
  1. List<Image> imageList = new List<Image>();
  2. Bitmap bitmap = new Bitmap("image.jpg");
  3. Image bitmapDataCopy = Image.FromHbitmap(bitmap.GetHbitmap();
  4. imageList.Add(bitmapDataCopy);
I've tested this and no file handles appear to be left.

Would it be a good idea to Dispose of the DataCopy variable each time through? Or once at the end of the loop? (132 Loops)
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#41: 3 Weeks Ago

re: Form Objects in Arrays


I wouldn't dispose bitmapDataCopy since that's what you're actually using. You can dispose bitmap though, and it's going to get disposed anyway on each loop iteration since it goes out of scope.
Member
 
Join Date: Sep 2009
Location: Usa, Michigan, Westland
Posts: 49
#42: 3 Weeks Ago

re: Form Objects in Arrays


Cool. I think I've this "Question" under wraps now. Thanks Gary!
Now... On to the next question. Lol
Reply