Let us look at a simple C# Windows Forms Components example.
Example 1: Components
This example will comprise the following files:
ImageList.cs
TimerForm.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). ImageList.cs
Create a file named ImageList.cs
Here is the full code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
[STAThread]
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
Text = "ImageList Example";
ClientSize = new System.Drawing.Size(300, 250);
Controls.AddRange(new Control[] { picture, buttonPrevious, buttonNext});
pictures.ImageSize = new System.Drawing.Size(128, 128);
pictures.ColorDepth = ColorDepth.Depth32Bit;
picture.BackColor = SystemColors.Window;
picture.BorderStyle = BorderStyle.Fixed3D;
picture.Bounds = new Rectangle(75, 25, 150, 150);
picture.SizeMode = PictureBoxSizeMode.CenterImage;
picture.Click += delegate {
LoadImages();
};
buttonPrevious.Text = "&<";
buttonPrevious.Location = new Point(75, 200);
buttonPrevious.Enabled = false;
buttonPrevious.Click += delegate {
if (currentImageIndex > 0) picture.Image = pictures.Images[--currentImageIndex];
buttonPrevious.Enabled = currentImageIndex > 0;
buttonNext.Enabled = currentImageIndex < pictures.Images.Count - 1;
};
buttonNext.Text = "&>";
buttonNext.Location = new Point(150, 200);
buttonNext.Enabled = false;
buttonNext.Click += delegate {
if (currentImageIndex < pictures.Images.Count) picture.Image = pictures.Images[++currentImageIndex];
buttonPrevious.Enabled = currentImageIndex > 0;
buttonNext.Enabled = currentImageIndex < pictures.Images.Count - 1;
};
Show();
LoadImages();
}
private void LoadImages() {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "All Image Files|*.bmp;*.gif;*.jpg;*.jpeg;*.png;*.tif;*.tiff|Bitmap Files|*.bmp|Gif Files|*.gif|Jpeg Files|*.jpg;*.jpeg|Png Files|*.png|Tiff Files|*.tif;*.tiff";
if (ofd.ShowDialog() == DialogResult.OK) {
pictures.Images.Clear();
foreach (var file in ofd.FileNames)
pictures.Images.Add(Image.FromFile(file));
currentImageIndex = 0;
picture.Image = pictures.Images[currentImageIndex];
buttonPrevious.Enabled = currentImageIndex > 0;
buttonNext.Enabled = currentImageIndex < pictures.Images.Count - 1;
}
}
private int currentImageIndex = 0;
private ImageList pictures = new ImageList();
private PictureBox picture = new PictureBox();
private Button buttonPrevious = new Button();
private Button buttonNext = new Button();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
*(a). TimerForm.cs
Create a file named TimerForm.cs
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
// The main entry point for the application.
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
Text = "Timer Example";
StartPosition = FormStartPosition.Manual;
Location = new System.Drawing.Point(400, 300);
ClientSize = new System.Drawing.Size(230, 130);
label.Parent = this;
label.Text = string.Format("{0:N1}", (double)(counter) / 10);
label.Location = new System.Drawing.Point(10, 10);
label.Size = new System.Drawing.Size(210, 70);
//label.AutoSize = true;
label.Font = new System.Drawing.Font("Arial", 48, System.Drawing.FontStyle.Italic);
label.ForeColor = System.Drawing.Color.DodgerBlue;
timer.Interval = 100;
timer.Tick += delegate(object sender, EventArgs e) {
label.Text = string.Format("{0:N1}", (double)(++counter) / 10);
};
button.Parent = this;
button.Text = "Start";
button.Location = new System.Drawing.Point(10, 90);
button.Click += delegate(object sender, EventArgs e) {
timer.Enabled = !timer.Enabled;
button.Text = timer.Enabled ? "Stop" : "Start";
};
}
private Label label = new Label();
private Button button = new Button();
private Timer timer = new Timer();
private int counter = 0;
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |