Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
Program1 sends pictureBox1 to MyClass to show cat.jpg,i have also tried to use sender and casted to PictureBox
but did not works thank you in advance


What I have tried:

     public class MyClass
     {
            public Thread MyThread;
            private PictureBox ClassPictureBox1 = new PictureBox();
            private void Delay()
            {
                //ClassPictureBox1.Image = (PictureBox)Image.FromFile(NameDefinedInProgram1);
                ClassPictureBox1.Image = Image.FromFile("cat.jpg");//<== Error Line C
                Thread.Sleep(5000);
            }
        }

        public MyClass(Object Sender ,  PictureBox XpictureBox1)//,NameDefinedInProgram1)
        {
            ClassPictureBox = (PictureBox)Sender;//<== still did not works Line A
            //ClassPictureBox1 = XpictureBox1;//<=== did not works//line B
            MyThread = new Thread(this.Delay);
            MyThread.Name = Name;
            MyThread.Start();
        }
    }//MyClass
//---Below is method in program1
    private void RunThreadClick(object sender, EventArgs e)//<==the event is made from pictureBox not from button
    {
        MyClass Obj1 = new MyClass(sender, pictureBox1);
        if (Obj1.MyThread.IsAlive == false) Obj1.MyThread.Abort();
    }
Posted
Updated 22-Jan-22 21:30pm
v4

1 solution

OK, let's start with the basics of Event handling.

When an event is raised by (for example) a button click, the system calls the handler method and passes it two parameters: sender which is an object, and e which is always derived from EventArgs. The former is the class instance that raised the event (the Button the user clicked) and the latter is information about the actual Click.
So in a mouse event like MouseDown, you would get the Control that the user was over when they press the mouse button as the sender and e would be a MouseEventArgs instance (which derived from EventArgs and contains more info) telling you which button(s) and where on the COntrol it was clicked.

You can't just cast either of those parameters to a "different type" unless they actually started out as that type (or something derived from it) - so
ClassPictureBox = (PictureBox)Sender;
will fail unless sender is a PictureBox.
But your RunThreadClick probably doesn't get called from a PictureBiox event - it's a lot more likely to have a Task, or Thread based object as it's sender so that it can "refer back" to that if it needs to report some progress for example. And since that isn;t derived from any Control, you definitely can't try to cast it to a PictureBox.

Your whole code looks like it was thrown together in panic - there isn't a lot there that looks like it was thought about and planned in advance.
Your class can't "show a picturebox" - it's not derived from Control, so it can't display anything, and if it's being executed on a thread other than the main thread it can't access any displayable Controls.

Why are you throwing PictureBoxes around anyway? Why not just pass the Image instead, and cut out the middleman?

Plus, when you call Image.FromFile, the source file is locked by the system so that it can't be accessed again until the Image you created from it has been Disposed - and I'm guessing that that doesn't happen until your app closes!

I'd strongly suggest that you need to throw that code away, sit down and think about exactly what you are trying to do, then plan what you need instead of rushing straight into code = it'll save you a lot of time in the long run!
 
Share this answer
 
Comments
Engineer khalid 23-Jan-22 2:37am    
Thank you for your long answer and patent.
please have a look at the updated code above search for line A and line B.
one line is commented and another were un commmented
i made RunThreadClick from the picturebox1 not from button but still did not works
OriginalGriff 23-Jan-22 3:15am    
It doesn't matter - if you are genuinely using threading then it's still most likely that sender is a Thread instance.

What does the debugger show you?
Engineer khalid 23-Jan-22 3:49am    
i have a run time error ,constructer has no problem but this line
ClassPictureBox1.Image = Image.FromFile("cat.jpg");//line C
Engineer khalid 23-Jan-22 4:09am    
it works after i changed to the click to the pictureBox1 ,there was another hidden error it was missing one jpg file
i do not know how to thank you
my pleasure
OriginalGriff 23-Jan-22 4:34am    
You're welcome!

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