Thread: Change the picturebox to a randome one once i clicked the button

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    8

    Change the picturebox to a randome one once i clicked the button

    Hi there.
    I have learned structural programming , for a while now, I started to learn OOP, so i'm still newbie in this field, please excuse me if it is too simple, but i can not do it. Can you guys help me ?
    So, i have made a UI, which will display a image, and when I press the button below , it should switch to another one ( randomly), I have searched the whole internet but i've found nothing clear that should have helped me.
    This is the display :




    https://i.gyazo.com/3df7b40264dc9d18...c35b3ecb80.png
    And this is the folder where the pictures should come from:
    https://i.gyazo.com/491abb6c40eba638...27021f864c.png
    Ok, and this is the code of the functions for the image and the button ( btw the names of the boxes are standard, i've not modified any of them ).
    My code :
    Code:
    #pragma once
    
    namespace speranta {
    
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
    
        /// <summary>
        /// Summary for MyForm
        /// </summary>
        public ref class MyForm : public System::Windows::Forms::Form
        {
        public:
            MyForm(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~MyForm()
            {
                if (components)
                {
                    delete components;
                }
            }
        private: System::Windows::Forms::PictureBox^  pictureBox1;
        protected:
        private: System::Windows::Forms::Button^  button1;
    
    
        protected:
    
    
        protected:
    
        protected:
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
                this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
                this->button1 = (gcnew System::Windows::Forms::Button());
                (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
                this->SuspendLayout();
                // 
                // pictureBox1
                // 
                this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBox1.Image")));
                this->pictureBox1->Location = System::Drawing::Point(9, 12);
                this->pictureBox1->Name = L"pictureBox1";
                this->pictureBox1->Size = System::Drawing::Size(747, 419);
                this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
                this->pictureBox1->TabIndex = 0;
                this->pictureBox1->TabStop = false;
                this->pictureBox1->Click += gcnew System::EventHandler(this, &MyForm::pictureBox1_Click);
                // 
                // button1
                // 
                this->button1->Location = System::Drawing::Point(299, 456);
                this->button1->Name = L"button1";
                this->button1->Size = System::Drawing::Size(153, 34);
                this->button1->TabIndex = 1;
                this->button1->Text = L"button1";
                this->button1->UseVisualStyleBackColor = true;
                this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
                // 
                // MyForm
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(768, 497);
                this->Controls->Add(this->button1);
                this->Controls->Add(this->pictureBox1);
                this->Name = L"MyForm";
                this->Text = L"MyForm";
                this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
                (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
            // the main window function
        }
        private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
            // the picutebox function
        }
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
            // the button function
        }
        };
    }
    if you need, those are the proprieties for each :
    https://i.gyazo.com/6acc30a1b5d99b5e...cde60ffec6.png
    https://i.gyazo.com/3760e2fea6b3ee66...5dff7fb13a.png



    Thank you...


  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You can divide this task into two sub-tasks.
    First is to read all the filenames from the image directory and store them in an array or list - best time is in the form load event
    Second you create a random number, pick the filename from the array or list and assign a new image to the pictute box - when the button is clicked

    For the first part use Directory::GetFiles method
    Directory.GetFiles Method (String) (System.IO)

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    8
    Quote Originally Posted by OldGuy2 View Post
    You can divide this task into two sub-tasks.
    First is to read all the filenames from the image directory and store them in an array or list - best time is in the form load event
    Second you create a random number, pick the filename from the array or list and assign a new image to the pictute box - when the button is clicked

    For the first part use Directory::GetFiles method
    Directory.GetFiles Method (String) (System.IO)
    Ok, I modified it but every time there is a error... I know this is not a big deal but I've tried everything for hours now...
    Can you please modify my code from above ( the last 3 function from the button of the code ) , with the code you gave me.
    My photos directory is : C:\Users\Alex\Desktop\POO\geo\photoss , and the photos are : 1.png, 2.jpg,3.jpg.
    That would be very helpful, thank you again.
    Last edited by Alex Matei; 02-03-2018 at 03:05 AM.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    OK, here it is:
    Try if you can run it after changing line 108 with your pic path.
    Code:
    #pragma once
     
    namespace speranta 
    {
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections::Generic;
        using namespace System::Windows::Forms;
        using namespace System::IO;
        using namespace System::Drawing;
     
        /// <summary>
        /// Summary for MyForm
        /// </summary>
        public ref class MyForm : public System::Windows::Forms::Form
        {
          String^ mPicPath;
          List<String^> ^mPicNames; 
          Random^ mRandom;
        public:
            MyForm(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
     
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~MyForm()
            {
                if (components)
                {
                    delete components;
                }
            }
        private: System::Windows::Forms::PictureBox^  pictureBox1;
        protected:
        private: System::Windows::Forms::Button^  button1;
     
     
        protected:
     
     
        protected:
     
        protected:
     
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
     
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
              this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
              this->button1 = (gcnew System::Windows::Forms::Button());
              (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
              this->SuspendLayout();
              // 
              // pictureBox1
              // 
              this->pictureBox1->Location = System::Drawing::Point(9, 12);
              this->pictureBox1->Name = L"pictureBox1";
              this->pictureBox1->Size = System::Drawing::Size(747, 419);
              this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
              this->pictureBox1->TabIndex = 0;
              this->pictureBox1->TabStop = false;
              this->pictureBox1->Click += gcnew System::EventHandler(this, &MyForm::pictureBox1_Click);
              // 
              // button1
              // 
              this->button1->Location = System::Drawing::Point(299, 456);
              this->button1->Name = L"button1";
              this->button1->Size = System::Drawing::Size(153, 34);
              this->button1->TabIndex = 1;
              this->button1->Text = L"button1";
              this->button1->UseVisualStyleBackColor = true;
              this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
              // 
              // MyForm
              // 
              this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
              this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
              this->ClientSize = System::Drawing::Size(768, 497);
              this->Controls->Add(this->button1);
              this->Controls->Add(this->pictureBox1);
              this->Name = L"MyForm";
              this->Text = L"MyForm";
              this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
              (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
              this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: 
          System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) 
          {
            mPicPath = "E:\\Bilder\\";
            mPicNames = gcnew List<String^>;
            array<String^> ^tmp = Directory::GetFiles(mPicPath, "*.png");
            mPicNames->AddRange(tmp);
            tmp = Directory::GetFiles(mPicPath, "*.jpg");
            mPicNames->AddRange(tmp);
            if (mPicNames->Count == 0)
            {
              MessageBox::Show("No pics loaded");
              return;
            } 
            mRandom = gcnew Random();
            pictureBox1->Image = Image::FromFile(mPicNames[0]);
          }
        private: 
          System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) 
          {
            // What shall happen here ??
          }
        private: 
          System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
          {
            int idx = mRandom->Next(mPicNames->Count);
            pictureBox1->Image = Image::FromFile(mPicNames[idx]);
          }
        };
    }
    BTW Hre are some tutorials you might want to look at.
    Microsoft Visual C++/CLI Programming
    YouTube

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    8
    Quote Originally Posted by OldGuy2 View Post
    OK, here it is:
    Try if you can run it after changing line 108 with your pic path.
    ---
    ---

    BTW Hre are some tutorials you might want to look at.
    Microsoft Visual C++/CLI Programming
    YouTube
    Thank you maaaaan... It was not working the first time, but I fixed it. And after that made one more button , which will show another picture which will kinda liked to the first one, from another folder. Anyway, you're code lightened my way. Thank you a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a new window and run function when button clicked
    By izah1311 in forum Windows Programming
    Replies: 1
    Last Post: 05-10-2017, 11:47 AM
  2. Button diseapers behind a picturebox
    By Brandon Switzer in forum Windows Programming
    Replies: 1
    Last Post: 11-25-2013, 10:00 PM
  3. Button diseapers behind a picturebox
    By Brandon Switzer in forum C++ Programming
    Replies: 0
    Last Post: 11-25-2013, 03:16 PM
  4. My BS_OWNERDRAW button draw as white when clicked...
    By Jonathan Beaubi in forum Windows Programming
    Replies: 1
    Last Post: 08-16-2007, 10:41 AM
  5. exit while loop when button clicked
    By shav in forum Windows Programming
    Replies: 18
    Last Post: 02-02-2005, 05:46 AM

Tags for this Thread