C#: Changing the image in a PictureBox

Hi

I'm building a application in winform to save images I would like to show the image in a pictureBox1 once it has saved clearly that is to much code to show here so I've built a demo

I can get a single image to display because when the code in class1 finishes control is retuned back to the form
How can I modify it so I can send a signal back to the form to change the image in this demo just looping through all the jpg's in a directory (Thus simulating the save image )


Form1.cs


using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        GetTextClass GetTextObject = new GetTextClass();
        public Form1()
        {

            InitializeComponent();
       
        }
        public string FirstName
        {
            get { return firstNameTextBox.Text; }
            set { firstNameTextBox.Text = value; }
        }
       
        

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                GetTextObject.IsChecked  = true;
            }
            else if (checkBox1.Checked == false)
            {

                GetTextObject.IsChecked = false;
            }
            string Txt = GetTextObject.GetText (FirstName );
            String PicPath = GetTextObject.ChangePic;
            LoadPic(PicPath);
            FirstName = Txt;
        }

        private void LoadPic(string picPath)
        {
            if (File.Exists(picPath))
            {
                pictureBox1.Image = System.Drawing.Image.FromFile(picPath);
            }
        }

       
    }
    
}

Open in new window


Class1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    class GetTextClass
    {
        public  bool IsChecked { get; set; }
        //public string _ImgPath;
        public string ChangePic
        {
            get;
            set;
        }

        public delegate void IsCheckedDelegate(bool IsChecked);

        public string GetText(string Name)
        {
            string greeting = "";
            
            if (IsChecked == true)
            {
                greeting = "Hello " + Name;
            }
            else
            {
                greeting = "Goodbye " + Name;
            }
            string Path = @"D:\Temp\Hazel.jpg";
            if (File.Exists(Path))
            {
                ChangePic= Path;
            }
            //if (Directory.Exists(Path))
            //{
            //    string[] filePaths = Directory.GetFiles(Path, "*.jpg",
            //                             SearchOption.AllDirectories);
      
            //        foreach (string fileName in filePaths)
            //    {
            //        _ImgPath = fileName;
            //        //ChangePic = fileName;
            //        System.Threading.Thread.Sleep(1000);
            //    }
      
            //}
            //else{
            //        System.Windows.Forms.MessageBox.Show("Can't find \n" + Path);
            //    }

            return greeting;
        }


    }
}

Open in new window

The form is show in attached
Form1.JPG
LVL 1
trevor1940Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You can raise an event and subscribe to that event:

Please see:
https://docs.microsoft.com/en-us/dotnet/standard/events/how-to-raise-and-consume-events
   and
https://www.intertech.com/Blog/c-sharp-tutorial-understanding-c-events/


Essentially you would have a pictureChanged event that is subscribed by the form.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Thanx
Unable to try this probably until the weekend
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.