- This topic helps to learn how to move PictureBox or any other controls up and down inside form in C#.
- This is done with the help of multithreading process in C#.
PictureBox Class – Move PictureBox Up and Down in form in C#
- PictureBox class represents a Windows picture box control for displaying an image.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
- Syntax
|
1 2 3 4 5 |
[DockingAttribute(DockingBehavior.Ask)] [ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] [DefaultBindingPropertyAttribute("Image")] public class PictureBox : Control, ISupportInitialize |
Code to display an Image:
|
1 |
PictureBox1.Image == Image.FromFile(strFileName) |
- Through the above code we are loading image to the Image Property of the PictureBox Control.
System.Threading Namespace – Move PictureBox Up and Down in form in C#
- The System.Threading namespace provides classes and interfaces that enable multithreaded programming.
- This namespace includes a ThreadPool class that allows you to use a pool of system-supplied threads, and a Timer class that executes callback methods on thread pool threads.
Thread.Start Method – Move PictureBox Up and Down in form in C#
- Help a thread to be scheduled for execution.
Thread.Abort Method – Move PictureBox Up and Down in form in C#
- It raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
Move PictureBox Up and Down in form in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace textc { public partial class Form1 : Form { Thread T1 = default(Thread); int x = 0; int y = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; T1 = new Thread(ThreadProc1); T1.Start(); } public void ThreadProc1() { x = 20; while (y < this.Bottom) { pictureBox1.Location = new Point(x, y); y = y + 1; Thread.Sleep(20); if (y == this.Bottom) { while (y > this.Top) { pictureBox1.Location = new Point(x, y); y = y - 1; Thread.Sleep(20); //set the interval for threading } } } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { T1.Abort(); } } } |
Move PictureBox Up and Down in form in C#






