Drop Targets and COM in Visual Studio .NET

Maker QR Code in Visual Studio .NET Drop Targets and COM
Drop Targets and COM
QR Code Generation In .NET Framework
Using Barcode drawer for .NET framework Control to generate, create Denso QR Bar Code image in VS .NET applications.
When you enable a control as a target, you open yourself up to the possibility that the user will receive the cryptic message shown in Figure 824
Read QR Code In Visual Studio .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
Figure 824 Cryptic Drag-and-Drop Error Message
Barcode Generation In Visual Studio .NET
Using Barcode maker for .NET Control to generate, create bar code image in .NET framework applications.
Because drag and drop is a feature provided using COM, COM must be initialized on the UI thread for drag and drop to work Although NET is smart enough to lazily initialize COM on the running thread as needed, for reasons of efficiency it picks the UI-hostile Multi-Threaded Apartment (MTA) for the thread to join unless told to do otherwise Unfortunately, for drag and drop, the UI thread must join the Single-Threaded Apartment (STA) To ensure that that's the case, always double-check that the Main function on all your WinForms applications is marked with STAThreadAttribute: [STAThread] static void Main() { ApplicationRun(new Form1()); } (Note that STAThread is a C# shortcut for STAThreadAttribute) By default, any VSNET-generated code will contain this attribute on the Main function (even Console applications), but just in case it somehow goes missing, this is the first thing to check when you see the message box from Figure 824
Reading Barcode In Visual Studio .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
The Drop Source
QR Code Generation In Visual C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
With the target implemented, what's left is initiating a drag-and-drop operation from the drop source using the DoDragDrop method of the Control class DoDragDrop is almost always placed in the handler for a MouseDown event: void button3_MouseDown(object sender, MouseEventArgs e) { // Start a drag-and-drop operation DoDragDrop(button3Text, DragDropEffectsCopy); } The DoDragDrop method's first parameter is the data, which can be any object The second parameter is a combination of the drag-and-drop effects that the source supports For example,Figure 825 shows the button initiating the drag and drop
Encode QR-Code In VS .NET
Using Barcode creation for ASP.NET Control to generate, create QR image in ASP.NET applications.
Figure 825 A Drag-and-Drop Operation Showing the None Effect
Quick Response Code Creation In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create QR-Code image in VS .NET applications.
As the drag-and-drop operation progresses, the DoDragDrop method tracks the mouse as it moves over controls, looking to see whether they are potential drop targets (as set with the AllowDrop property) and firing the DragEnter event to see whether potential targets can accept the data Depending on whether the target can accept the data, DoDragDrop sets the cursor based on the current effect indicated by the target to communicate to users what would happen if they were to drop at any point Notice in Figure 825 that the button itself is not a drop target, so the cursor indicates that a drop on the button would have no effect On the other hand, when the data is dragged over a text box that is enabled to accept string data, the DragEnter event is fired, and the control indicates the effect that it will support This causes the cursor to be updated appropriately, as shown in Figure 826
Code-39 Generator In Visual Studio .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 39 Extended image in .NET applications.
Figure 826 Drop Target Indicating the Copy Effect
Drawing Code 128 Code Set C In Visual Studio .NET
Using Barcode encoder for .NET framework Control to generate, create Code-128 image in .NET applications.
When the user releases the mouse button, dropping the data, the DragDrop event is fired on the target, and the target accepts the data, as shown in Figure 827
Print EAN-13 In Visual Studio .NET
Using Barcode generator for VS .NET Control to generate, create UPC - 13 image in VS .NET applications.
Figure 827 Completed Drag-and-Drop Copy Operation
Painting UCC - 12 In .NET
Using Barcode creation for .NET framework Control to generate, create UPC-A image in VS .NET applications.
When the drag and drop is completed, the DoDragDrop method returns with the effect that was performed If the effect was a Move, the source knows to remove its copy of the data
Generate Ames Code In VS .NET
Using Barcode maker for .NET framework Control to generate, create ABC Codabar image in VS .NET applications.
Supporting Multiple Effects
Draw Barcode In .NET
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
If you want to support more than one effect, such as Copy and Move, you can check the KeyState property of the DragEventArgs structure KeyState is a set of flags that determines which keys are being pressed By Windows convention, the lack of modifier keys indicates a Move, the Ctrl modifier indicates a Copy, and the Ctrl+Shift modifier indicates a Link (which your application may or may not support) Unfortunately, the KeyState property is an integer, and WinForms provides no data type for checking the flags So you'll need your own, such as this KeyState enumeration:[8]
Create Barcode In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
The FlagsAttribute makes instances of the KeyState enumeration show up in a friendlier manner when debugging, such as "LeftMouse, CtrlKey" instead of "9"
Bar Code Creator In Visual C#.NET
Using Barcode creation for .NET Control to generate, create bar code image in VS .NET applications.
// KeyState Values (not available in WinForms) [FlagsAttribute] enum KeyState { LeftMouse = 1, RightMouse = 2, ShiftKey = 4, CtrlKey = 8,
Bar Code Drawer In VB.NET
Using Barcode creation for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
MiddleMouse = 16, AltKey = 32, } Because users may change the keys they're pressing at any time to get the effect they're looking for, you will want to notify the drag operation of which operation they are trying to do while the mouse is hovering To do this you check the DragEnter and DragOver events: void textBox3_DragEnter(object sender, DragEventArgs e) { SetDropEffect(e); } void textBox3_DragOver(object sender, DragEventArgs e) { SetDropEffect(e); } void SetDropEffect(DragEventArgs e) { KeyState keyState = (KeyState)eKeyState; // If the data is a string, we can handle it if( eDataGetDataPresent(typeof(string)) ) { // If only Ctrl is pressed, copy it if( (keyState & KeyStateCtrlKey) == KeyStateCtrlKey ) { eEffect = DragDropEffectsCopy; } else { // Else, move it eEffect = DragDropEffectsMove; } } // We don't like the data, so do not allow anything else { eEffect = DragDropEffectsNone; } } The SetDropEffect method makes sure that the data is a string because that is all we are expecting If it finds a string, it tests to see whether the Ctrl key is pressed If so, it specifies that the operation is a copy; otherwise, it specifies that it will do a move Figure 828 shows what the drag operation now looks like over the text box without the Ctrl key pressed, indicating a move effect
Data Matrix 2d Barcode Generation In Visual C#
Using Barcode generation for .NET framework Control to generate, create DataMatrix image in VS .NET applications.
Code39 Maker In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code 3 of 9 image in Visual Studio .NET applications.
Encoding ECC200 In VS .NET
Using Barcode creation for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.