using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("c:\\testImage.jpg");
}
}
}
First, you have to declare the PictureBox image property then you have to define the path from the constructor of Image.FromFile as mention in above code. In the following image, you can preview that how we have set the TestImage inside PictureBox.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEPArticle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TEPpictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("You Just Click On the TEP PictureBox");
}
}
}
You can declare any kind of functionality within the click event handler according to your desire, we just used the message box to minimize the code and save our time. The main purpose is to share the basic logic of click event handler. In the following image, you can observe that when we have to click the PictureBox it's prompt the message.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEPArticle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TEPpictureBox1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("You Just DoubleClick On the TEP PictureBox");
}
}
}
We have declared the message box in DoubleClick event handler. You can declare any other functionality within double click event handler. In the following image, you can be observed that how message prompt when user will click twice on the C# PictureBox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEPArticle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TEPpictureBox1_MouseEnter(object sender, EventArgs e)
{
MessageBox.Show("You Just Enter in visible part of TEP PictureBox");
}
}
}
We have declared the message box within the MouseEnter event handler. Now when user will enter the mouse cursor to visible part message prompt. In the following image, you can observe the output of the above code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEPArticle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TEPpictureBox1_MouseHover(object sender, EventArgs e)
{
TEPpictureBox1.Image = Image.FromFile("C: \\Users\\Public\\Pictures\\test.jpg");
}
}
}
In the above code, you can observe that we have used the test.jpg as the image when the MouseHover event will be executed. You can declare any kind of functionality as you are required. You can replace the path of the image and set MouseHover event to your C# PictureBox. When you will execute the code and hover the mouse cursor on the PictureBox it will change the image.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEPArticle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TEPpictureBox1_MouseHover(object sender, EventArgs e)
{
TEPpictureBox1.Image = Image.FromFile("C: \\Users\\Public\\Pictures\\pic1.jpg");
}
private void TEPpictureBox1_MouseLeave(object sender, EventArgs e)
{
TEPpictureBox1.Image = Image.FromFile("C: \\Users\\Public\\Pictures\\pic2.jpg");
}
}
}
When you will execute above code, and hover the PictureBox the image get changed and when left the PictureBox image get change again. By this, you can create animated effects in your desktop application. Remember if you will copy the above code you have to add these event handlers to the C# PictureBox.
We have tried to give you basic to advance information about C# PictureBox. In this tutorials, you have learned many different techniques which are used in the professional development of desktop application along with the usage of Event handlers. After this, you can go for C# ListBox Control, C# Button Control, C# Label Control and C# ComboBox Control. In all of this, don't forget to subscribe our YouTube Channel "TheEngineeringProjects" and C# Video Tutorial PlayList.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w