Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//Ive tried these, but ain't working...
ArrayList list = new ArrayList();
            try
            {

                DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) );
                FileInfo[] file = dir.GetFiles();
                
                foreach (FileInfo fl in file)
                {
                    if (fl.Extension == ".jpg")// || fl.Extension == ".jpeg" || fl.Extension == ".png" || fl.Extension == ".gif")
                    {
                        list.Add(fl);
                    }

                }

                lbltotal.Content = "Photo Viewer(" + list.Count + ")";
                Button[] btn = new Button[list.Count];
                Image[] img = new Image[list.Count];
                DockPanel[] dock = new DockPanel[list.Count];

                Label[] lbl = new Label[list.Count];

                                for (int i = 0; i < btn.Length; i++)
                                {
                                    btn[i] = new Button();
                                    img[i] = new Image();
                                    lbl[i].Content = list[i].ToString();
                                    img[i].Source = new BitmapImage(new Uri(list[i].ToString()));
                                    btn[i].Width = 180;
                                    btn[i].Height = 150;
                                    btn[i].BorderThickness = new Thickness(2, 2, 2, 2);
                                    btn[i].Margin = new Thickness(0, 5, 5, 0);
                                    //dock[i].Children.Add(img[i]);
                                    //dock[i].Children.Add(lbl[i]);
                                    

                                    //btn[i].Content = img[i];
                                    //stack[i].Children.Add(img[i]);
                                    //btn[i].Content = dock[i];
                                    wrapContainer.Children.Add(btn[i]);
                                }
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message);
            }
Posted
Comments
johannesnestler 12-Aug-14 9:46am    
It would help if you tag your question - from code I "guess" WPF, but wait the only reason I can remember for using ArrayList is when I'm stuck < .NET 1.1. - so give correct Version, Tag the used UI lib... and in general don't use ArrayList...
So now to your question: What could you possibly mean by "list element appear on an Array of button" - I'd assume something was lost in translation. The whole task seems pretty clear - you want to create a list of image files, why you mess arround with arrays and ArrayLists is beyond my imagination... So you could you please try to rephrase your question (Google translate?), try to use correct technical terms (typenames etc.), and maybe say what "ain't working"?
johannesnestler 12-Aug-14 9:51am    
I forgot to mention - if this is really WPF - you should consider WPF binding (DataTemplate) instead of your manual "control generation" (this was a valid Approach with WinForms but with WPF?)

1 solution

C#
//i created the URI object itself and passed in the FileInfo object in each uri in the foreach loop
//you can replace the loop as 
                        uri = new Uri(fl.FullName);
                        list.Add(new BitmapImage(uri));
//after you might have created a Uri class
//then replace the for loop part with these
 btn[i] = new Button
                                    {
                                        Width = 180,
                                        Height = 150,
                                        BorderThickness = new Thickness(2, 2, 2, 2),
                                        Margin = new Thickness(0, 5, 5, 0)
                                        /*
                                        Content = new Image 
                                        {
                                            Source = list[i],
                                            VerticalAlignment = System.Windows.VerticalAlignment.Center
                                        }*/
                                    };
                                   
                                    
                                     dock[i] = new StackPanel();
                                     img[i] = new Image();
                                     img[i].Source = list[i];
                                     dock[i].Children.Add(img[i]);
                                     lbl[i] = new Label();
                                     lbl[i].Content = list[i].ToString();
                                     dock[i].Children.Add(lbl[i]);
                                     btn[i].Content = dock[i];                                    
                                     wrapContainer.Children.Add(btn[i]);
 
Share this answer
 

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