2007
03.27

Create Drag-and-Drog File Path Locator

There are days when we actually need to get the full path location of a file. A quick example would be when you are adding entries into your Book Collector application. After downloads all information about the particular book you are adding, you are then adding the path of the e-Book into the ‘eBook Textbox’ entry. To fill up the entry, you can either browse for the file, or quickly copy and paste the full path location of the file. Usually the latter option is the fastest. Normally, you will do it in the following sequence:

  1. Open ‘Explorer
  2. Navigate the the folder, copy the folder name from address bar
  3. Return back to Book Collector, paste the folder name at the textbox
  4. Return again to Explorer, select the file, press F2/single-click, then copy the filename.
  5. Finally return to Book Collector to paste the filename.

This will become tedious and tiring once you have tens or hundreds entries to key in. So I came up with this solution. The logic of the application will be:

  1. Create a small, transparent floating box which is movable with mouse drag
  2. You can drag and drop a file to this floating box
  3. The full path of that file is copied to the Clipboard
  4. You can go back to Book Collector (or any other use) and paste the full path using Ctrl+V / right-click+paste

Lets tackle the requirement one by one. To create a small, transparent and floating box you need to set the following property:

  • AllowDrop: Change it into True to allow us drag-and-drop object to this Windows Form
  • BackColor: Change it into something cool (which is other than gray)
  • ControlBox: You only need the box. Set it into False
  • FormBorderStyle: Change it to anything but Sizeable.
  • Locked: Set it into True.
  • MaximizeBox: Set it into False
  • MinimizeBox: Set it into False
  • Opacity: This property regulate how transparent is your Windows Form.0% is completely invisible. 100% is not transparent at all. Set the value into around 50%
  • ShowIcon: Set it into False
  • ShowInTaskbar: Set it into False
  • TopMost: Set it into True

Finally modify the constructor to automatically set the size

public Form1()
{ 
	InitializeComponent();
    this.SuspendLayout();
    this.Size = new Size(35, 35);
    this.ResumeLayout();
} 

To allow the window to be moved by dragging, you need to declare class-wide variable and create Event handlers

private bool isMouseDown = false; // to record whether window on dragging mode 
private int oldX; //x-axis position when dragging start 
private int oldY; //y-axis position when dragging start 

// handle MouseDown Event
private void mouseDown(object sender, MouseEventArgs e) 
{
	oldX = e.X; 
    oldY = e.Y;
    isMouseDown = true; 
}

// handle MouseMove Event 
private void mouseMove(object sender, MouseEventArgs e)
{
	if (isMouseDown)
		this.Location = new Point(e.X+this.Location.X-oldX, e.Y+this.Location.Y-oldY);
}

// handle MouseUp Event 
private void mouseUp(object sender, MouseEventArgs e)
{
	if (isMouseDown)
		isMouseDown = false;
}

// handle Keypress Event 
// To close the program when user press 'q' 
private void Keypressed(object sender, KeyPressEventArgs e)
{
	if (e.KeyChar == 'q')
		this.Close();
}

The next step is to take care when a user Drop an object into the form

private void FmDragOver(object sender, DragEventArgs e)
{
	if (e.Data.GetDataPresent(DataFormats.FileDrop) 
	|| e.Data.GetDataPresent(DataFormats.StringFormat))
	{
		if ((e.AllowedEffect & DragDropEffects.Move) != 0)
			e.Effect = DragDropEffects.Move;
		if (((e.AllowedEffect & DragDropEffects.Copy) != 0) 
		&& ((e.KeyState & 0x08) != 0))
			e.Effect = DragDropEffects.Copy;
	}
}
private void FmDragDrop(object sender, DragEventArgs e)
{
	if (e.Data.GetDataPresent(DataFormats.FileDrop))
	{
		string[] astr = (string[])e.Data.GetData(DataFormats.FileDrop);
		Clipboard.SetText(astr[0]);
	}
}

Download the project here:

This solution has helped me save many minutes spend browsing, copy-pasting file’s full path. I hope it help you too.

Reference:
MSDN
GD Star Rating
loading...
Create Drag-and-Drog File Path Locator, 3.0 out of 5 based on 1 rating

Incoming Search Term

Advertise Here

No Comment

Add Your Comment