A complete program that doesn't work as I want to

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
 
P

Peter Duniho

Tony said:
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. [...]

As a general debugging tip:

You can "Debug/Break All" in the debugger when the dialog is showing,
and look at the call stack to see how your method is being called. That
can sometimes help understand what's different, if anything, about the
two times it's displayed.

More generally though, you should not be displaying dialogs _during_ a
drag operation, including not in response to a DragEnter event. Showing
dialogs while the user is in the middle of dragging something is a very
bad UI technique.

In this case, I would guess that the dialog is being shown twice because
for some reason showing it the first time in the DragEnter event causes
the DragEnter event to be raised again. But I don't have the time at
the moment to debug the program and see for sure.

If that's the case, if you fix the behavior so that you're not
interrupting the drag operation with a dialog in the first place, then
of course you won't have any trouble with the interruption occuring
twice instead of once.

Finally, I'll point out that .NET does support reading JPEG files. So
it's not really clear why you're excluding those from consideration in
this code.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top