• We just launched and are currently in beta. Join us as we build and grow the community.

How to Drag External File in the ListView Using C#

LegendaryErik

Whimsy Wizard
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
43
Likes
64
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
This time, I will teach you how to drag an external file in the ListView using C#. By using this kind of method this allows you to easily drag and drop an image in the Listview. You might encounter this kind of problem in programming, but you will only achieve it once you follow the steps below.
Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2

Add a ListView and PictureBox inside the Form just like shown below.
ps2_0.png

Step 3

Press F7 to open the code editor. In the code editor, declare an item in the listview control.

  1. ListViewItem lstviewItem;

Step 4

Double click the form and add the following code to setup a ListView and PictureBox properties in the first load of a form.

  1. listView1.

    AllowDrop

    =

    true

    ;
  2. listView1.

    View

    =

    View.

    Details

    ;
  3. listView1.

    AllowDrop

    =

    true

    ;
  4. listView1.

    Columns

    .

    Add

    (

    "File"

    )

    ;
  5. listView1.

    Columns

    [

    0

    ]

    .

    Width

    =

    500

    ;

  6. pictureBox1.

    SizeMode

    =

    PictureBoxSizeMode.

    StretchImage

    ;

Step 5

Write the following code for the DragEnter

event of the ListView.


  1. private

    void

    listView1_DragEnter(

    object

    sender, DragEventArgs e)
  2. {
  3. if

    (

    e.

    Data

    .

    GetDataPresent

    (

    DataFormats.

    FileDrop

    , false

    )

    ==

    true

    )
  4. {
  5. e.

    Effect

    =

    DragDropEffects.

    All

    ;
  6. }
  7. }


Step 6

Write the following code for the DragDrop

event of the ListView.


  1. private

    void

    listView1_DragDrop(

    object

    sender, DragEventArgs e)
  2. {
  3. try
  4. {
  5. string

    [

    ]

    file;
  6. file =

    (

    string

    [

    ]

    )

    e.

    Data

    .

    GetData

    (

    DataFormats.

    FileDrop

    )

    ;
  7. foreach

    (

    string

    files in

    file)
  8. {
  9. lstviewItem =

    new

    ListViewItem(

    files)

    ;
  10. listView1.

    Items

    .

    Add

    (

    lstviewItem)

    ;
  11. }

  12. }
  13. catch

    (

    Exception ex)
  14. {
  15. MessageBox.

    Show

    (

    "error : "

    +

    ex.

    Message

    )

    ;
  16. }

  17. }

Step 7

Double click the ListView and do the following code to display the image in the PictureBox when the ListViewItem is selected.

  1. if

    (

    listView1.

    SelectedItems

    .

    Count

    ==

    0

    )

    return

    ;
  2. pictureBox1.

    Image

    =

    Image.

    FromFile

    (

    listView1.

    SelectedItems

    [

    0

    ]

    .

    Text

    )

    ;
  3. this

    .

    Text

    =

    listView1.

    SelectedItems

    [

    0

    ]

    .

    Text

    ;

The complete sourcecode is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top