Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys
When I drag-drop to TextBox, it drops two times. One from MainWindow, and two from TextBox.
Can anyone kindly help me on this?
I want to drop only one time.
Thanks

What I have tried:

XML
<Window x:Name="TheMainWindow" x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SRT2Nuendo"
        mc:Ignorable="d"
        Title="MyDropTest"
        Height="500"
        Width="1000"
        WindowStartupLocation="CenterScreen"
        WindowStyle="None"
        AllowDrop="True"
        Drop="FileDrop">

    <TextBox x:Name="tbSaveCSV"
    HorizontalContentAlignment="Left"
    AllowDrop="True"
    Drop="FileDrop"/>
</Window>



VB
Private Sub FileDrop(sender As Object, e As DragEventArgs)
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            Dim DroppedItems As String() = e.Data.GetData(DataFormats.FileDrop)
            Dim TheFile As String = IO.Path.GetFullPath(DroppedItems(0))
            MsgBox (TheFile.Name)
        End If
    End Sub
Posted
Updated 3-Jul-23 21:29pm
v4

Routed events bubble up to the root. Add this following to your method and it should stop the event bubbling and causing it to fire twice:
C#
e.Handled = true;

More information here: RoutedEventArgs.Handled Property (System.Windows) | Microsoft Learn[^] [^] and read more about routed events here: Routed events overview - WPF .NET | Microsoft Learn[^]
 
Share this answer
 
Comments
Sh.H. 4-Jul-23 5:39am    
@Graeme_Grant Worked! Thank you! :-)
Graeme_Grant 4-Jul-23 5:49am    
Glad to hear!
Without your code, we can't tell - but at a guess you have added the handler to the same event multiple times. Do that, and both will be called when you drop data onto it.
 
Share this answer
 
Comments
Sh.H. 4-Jul-23 0:36am    
@OriginalGriff I have written the code!!!! May be you should hit F5 to refresh your browser to see the code.
OriginalGriff 4-Jul-23 0:45am    
:sigh:
You have shown us a tiny fragment, but the handler is added elsewhere and we can't see that (or those) bits of code ...
Sh.H. 4-Jul-23 1:27am    
Come on! The handler is under the code with name Sub FileDrop!!! I have uploaded both main code and the handler code!!! I am surprised why you cannot see both of them!!!!!!!
Sh.H. 4-Jul-23 0:39am    
@OriginalGriff Also I should add that, as you can see in the code, both MainWindow and TextBox has handle to Sub FileDrop.
So when I drag-drop on the TextBox, 1st TextBox calls FileDrop, then MainWindow calls FileDrop. So this is why the sub calls two times. and this is what I DON'T want.
OriginalGriff 4-Jul-23 0:47am    
So remove the call to drop from the main window and just leave the text box - or vice versa depending on where you want the user to have to drop the data.

If you have two Drop calls, they *BOTH* get honoured if the user drops in the right place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900