C++ WinForms: Looping through directory of pictures with buttons

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2226 Views - Last Post: 23 April 2018 - 12:25 PM

#1 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 08:04 AM

Asking StackOverflow was a mistake, they just downvoted me instead of pointing me in the right direction. So I came here.

I seem to have an issue regarding how to attach a loop of an image directory when using C++ winforms. After searching countlessly for answers I can't seem to find my own. Is there anyway that this can be accomplished? All I have is an unfinished for-loop as that is what my brain is thinking to do for something like this.

The way it should work is, if you look at the picture...
- The Open Directory button should load all the images within the directory that the users chooses. it must contain .BMP!
- The .BMP lookup picturebox should display the first image in the folder
- The small arrow buttons should allow the user to manually click through the folder and scroll through all the pictures eventually making it's way back to the first one.

Here's a picture of the GUI
https://i.imgur.com/CIOnycs.png

And of course here is my code so far.

private: System::Void btnDirectory_Click(System::Object^  sender, System::EventArgs^  e) 
         {
            /*Process ^p; 
            ProcessStartInfo ^pInfo;

            pInfo = gcnew ProcessStartInfo("explorer.exe");
            pInfo->Arguments = "C:\\Windows\\.";

            p = Process::Start(pInfo);
            */
             String^ texture = "";

             FolderBrowserDialog^ directory = gcnew FolderBrowserDialog();
             //directory->Filter = "Texture Files|*.bmp|All Files|*.*";
             if(directory->ShowDialog() != System::Windows::Forms::DialogResult::OK )
             { 
                return; 
             }
             MessageBox::Show("texture file loaded");
             //texture = directory->FileName;
             //textureBox->Image = Bitmap::FromFile(texture);
             // directory can now browse 

             for(int counter=0; counter < ; counter++)
             {

             }



         }


Any help would be appreciated fellas, I really want to get this to work.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ WinForms: Looping through directory of pictures with buttons

#2 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 08:29 AM

The FolderBrowserDialog class allows you to browse and select directory "Folders".

To actually load images you would need the OpenFileDialog class.

Setting the initialDirectory to SelectedPath of the folderBrowserDialog.
Was This Post Helpful? 0
  • +
  • -

#3 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 08:43 AM

I'm a little bit new to C++, so I need to change the FolderBrowserDialog or expand it with the OpenFileDialog?

This post has been edited by Skydiver: 21 April 2018 - 02:15 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 10:20 AM

Well if you look at the docs on msdn,

you would see that SelectedPath is part of the FolderBrowserDialog class

and that InitialDirectory is part of the OpenFileDialog class

There are examples of how to use both on msdn.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 02:13 PM

Moving to C++ CLI...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 02:28 PM

There is no need to quote the post above yours. Just use the Big Reply button or the Fast Reply area.

View Postvictorque, on 21 April 2018 - 11:43 AM, said:

I'm a little bit new to C++...


If you are new to C++, to then choosing to learn C++/CLI is the worse possible choice you could have made to aid in your learning. C++/CLI is meant to fit a niche for people wanting to use their existing C++ knowledge to take advantage of the performance and control offered by C++, but interact with the the .NET Framework at the same time. It has a steep learning curve even for experienced C++ programmer because it does some things in such a none C++ way of doing things. (Granted that it is better than Managed C++ was, but it is still not significantly better.)

Furthermore, you are pushing not only going down the niche of C++/CLI, but you are also going down the deadend that is WinForms. WinForms is at end of life. Even Microsoft has said that it will only update WinForms if there is a very big security issue, otherwise no further effort will be put towards it. If you are plotting a career of getting paid the big bucks based on maintaining legacy systems using WinForms much like this niche programmers left maintaining COBOL and FORTRAN systems, I wish you good luck.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 02:52 PM

View Postvictorque, on 21 April 2018 - 11:04 AM, said:

I seem to have an issue regarding how to attach a loop of an image directory when using C++ winforms. After searching countlessly for answers I can't seem to find my own.

I suspect that you were limiting your search to C++. If you opened up your search to include C# or VB.NET, you very likely would have found somebody who has already done something very similar: load all images (or sound files, or Excel files, or PDFs, or what have you) and present somekind of information or preview pane.
Was This Post Helpful? 0
  • +
  • -

#8 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 03:46 PM

View PostSkydiver, on 21 April 2018 - 02:52 PM, said:

View Postvictorque, on 21 April 2018 - 11:04 AM, said:

I seem to have an issue regarding how to attach a loop of an image directory when using C++ winforms. After searching countlessly for answers I can't seem to find my own.

I suspect that you were limiting your search to C++. If you opened up your search to include C# or VB.NET, you very likely would have found somebody who has already done something very similar: load all images (or sound files, or Excel files, or PDFs, or what have you) and present somekind of information or preview pane.


Thank you for pointing me in the right direction. I am actually going to do maintenance work for a reactor after I finish my
certificates. One of their requirements is having knowledge of C++/WinForms. Are there any resources that exist that can aid me
in this? This is something much of a dream for me.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7447
  • View blog
  • Posts: 15,441
  • Joined: 16-October 07

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 04:37 PM

"knowledge of C++/WinForms"

CLI C++ is kind of a mutant hybrid that in nature would have been still born. It fails to be C++ and in that failure is probably the weakest of the .NET languages, managing to even make VB look good.

Now, C++ is a veteran general purpose language. It persists today by being a true panacea with bare metal roots. It remains the choice for things like operating systems and console games. In short, it satisfies the need for speed. And, notably, putting it in .NET removes this significant advantage.

So, make very sure they want "C++/WinForms," which makes almost no sense, and not something like C++/MFC, which would make sense if you have a non .NET speed requirement.

If some idiot really wants C++ CLI WinForms... Learn C++ alone: it's a big messy language with probably the most time investment out there, but you'll still have an easier time if you know the base line. Learn C# alone: also a big messy language, but nowhere near as bad a C++ and designed from the ground up to work with .NET. Then go to C++ CLI. You see, you'll find almost zero support for C++ CLI WinForms, but if you know C# you'll be able to take those examples and translate them to their ill conceived relative.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 06:24 PM

Is it just me or is no one else concerned that there is a nuclear reactor out there being run on winforms....??

Lets just hope that the winforms is just the front end and something a little more stable like a C DOS program is being run on the backend...!!

Kaboom...!!
Was This Post Helpful? 0
  • +
  • -

#11 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 21 April 2018 - 06:59 PM

All your jokes make me laugh because there is a huge security issue with WinForms LOL

Anyway. This is what I made so far, still no images in the .BMP menu, would really appreciate it if someone helped me figure this out.

if(result == System::Windows::Forms::DialogResult::OK)
			{
				array<String^>^ files = System::IO::Directory::GetFiles(browser->SelectedPath);
				array<PictureBox^>^ images = gcnew array<PictureBox^>((int)files->Length);
				MessageBox::Show("loaded: " + files->Length.ToString(), "information");
				// display .bmp into picturebox
				OpenFileDialog^ bmpfile = gcnew OpenFileDialog();
				//textureBox->Image = System::Drawing::Image::FromFile(files->ToString);
				textBox1->Text = browser->SelectedPath;

				for(int index = 1; index < images->Length; index++)
				{
					images[index]->Image = System::Drawing::Image::FromFile(files[index]->ToString());
					textureBox->Image = Bitmap::FromFile((String^)images[index]->Image);
				}
			}



Was This Post Helpful? 0
  • +
  • -

#12 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 22 April 2018 - 12:44 AM

private: System::Void btnDirectory_Click(System::Object^  sender, System::EventArgs^  e) 
         {
           

             FolderBrowserDialog^ directory = gcnew FolderBrowserDialog();
             
			 OpenFileDialog^ files = gcnew OpenFileDialog();
			 files->Filter = "Bitmap Files|*.bmp";
			 files->MultiSelect = true;
			 array<String^> ^TextureFiles;
			 
             if(directory->ShowDialog() == System::Windows::Forms::DialogResult::OK )
             { 
		 
                         files->InitialDirectory = directory->SelectedPath;

		         if(files->ShowDialog() == System::Windows::Forms::DialogResult::OK)
				 {
					 TextureFiles = files->FileNames;
					 
				 }
               
             }
             
             
             textureBox->Image = Bitmap::FromFile(TextureFiles[0]);
             // directory can now browse 




         }




It would be something similar to the above...

This post has been edited by snoopy11: 22 April 2018 - 02:15 AM

Was This Post Helpful? 0
  • +
  • -

#13 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 22 April 2018 - 05:41 AM

Let me try this out and I'll let you know. Thank you for doing this.
Was This Post Helpful? 0
  • +
  • -

#14 victorque   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-April 18

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 22 April 2018 - 07:46 AM

So everything seems to work!


But the new problem is creating a for loop to iterate through the images selected.

Any thoughts on that?
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ WinForms: Looping through directory of pictures with buttons

Posted 22 April 2018 - 08:14 AM

Yes plenty of thoughts....

You have a back arrow button and a forward arrow button.

You need to create a class which has all the elements needed to share across functions.


TextureFiles->length gives you how many images you have selected

the back arrow button should decrement a counter variable...

if less than 0 it should be set to length - 1.

the forward arrow should increment a counter variable

if more than length-1 it should be set to zero.


Those are my thoughts ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2