6 Replies - 602 Views - Last Post: 27 September 2010 - 08:36 AM Rate Topic: -----

#1 ANCChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 10

Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 07:57 AM

Hey guys/gals,
I've got a question about making dynamic picBoxes, and then allowing them to be clickable as well. Here's the code I have for creating the boxes. What happens is, it reads a directory structure, and creates an ICON for each folder. However, I need these ICONS to be click able. What I mean is, as the loops reads the directory it creates a picBOX with and a label for the picBox according to the folder name. Once that is done, I the user to be able to click on the icon created and it will bring up the contents of the directory that it was assigned a name. I know how to do that part, but I just can't get the darn things to respond to a click!! HELP!

foreach (String file in Directory.GetDirectories(strPath)) //Foreach Statement loops until reaches end of directory.
{
string[] files = file.Split('\\'); //Parses out the / in the directory name and assigns value to string array.

//Creates new picture box.
if (files[files.Length - 1] != "Home")
{
PictureBox picBox = new PictureBox();
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Location = new System.Drawing.Point(imgPlaceCounter1, imgPlaceCounter2); //Sets Initial Position.
picBox.Size = new System.Drawing.Size(75, 75); //Size of classroom icons.
picBox.Image = new Bitmap(@"C:\Users\Chris\Documents\Visual Studio 2010\Projects\Virtual Classroom Testing\images\book" + count + ".gif"); //Location of classroom icon.
picBox.Name = files[files.Length - 1]; //Sets name of picture box so it may be clicked on.
//picBox.Click += new System.EventHandler(this.Button_Click);
button1.Text = picBox.Name;


/****Can't figure out how to have method name change to picBox.Name through every iteration of loop.*****/
/****Maybe a switch statement with a method in it pulling directory names with file[file.Length-1] as the check????***/


//Creates new label box for each picture box
Label lblBox = new Label();
lblBox.Location = new System.Drawing.Point((imgPlaceCounter1 + 50 / 2), imgPlaceCounter2 + 80); //Sets location of label box.
lblBox.AutoSize = true; //Allows labelbox to expand to size of text automatically.
lblBox.Text = files[files.Length - 1]; //Sets text to directory name.

//Adds picturebox and labelboxes to the groupbox on the form.
this.groupBox1.Controls.Add(picBox);
this.groupBox1.Controls.Add(lblBox);

imgPlaceCounter1 += 85; //Adds pixels to X-Value of classroom icons.
count++; //Keeps track of number of times through the foreach loop. Controls number of rows of icons.
}
//If 4 icons have been placed. Move next icon back to beginning position, but down one row.
//Resets counter to complete four more icons. This will repeat as long as foreach loop runs.
if (count == 4)
{
imgPlaceCounter1 = 15;
imgPlaceCounter2 += 150;
count = 0;
}

/*testing path variables placing text in button when pressed.
button1.Text = "strPath = " + strPath + " file = " + file + " files = " + files[files.Length - 1];
button1.Text = picBox.Name;*/
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Making Dynamicly created picboxes, clickable.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:09 AM

A) Put your code in code tags per DIC policy and the email you got when you joined.

Quote

picBox.Name = files[files.Length - 1]; //Sets name of picture box so it may be clicked on.
It doesn't have to have a name to be clickable.

Quote

//picBox.Click += new System.EventHandler(this.Button_Click);
This is right. Why do you have it commented out?


Quote

/****Can't figure out how to have method name change to picBox.Name through every iteration of loop.*****/
You're off in some weird tangent. You don't change the method name for every iteration through the loop. What you had above was right.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:22 AM

Personally, if everyplace you are going to have a picturebox you also have a label, then I would make a custom control that had those two items on it.

That way you aren't trying to keep them sync'd. You can have an "ancPictureBox" object. Adding an ancPictureBox automatically gets you both picture and label. Deleting one means deleting both. When you set its name property, have that set the text on it's label.


*Now* that you have your two components as one control, you can quit worrying about where to put them. Just make a FlowLayoutPanel and add your dynamically created ancPictureBox to it. It will do the rest and you can quit playing silly buggers with the locations.


Quote

string[] files = file.Split('\\'); //Parses out the / in the directory name and assigns value to string array.

//Creates new picture box.
if (files[files.Length - 1] != "Home")
Or you could just have the Framework get the file name for you.
System.IO.Path.GetDirectoryName(file);



Bulding an application - Part 1



Keep in mind that for every += subscription to a click event you make, you should eventually have a -= unsubscribe. If you don't unsubscribe before destroying the object you create a memory leak. Its tiny. and if you are only going through this once then you may never see an issue. But if the program remains open, keeps changing directories, keeps making new photos and thus new click subscriptions that later get destroyed without unsubscribing... you eventually have a *issue*.


Quote

picBox.Image = new Bitmap(@"C:\Users\Chris\Documents\Visual Studio 2010\Projects\Virtual Classroom Testing\images\book" + count + ".gif");
One problem with this is that it keeps your application linked to this file for as long as the picturebox object has scope (is alive in memory). The file is locked up, nobody else can change it. If you read the file via a stream then make that an image you leave the file on HDD free.
Was This Post Helpful? 0
  • +
  • -

#4 ANCChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:26 AM

Ok, thank you for the help so far. The problem I have is that each picBox needs to point to a different directory. With the way it is now, all the buttons are getting set to the same thing. I guess then my real question is, how do I get each button to link to a different directory.

The loop I posted above reads a structure to the affect of different student classes. So each button gets assigned a class Folder, BIO101, SCI101, etc. I need to get each button to hold the value of the directory it was named.

The loop basically creates the following layout in the form. And I need each button to be linked to it's directory that it was named from.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:31 AM

Do do as tlhIn'toq suggested, make a custom UserControl that holds the label and picturebox, and also a property called Directory or something like that. You can set the directory when you create the instance of the control.

Then, in the event, you can cast sender as one of your controls, and have access to the directory name.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:35 AM

The right way is to make a custom control. Obviously everything you are doing is based around it. It is the center of your program. It will eventually grow to more and more functions.

The 'cheat' way for now (for testing only) is to put the path in the .Tag of the picturebox. A tag can be any object. So put a string in their to hold the path and later get it back out

((PictureBox)sender).tag.ToString()
Was This Post Helpful? 0
  • +
  • -

#7 ANCChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 10

Re: Making Dynamicly created picboxes, clickable.

Posted 27 September 2010 - 08:36 AM

Hah! Thank you for your suggestions. The problem is the company that I worked for figured out that I graduated 10 years ago with a degree in C++. The problem is, I've not used it since I graduated. I've been a network admin since just before I graduated. Now I'm trying to write this small program for them and I just can't remember half this stuff. Not to mention I'm doing it in C# which I've only picked up recently. So thank you for taking the time to explain things and set me on the right path. I used to know this stuff to a point, now...I'm a newb...I've been re-newbed! lol Makes sence putting them into one control. I'll hit that first so I can get rid of that rediculas placement code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1