C# Dragdrop events not working - c#

i created a little form in C# and registered some drag drop events to it. But i cant get it to work properly!
Goal: Dragging files from explorer.exe into the form, and handeling it, and photos dragged into the form. I test the code by dragging a .txt file from my desktop into the form. It shows the (/) cursor when doing so
Code:
public partial class Viewer : Form
{
public Viewer()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Viewer_DragEnter);
this.DragDrop += new DragEventHandler(Viewer_DragDrop);
}
public void Viewer_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragEnter");// Line has breakpoint
}
public void Viewer_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("Viewer_DragDrop: "+e.Data.GetFormats());// Line has breakpoint
}
}
What i have tried:
Try-catch over the constructor => nothing
Running succesfull build outside of visualstudio(2010)
Running explorer.exe as administrator when dragging items into the form
Thanks for reading my question
EDIT:
when testing the files were on the desktop(%userprofile%\Desktop). but it shouldnt matter where they are stored. the images are supposed to be dragged from the browser or a word document into the form
ANOTHER EDIT:
When trying to run it without visual studio, i get an InvalidOperationException, that occurs on the AllowDrop = true; but i dont get a reaction when try-catching it
SOLUTION:
OMG... so.... i kinda moved the public static Main() into the viewer.cs file.... and i forgot to add the STAThread attribute.
I found this when executing the build outside of visual studio again, and in the exception form it showed that the thread needed to be a STAThread but that part of the message was hidden and hard to find
anyway: ALWAYS USE STATHREAD :P

Related

Problem when running multiple instances of the same form

Here's the situation. I've been developing a serial communication application and I needed multiple instances of my main form to show up when I click on a ListView item. Everything was running fine before I added this code:
private void ListView_DeviceList_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView_DeviceList.HitTest(e.Location);
if (hit.Item != null)
{
Form m = new Form1();
m.Show();
}
}
After I tried to run it and double clicked in my ListView new instance of my form opened up as expected. The problem was they were glued together and I couldn't see the first one. After I closed it and tried to run it again it didn't show up. I could see the application running in the task manager and there was the icon in the windows taskbar but no UI opened up... Sorry this might be really unclear but I don't know how to describe it differently.

c# winforms drag and drop events not firing [duplicate]

I have a winforms app that uses a UserControl. The user control's job is to collect a file that the user drops on it from Windows Explorer, Open the file, determine the type and handle it accordingly.
This control worked PERFECTLY under Visual Studio 2008 Pro. I upgraded to VS 2010 Pro, and now, it doesn't work. Is there a flag or a property that has changed that I should be aware of??
I made a quick demo to test. This demo works perfectly under 2008, but doesn't work at all under 2010.
The setup: Create a new winform project. Add a user control. Set the following code in the user control's code section. (compile to get the user control to appear in the toolbox) Add the user control to the form. Run the program, and drag ANY file from windows onto the form. If it works, the user control area should change colors.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(UserControl1_DragDrop);
this.DragEnter += new DragEventHandler(UserControl1_DragEnter);
this.DragLeave += new EventHandler(UserControl1_DragLeave);
}
void UserControl1_DragLeave(object sender, EventArgs e)
{
this.BackColor = Color.FromName("Control");
}
void UserControl1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
this.BackColor = Color.Blue;
}
else
{
e.Effect = DragDropEffects.None;
}
}
void UserControl1_DragDrop(object sender, DragEventArgs e)
{
this.BackColor = Color.Yellow;
}
}
I'm open to any explanation or fix that you guys may think up!
UPDATE:
I tested using the comments listed below. STILL doesn't work. However, I have noted that it only fails while in the development environment. When I go to the bin directory and launch the program manually, it works fine. It just doesn't work when I am in the development environment, which makes debugging a bit difficult. Still looking for the big-picture fix.
A likely failure cause here is UIPI, the user interface component of UAC. You cannot drag from a non-elevated process and drop to a window owned by an elevated process. You'll trigger this when you started Visual Studio from a shortcut that has the "Run this program as an administrator" option in the Compatibility tab turned on. The only workaround is to turn that option off. Or to run it directly from the .exe file, as you discovered.

Outlook New Mail Window opened from C# seems to have focus but my app still has it

I'm having a problem that I've been trying to solve for days now, but without luck!
On my Windows Forms Application I have a grid. One column contains an email address. When the user double clicks this column, I want to open a new E-Mail Window via Outlook automation. This window should have the focus and allow the user to type immediately.
Everything works fine, when:
I'm running my app from Visual Studio.
Or my app has the focus.
However, when I run my .exe and outlook has the focus when I double click the column, the following happens:
The new Mail window opens as expected
The cursor blinks in the new mail window (as expected)
when the user starts typing, the cursor still blinks in outlook but the typed text appears in the grid of my application, not in outlook.
I was able to reproduce the problem with a simple form that has a textbox on it.
I use the following code:
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenOutlookMail(textBox1.Text);
}
private void OpenOutlookMail(string to)
{
MailItem item = OutlookApp.CreateItem(OlItemType.olMailItem) as MailItem;
item.To = to;
item.Subject = string.Empty;
item.Body = string.Empty;
item.Display();
}
protected Application OutlookApp
{
get
{
if (mOutlookApp == null)
{
mOutlookApp = new Application();
}
return mOutlookApp;
}
}
What i already tried was to
Activate my current form via this.Activate() before the call to OpenOutlookMail
Activate the MailItem Inspector Object
Activate the ActiveWindow and ActiveExplorer of Outlook via Automation
Using AutoIt as explained here Similar Problem with MS Word on the MSDN Forum
Any help would be appreciated!
I wrote about focusing a background window some time ago:
http://blog.sebastianbrand.com/2010/02/activate-form-in-background.html
private void label1_Click(object sender, EventArgs e)
{
// mainform.BringToFront(); // doesn't work
BeginInvoke(new VoidHandler(OtherFormToFront));
}
delegate void VoidHandler();
private void OtherFormToFront()
{
mainform.BringToFront(); // works
}
If you do have an handle of the bad window, give that a try.
You can try to use Dispatcher.BeginInvoke(...) with some low priority in your textBox1_MouseDoubleClick(...) method to call OpenOutlookMail(). It often helps to workaround focus management issues like this one.
I haven't been able to reproduce the problem with your code. I've used Microsoft.Office.Interop.Outlook version 14.0.0.0 and in every tests i've done the mail window get the focus.
As you state,
Everything works fine, when:
•I'm running my app from Visual Studio.
•Or my app has the focus.
Maybe trying to focus your form and/or making your application sleep before opening the mail window would work
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Focus();
System.Threading.Thread.Sleep(500);
OpenOutlookMail(textBox1.Text);
}
Interops often have weird behaviors. :s

Implementing activeX control to view PowerPoint slides in Windows forms using c#, not showing control

I'm working on a project and I need to embed a PowerPoint viewer in windows forms. I'm using the following activeX control: http://www.daolnwod.com/free-powerpoint-viewer-activex.html.
I activated the control to be used with the form designer's toolbox and dragged it into my form. I then edited the code in the InitializeComponent() method to the following:
this.axPowerPointViewer1 = new AxPOWERPOINTVIEWERLib.AxPowerPointViewer();
((System.ComponentModel.ISupportInitialize)(this.axPowerPointViewer1)).BeginInit();
this.axPowerPointViewer1.Enabled = true;
this.axPowerPointViewer1.Location = new System.Drawing.Point(0, 0);
this.axPowerPointViewer1.Name = "axPowerPointViewer1";
this.axPowerPointViewer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPowerPointViewer1.OcxState")));
this.axPowerPointViewer1.Size = new System.Drawing.Size(925, 573);
this.axPowerPointViewer1.TabIndex = 5;
//this.axPowerPointViewer1.CreateControl();
this.Controls.Add(this.axPowerPointViewer1);
((System.ComponentModel.ISupportInitialize)(this.axPowerPointViewer1)).EndInit();
And in my Forms constructor
public Form1()
{
InitializeComponent();
axPowerPointViewer1.Show();
bool loaded = axPowerPointViewer1.LoadFile(#"C:\Debug\test2.ppt"); // loaded = false
string z = axPowerPointViewer1.GetSlideCount().ToString();
}
However, when I'm opening the form nothing shows up. The code compiles but I can't see my test slide that I've been working on. I have created 2 buttons for 'Previous' and 'Next' slides but debugging gives me a slide location of 0 every time so something must be wrong and I can't seem to find it.
UPDATE
The problem has been solved. It seems I didn't call axPowerPointviewer1.InitControl(). It still has a few troubles, sometimes it won't display the first slide at startup. If things keep running smoothly I'll post an answer to this problem.
The problem is in initialising the control. In order for the control to fully function you need to call the InitControl() method so call calling the following code should make the program work:
private void Form1_Load(object sender, EventArgs e)
{
this.axPowerPointViewer1.InitControl();
}

Drag and drop from Windows File Explorer onto a Windows Form is not working

I'm having an issue dragging a file from Windows Explorer on to a Windows Forms application.
It works fine when I drag text, but for some reason it is not recognizing the file. Here is my test code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
AllowDrop is set to true on Form1, and as I mentioned, it works if I drag text on to the form, just not an actual file.
I'm using Vista 64-bit ... not sure if that is part of the problem.
The problem comes from Vista's UAC. DevStudio is running as administrator, but explorer is running as a regular user. When you drag a file from explorer and drop it on your DevStudio hosted application, that is the same as a non-privileged user trying to communicate with a privileged user. It's not allowed.
This will probably not show up when you run the app outside of the debugger. Unless you run it as an administrator there (or if Vista auto-detects that it's an installer/setup app).
You could also run explorer as an admin, at least for testing. Or disable UAC (which I would not recommend, since you really want to catch these issues during development, not during deployment!)
The code you posted should work.
Try putting this at the beginning of the DragEnter method
string formats = string.Join( "\n", e.Data.GetFormats(false) );
MessageBox.Show( formats );
which will dump data formats associated with the d'n'd operation. Might help us narrowing down where the problem lies.
I added the code that arul mentioned and things still didn't work, but it got me thinking.
I started thinking it might be a Vista issue so I sent it to a friend that had Windows XP and it worked great! I then tried running it outside of the Release folder in the bin directory and what do you know it worked!
The only time it does not work is when I am running it inside the Visual Studio 2008 IDE ... that's just weird.
Did you try to add the STAThread attribute to the main method?
[STAThread]
static void Main(string[] args)
{
}
I had the same problem as #mattruma meaning i got not Drag&Drop events.
After adding the STAThread attribute to the main method it worked as expected.

Categories

Resources