I've been attempting to drag an image from a Picturebox outside my form boundaries just in the same manners that Windows Explorer does, so for instance, dropping it into an external webbrowser to view, Skype to send etc
Switched to VB.NET to find the most basic solution, but no luck there unfortunately. The drag n' drop effect shows outside my form, however, no application accepts or properly responses to the image drop.
Code is ran on the MouseDown event of the Picturebox.
Me.DoDragDrop(PictureBox1.Image, DragDropEffects.All)
Any ideas? C# or VB.NET, both are fine for me.
Thanks
I found this solution for WPF: Drag and drop to Desktop / Explorer
For winforms works like this:
private void DragDropImage()
{
var filename = "filename.jpg";
var path = Path.Combine(Path.GetTempPath(), filename);
this.pictureBox1.Image.Save(path);
var paths = new[] { path };
this.pictureBox1.DoDragDrop(new DataObject(DataFormats.FileDrop, paths), DragDropEffects.Copy);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
this.DragDropImage();
}
This will copy the content of your picturebox to a temp directory. Then makes a dodragdrop. Its tested and works for me. Code is executed in MouseMove and checks if mouse is clicked.
feal's code worked, however, I didn't see the need to waste time/processing power on saving the image so I modified it to the following in case somebody faces the same issue in the future. Code is inserted in the MouseDown event of the control.
VB.NET:
Me.DoDragDrop(New DataObject(DataFormats.FileDrop, {"Image Path"}), DragDropEffects.All)
C#:
this.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] {#"Image Path"}), DragDropEffects.All);
Thanks a lot, feal.
Related
I just found DockPanel Suite and am learning to use it.
I downloaded v2.9 and am using it with C# in VS2013. I created a simple Windows Forms MDI app. It has one type of child form which is a simple form with only a rich text box control in it. I loaded a few of these as documents. I added an icon to the document's tab to indicate whether the rich text box text has been saved or not.
I change the tab icon anytime the user types in the rich text box through the use of the text_Changed event:
private void txtCode_TextChanged(object sender, EventArgs e)
{
//The text has changed, set the warning icon.
this.Icon = MyApp.Properties.Resources.Warning;
}
The problem I have is that the icon does not update when running at full speed. It updates fine when I am single stepping through the above event. It also updates fine when I load a file within the form's Load event;
private void frmCode_Load(object sender, EventArgs e)
{
//Get the full file path.
string sFile = Path.Combine(cCoCoIDE.CodeBaseRootFolder, Path.GetFileNameWithoutExtension(cCoCoIDE.CodeBaseFile), this.Text);
//Load the source file into the code window.
//A few validations.
//Make sure the file exists.
if (File.Exists(sFile))
{
//File is there. Is it empty? Load only if not empty.
if (new FileInfo(sFile).Length > 0)
{
txtCode.LoadFile(sFile);
//I had to add the following because the text changed
//event fires when I load the file and I need to start
//off with the green checkmark icon.
this.Icon = CoCoIDE.Properties.Resources.GreenChk;
}
}
}
I tried the following after I change the icon (in the Text Changed event):
this.ShowIcon = true;
this.Refresh();
this.Update();
Note: I tried these one at a time. The above is just a list of methods.
Can anyone help me with this? Thank you! SgarciaV
I found the solution. See here:
Dockpanel Suite: Image not updating
I'm new into WPF and have a problem I can't seem to find a solution for.
I'm writing a G19 (Keyboard) applet. This Keyboard has a 320x240 display attached, which you can access with C#.
I'm using WPF for this, because I don't need to do any GDI drawing anymore and use the normal controls instead.
So. It works as I wish. Everything draws properly except one UserControl. I have downloaded this control -> http://marqueedriproll.codeplex.com/
In the designer, the control works, the Loaded event get's fired and the animation is good.
When I run my application, I just see the label and the text. The animation does not work, and the Loaded event does not fire anymore.
Any help is appreciated.
The main function is my wrapper. The wrapper is already a Usercontrol and displays plugins which are switchable. This wrapper has the Frame Control(Wrapper1). I replace the content of this frame every time I switch the plugin.
public void SetPlugin(IPlugin plugin)
{
if (this.MainPlugin != null)
{
this.MainPlugin.OnHide();
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Hidden;
}
this.MainPlugin = plugin;
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Visible;
this.MainPlugin.OnShow();
this.Wrapper1.Content = this.MainPlugin;
}
I think it's the right approach to handle a plugin system that way. The plugin get's drawed on my keyboard.
What I don't understand is why the usercontrol only works in the designer view and not in the running application.
The basic code of the scrolling label is so:
public MarqueeText()
{
this.Loaded += new RoutedEventHandler(MarqueeText_Loaded);
InitializeComponent();
canMain.Height = this.Height;
canMain.Width = this.Width;
}
void MarqueeText_Loaded(object sender, RoutedEventArgs e)
{
StartMarqueeing(_marqueeType);
}
I don't see a reason why it doesn't work. Actually Ive always found a way to fix a problem but this time I see nothing.
Thanks in advance. Your help is really required today.
Have a great saturday! :)
I am guessing you are rendering to a bitmap target, rather than onscreen. If you are using RenderTargetBitmap, you have a couple of responsibilities. You need to set both a presentation source, and make sure you run events on the dispatcher.
Normally, App.xaml or Application.Run does this for you, but if you are not using a Window, you are on your own.
See this related question for details.
I want to drag and drop a control (label for example) in a winform application. I saw some examples on dragging and dropping text, but this is not what I want. I want to enable the user to move a control around. Can anyone direct me to some resources or examples? Thanks.
you should look at examples on how to make draggable controls.
There are some answers here in SO as well.
See this Move controls when Drag and drop on panel in C#
this is a complete example on how to host the Form Designer:
Tailor Your Application by Building a Custom Forms Designer with .NET
I did something similar in Delphi long time ago, will search the source code, convert it into .NET C# and make a wiki page on that matter, as it is becoming such popular question recently :)
As far as i understand, where you wish to drop a control is called a container, infact any control can act as a container. So first that container, you need to enable the drop property as well as the drag property of the controls which you need to drag.
Then write events (Candrag, candrop, controladded, etc.) for each control where in which, some logic to hold the objects and display them as you may want.
Say, ill take an example where in which, you wish to drag imagetext from combombox into a picturebox and then make the picturebox analyze the text and fine related file name in a directory and load that image into its if its present.
So here, when you start dragging the text from combombox, you have to write some logic in event candrag. Then once you drop, you have to write logic to understand what kinda object was added and get the text related to it (kinda deciphering) in the control where you drop other control.
Sorry, i have no code to give you now, but i hope you got the idea how its done. May be this article can help you? http://vicky4147.wordpress.com/2007/02/04/a-simple-drag-drop-in-winforms/
bool draging = false;
int curPosX, curPosY;
private void label2_MouseDown(object sender, MouseEventArgs e)
{
draging = true;
curPosX = Cursor.Position.X;
curPosY = Cursor.Position.Y;
}
private void label2_MouseMove(object sender, MouseEventArgs e)
{
if (draging)
{
label2.Left += Cursor.Position.X - curPosX;
curPosX = Cursor.Position.X;
label2.Top += Cursor.Position.Y - curPosY;
curPosY = Cursor.Position.Y;
}
}
private void label2_MouseUp(object sender, MouseEventArgs e)
{
draging = false;
}
HI,
How to drag and drop folders/files from One Windows Explorer to another windows explorer in C# ?
Internally when i drag and drop from one explorer to another upload should happen
I am using FTP
My requirement is like i have to drag and drop files,folders... to FTP folder in windows explorer ?
Any code snippet...
If the file already exists you can do it like this: DoDragDrop
If the file does not exists then this how to do it: Transferring Virtual Files to Windows Explorer in C#
When you want to be the Drop Target, you need to enable dropping on the form:
AllowDrop = true;
At that point, you need to wire up some of the event handlers:
ItemDrag
DragOver
DragEnter
DragLeave
DragDrop
So if you had a listbox and wanted to list the files dropped, something like:
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
foreach (string fileName in (string[])e.Data.GetData(DataFormats.FileDrop) )
{
listBox1.Items.Add( fileName );
}
}
To be the Drop Source, slightly more work and works best if you actually have a file-name to a real file (see the MSDN link), there is a complete series that describes all of the tricks Shell Style Drag and Drop in .NET which provides a complete library for you to use.
Some sources:
Drag and Drop Using C#
Drag and Drop using C# - another one with the same title.
How to provide file drag-and-drop functionality in a Visual C# application
MSDN Performing Drag-and-Drop Operations in Windows Forms
This code snippet is pretty close to drag from "item" (just replace the "test.txt" string) to any explorer window (including ftp), when user is moving the item manually.
private void Form1_MouseDown(object sender, MouseEventArgs e) {
string[] files = new string[] { #"c:\temp\test.txt" };
this.DoDragDrop(new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy);
}
OK, first for context look at the Windows desktop; You can take items (folders, files) on the desktop and drag them around to different places and they "stay" where you dragged them. This seems to be a pretty useful feature to offer users so as to allow them to create their own "groupings" of items.
My question is thus:
Is there a control in .NET that approximates this behavior with a collection of items?
I'm thinking something like a listview in "LargeIcon" mode, but it allows you to drag the icons around to different places inside the control.
You can do this with a standard ListView control by implementing drag-and-drop. Here's a sample control that does this:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyListView : ListView {
private Point mItemStartPos;
private Point mMouseStartPos;
public MyListView() {
this.AllowDrop = true;
this.View = View.LargeIcon;
this.AutoArrange = false;
this.DoubleBuffered = true;
}
protected override void OnDragEnter(DragEventArgs e) {
if (e.Data.GetData(typeof(ListViewItem)) != null) e.Effect = DragDropEffects.Move;
}
protected override void OnItemDrag(ItemDragEventArgs e) {
// Start dragging
ListViewItem item = e.Item as ListViewItem;
mItemStartPos = item.Position;
mMouseStartPos = Control.MousePosition;
this.DoDragDrop(item, DragDropEffects.Move);
}
protected override void OnDragOver(DragEventArgs e) {
// Move icon
ListViewItem item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
if (item != null) {
Point mousePos = Control.MousePosition;
item.Position = new Point(mItemStartPos.X + mousePos.X - mMouseStartPos.X,
mItemStartPos.Y + mousePos.Y - mMouseStartPos.Y);
}
}
}
I think the closest would the ListView control, but even that is more like an explorer window. You might be able to create your own view that does what you want, but you'd need to manually persist icon locations somewhere.
If you are not opposed to using WPF, Josh Smith has created a pretty neat canvas that I am currently using for a project. It allows you to add controls and drag them around the canvas. You would have to handle what is loaded on the canvas and where on the next load of the program, but that is pretty simple.
http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx
This depends on whether this is a windows application or a web browser based application. In either case you need to have some sort of container to manage the locations of controls. You can manage the position of controls inside of a container with their X and Y coordinates.
You would handle the actual movement using the drag events. So you have drag start, while dragging (you might show a place holder graphic or change the cursor), and finally a drag end (set the control's x and y to the new position). Obviously these aren't the actual event names, but a search for "how to handle drag events" should get you started.
In a web environment, I know jquery has dragging capability built in. So you might want to look at that. The one big thing you'll have to be careful of is maintaining the positions of your controls between postbacks. I'm not sure what would happen in this case.
Windows uses ListView32, an internal control with drag n' drop placeholder features, custom borders...
The icon location can be stored in a XML file, or in the application settings (by putting the XML as string and converting it to file when needed).
You can do, for example:
<icons>
<icon1>
<name>Icon1</name>
<text>My PC</text>
<imageIndex>16</imageIndex>
</icon1>
<icon2>
.....
</icon2>
.....
</icons>
Lorenzo