T
Tony Johansson
Hi!
Here I have a complete program where I can drag a file from the exporer that
have extension of *.gif, *.png or *.bmp into an area that is displayed.
What I don't understand is if I for example drag a *.jpg file into the
program this statement
MessageBox.Show("This program only support the following file format *.gif,
*.png and *.bmp");
is displayed twice. It should only be displayed once. So I can't figure what
the problem is ?
I think it has something to do with threads
I hope somebody can tell me what I can do to correct this.
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting;
using System.Threading;
using System.Windows.Forms;
namespace TIG076_Lab2
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyFormDrop());
}
}
partial class MyFormDrop
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
this.pictureBox.Image.Dispose();
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBox
//
this.pictureBox.Location = new System.Drawing.Point(10, 185);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(506, 308);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(273, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Drop a GIF, PNG or BMP file in the drop-area of
this form";
//
// panel1
//
this.panel1.AllowDrop = true;
this.panel1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(10, 62);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(125, 103);
this.panel1.TabIndex = 2;
this.panel1.DragDrop += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragDrop);
this.panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 46);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Drop area:";
//
// MyFormDrop
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(528, 505);
this.Controls.Add(this.label2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox);
this.Name = "MyFormDrop";
this.Text = "FormDrop";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Panel panel1;
}
public partial class MyFormDrop : Form
{
bool validData = false;
protected Image myImage;
public MyFormDrop()
{
InitializeComponent();
}
private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
if (validData)
pictureBox.Image = myImage;
}
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
string filename;
if (((validData = GetFilename(out filename, e)) == true) &&
(e.Data.GetDataPresent(DataFormats.FileDrop)))
{
myImage = new Bitmap(filename);
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}
protected bool GetFilename(out string filename, DragEventArgs e)
{
bool status = false;
filename = String.Empty;
if ((e.AllowedEffect & DragDropEffects.Copy) ==
DragDropEffects.Copy)
{
string[] data = (string[])e.Data.GetData("FileName");
if (data.Length == 1)
{
filename = ((string[])data)[0];
string extension = Path.GetExtension(filename).ToLower();
if ((extension == ".gif") || (extension == ".png") ||
(extension == ".bmp"))
status = true;
else
MessageBox.Show("This program only support the following
file format *.gif, *.png and *.bmp");
}
}
return status;
}
}
}
//Tony
Here I have a complete program where I can drag a file from the exporer that
have extension of *.gif, *.png or *.bmp into an area that is displayed.
What I don't understand is if I for example drag a *.jpg file into the
program this statement
MessageBox.Show("This program only support the following file format *.gif,
*.png and *.bmp");
is displayed twice. It should only be displayed once. So I can't figure what
the problem is ?
I think it has something to do with threads
I hope somebody can tell me what I can do to correct this.
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting;
using System.Threading;
using System.Windows.Forms;
namespace TIG076_Lab2
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyFormDrop());
}
}
partial class MyFormDrop
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
this.pictureBox.Image.Dispose();
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBox
//
this.pictureBox.Location = new System.Drawing.Point(10, 185);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(506, 308);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(273, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Drop a GIF, PNG or BMP file in the drop-area of
this form";
//
// panel1
//
this.panel1.AllowDrop = true;
this.panel1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(10, 62);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(125, 103);
this.panel1.TabIndex = 2;
this.panel1.DragDrop += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragDrop);
this.panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 46);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Drop area:";
//
// MyFormDrop
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(528, 505);
this.Controls.Add(this.label2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox);
this.Name = "MyFormDrop";
this.Text = "FormDrop";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Panel panel1;
}
public partial class MyFormDrop : Form
{
bool validData = false;
protected Image myImage;
public MyFormDrop()
{
InitializeComponent();
}
private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
if (validData)
pictureBox.Image = myImage;
}
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
string filename;
if (((validData = GetFilename(out filename, e)) == true) &&
(e.Data.GetDataPresent(DataFormats.FileDrop)))
{
myImage = new Bitmap(filename);
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}
protected bool GetFilename(out string filename, DragEventArgs e)
{
bool status = false;
filename = String.Empty;
if ((e.AllowedEffect & DragDropEffects.Copy) ==
DragDropEffects.Copy)
{
string[] data = (string[])e.Data.GetData("FileName");
if (data.Length == 1)
{
filename = ((string[])data)[0];
string extension = Path.GetExtension(filename).ToLower();
if ((extension == ".gif") || (extension == ".png") ||
(extension == ".bmp"))
status = true;
else
MessageBox.Show("This program only support the following
file format *.gif, *.png and *.bmp");
}
}
return status;
}
}
}
//Tony