C# WinForms DragEnter never fires - c#

I'm puzzled with this. I attempted implementing drag and drop on a DataGridView. Failing to see any events fired, I tried a simple form, with a text box.
I would like to be able to drag files or folders from Windows Explorer.
I'm missing something because these events never fire. I did read about DragEvents, Windows 7 and UIPI but I still couldn't get around this.
I'm out of ideas and I welcome your suggestions.
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
textBox1.AllowDrop = true;
textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
textBox1.DragOver += new DragEventHandler(textBox1_DragOver);
}
void textBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void textBox1_DragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
It seems that this should work. I have a clean install on WP7 64 - with all updates, I don't have virus or malware protection running, or anything (to my knowledge) which could prevent these events firing.

I had the same issue. it was only because I was debugging from a "run as administrator" session. I think that since VISTA there is a security that prevents from dropping to a privileged application.

I found that while I was running my Forms application in debug mode from Visual Studio, it didn't work. Only when I ran it outside of VS does it work perfectly. Presumably this is also something to do with security on Windows 7 (and possibly later versions).

Related

MAUI - How to Set Window to be Always on Top?

How can I make a .NET MAUI app to always be on top, top must, always visible?
I'm looking for an equivalent of Topmost="True" that we have in WPF.
I tried managing the UnFocused event and calling Focus(), but I had no luck:
private void Shell_Unfocused(object sender, FocusEventArgs e)
{
(sender as AppShell).Focus();
}
private void Shell_Disappearing(object sender, EventArgs e)
{
(sender as AppShell).Focus();
}
As suggested by Ralf, TopMost is a Windows Feature and that's a feature request can be tracked here:https://github.com/dotnet/maui/issues/8198.
If you want the feature works in Mac, you can follow up with Add "Topmost" shell/app feature
.

Detect if AdControl (Microsoft Advertising SDK) is clicked

I tried IsEngagedChanged, PointerPressed/PointerReleased, none of those events is fired when I click the AdControl. (AdRefreshed event does occur)
I am developing an Windows Store App in C#/XAML.
<ad:AdControl Height="90" x:Name="ad1" Width="728" AdUnitId="10042998" ApplicationId="d25517cb-12d4-4699-8bdc-52040c712cab" IsEngagedChanged="ad1_IsEngagedChanged_1" AdRefreshed="ad1_AdRefreshed_1" PointerPressed="ad1_PointerPressed_1" />
private void ad1_IsEngagedChanged_1(object sender, RoutedEventArgs e)
{
//not fired
}
private void ad1_AdRefreshed_1(object sender, RoutedEventArgs e)
{
//fired
}
private void ad1_PointerPressed_1(object sender, PointerRoutedEventArgs e)
{
//not fired
}
I want to award the user for clicking on the Ad.
The IsEngagedChanged-event does actually fire, but not really when you expect it to. If you use ads which causes the ad to go full-screen (click-to-fullscreen) the IsEngagedChanged-event will fire when a user clicks it. If you use a normal ad (which you did according to your adUnitId) which goes to some link, the event will not fire. It's stupid as hell.
As Walt Ritscher pointed out, this is in the docs:
Raised when the user clicks the ad, and is interacting with it rather
than the app.
Which makes one believe it would get called on any click, but it doesn't. There's so many things missing/"wrong" in the Microsoft Advertising SDK that you wanna cry.
Hope this helped someone.

Drag'n'drop to a Windows form issue

I have, what should be, a simple question on drag'n'drop. I have a fresh Win Form project where the form has set to allow drops using AllowDrop = true. Should also mention that I am running Windows 7 64-bit.
Just to be sure, I have subscribed to
this.DragDrop += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);
as well.
But when I run the app and drag anything from my desktop or explorer, it indicates with the mouse pointer icon that I am not allowed to drop any file(s) to it after all.
I found a a similar question like this one (but Win Vista) where the issue was that Visual Studio was running with admin priveleges which windows explorer wasn't. But building the app and running the executable results in the same problem.
I have done this many times in the past and couldn't Google my way to solve this one. What am I missing?
By default the target drop effect of a drag-and-drop operation is not specified (DragDropEffects.None). Thus there is no drop event for your control in this case.
To allow Control to be a drag-and-drop operation's receiver for the specific data you should specify the concrete DardDropEffect as shown below (use the DragEnter or DragOver events):
void Form1_DragDrop(object sender, DragEventArgs e) {
object data = e.Data.GetData(DataFormats.FileDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e) {
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
}
}
Related MSDN article: Performing a Drag-and-Drop Operation in Windows Forms
You are using the wrong Event, use the DragEnter Event.
this.DragEnter += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);

SystemEvents.SessionEnding not firing

I am developing an windows forms application in c# .net 4.0.
I want to capture windows logoff event.
Here is the code:
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnding += (s, e) =>
{
if (e.Reason == SessionEndReasons.Logoff)
{
MessageBox.Show("LogOff");
}
else if (e.Reason == SessionEndReasons.SystemShutdown)
{
MessageBox.Show("ShutDown");
}
};
}
Why isn't my sessionEnding firing?
It depends on the configuration that is set on gpedit.msc.
Open gpedit.msc, navigate to Computer Configuration > Administrative Templates > System > Shutdown Options and choose Turn off automatic termination of applications that block or cancel shutdown. Since mine laptop configure make it automatic shutdown, so it will never fire session ending
Perhaps you can move your code above into entry point of its windows (in the main).
Perhaps you can override windows message. You can see it in MSDN library documentation.
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
Shutdown message pump has been re route by other software and not re route to your apps
This could be useful to someone.
if form close event is included
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
then SessionEnding will not be fired, i just encoutered this problem and rectified it
void SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e)
{}
here i need to prevent Form close upon Alt+F4 command, so i added this form closing event this resulted in this issue. so we can integrate session ending event in form close event. Option 2 in Refered from stackoverflow answer

Add serial port to web page

Is it possible to add a serial port control to a web application. I've tried creating one programmatically, but i have issues with the port staying open. I'm not sure how to fix that except by adding a serial port control somehow to the web page. Any ideas on how I can accomplish this task? Thanks in advance.
the following is the code I currently have:
public partial class LoadCellTest : System.Web.UI.Page
{
SerialPort serialPort1 = new SerialPort("COM3",9600,Parity.None,8,StopBits.One);
protected void Page_Load(object sender, EventArgs e)
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort1_ErrorReceived);
}
delegate void SerialDataReceivedDelegate(object sender, SerialDataReceivedEventArgs e);
delegate void SerialErrorReceivedDelegate(object sender, SerialErrorReceivedEventArgs e);
protected void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
TextBox1.Text = (serialPort1.ReadExisting());
if (serialPort1.ReadExisting().Length == 0)
{
ListBox1.Items.Insert(0, TextBox1.Text);
TextBox1.Text = "";
}
}
protected void button1_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
button1.Text = "Start";
}
else
{
serialPort1.Open();
button1.Text = "Stop";
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}
Turning my comments into an answer...
The serial port code is server-side code. You can't do it on the client with ASP.NET.
Creating an ActiveX or other fat client control is a load of work, and just not a good idea.
My recommendation would be to continue on with your WinForms app for the code where you need the scanner, and add a menu to it to enable you to launch a separate ASP.NET web app for the reports/data access.
If you want to make it more "seamless" for the users, you can add a form with a WebBrowser control that loads your report/data access site. To them it will just be a "part of the application".
THAT SAID, depending on the device you have connected to the port, there may be an even simpler option.
One of our barcode scanners comes with software that just takes barcode data as it's scanned and pastes it into whatever open document has focus. If you're working in Notepad, the scanned data is pasted into Notepad. If you have a web app open, and the cursor in a text box, the data simply pastes in there.
It's a simpler option to implement, BUT it's harder on the users, because if they're not technical, they're going to call you wondering why the barcode beeps but doesn't populate the text box. (The answer will be "Because your cursor isn't in the text box or the form doesn't have focus")
So I go back to recommendation #1.
Your code just accesses the serial port of the server since it is running on the server...
IF you really need to access the serial port on the client from a web app then you will need to use some technology that runs directly on the client... this could be an ActiveX control embedded into your web page...
I am really not sure perhaps a Silverlight application embedded into your web page could achieve this too...
Beware that doing so in a web page is a possible security problem !

Categories

Resources