adding files to listview in C# - c#

I have a Listview and an "ADD" button,when I click on ADD i should be able to browse the files in computer, select files and when click OK or the Open, the file list should be added in the listview...how to do that...is listview correct or any other alternative...?

ListView should be fine for file listing. Just be aware that longer file paths are difficult to see (have to horizontally scroll which is bad!) if you are gonna just add the full path to list. You can toy with the idea of other representation like:
File.Txt (C:\Users\Me\Documents)
C:\Users\..\File.Txt
etc
As far as doing it using code is concerned, you will need to use OpenFileDialog control to let user choose the files.
var ofd = new OpenFileDialog ();
//add extension filter etc
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
if(ofd.ShowDialog() == DialogResult.OK)
{
foreach (var f in openFileDialog1.FileNames)
{
//Transform the list to a better presentation if needed
//Below code just adds the full path to list
listView1.Items.Add (f);
//Or use below code to just add file names
//listView1.Items.Add (Path.GetFileName (f));
}
}

If you want to do this in the designer, you can take the following steps to add the images to the ListView control:
Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
Click the smart tag, and click "Choose Images" on the pane.
On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
Click OK to finish adding images to the ImageList.
Click the ListView on the form, there will be a smart tag appear on the top-right corner.
Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
Click OK to finish item editing, now you'll find the images are displayed on the ListView.
If you want to add the images to the ListView by code, you can do something like this
give the following code in addButton_click
var fdlg = new OpenFileDialog();
fdlg.Multiselect = true;
fdlg.Title = "Select a file to add... ";
fdlg.InitialDirectory = "C:\\";
fdlg.Filter = "All files|*.*";
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
foreach (var files in fdlg.FileNames)
{
try
{
this.imageList1.Images.Add(Image.FromFile(files));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}

Related

How Do I use Drag And Drop To Export A File (C#)

I am currently using a file dialog to export a file, but I was wondering how I could export my file using drag and drop. I couldn't figure out how to get the file path of where the item is being dropped. Here is the code that I used for open file dialogue in case its required.
if (this.listView1.SelectedItems.Count > 0)
{
ListViewItem item = this.listView1.SelectedItems[0];
string text = this.faderLabel8.Text;
if (!text.EndsWith(#"\"))
{
text = text + #"\";
}
using (SaveFileDialog dialog = new SaveFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
Jtag.ReceiveFile(item.SubItems[0].Text, text + item.SubItems[0].Text);
}
}
}
You don't need the path of where the file is being dropped. Instead you need to create a temporary file.
Save file to a temporary folder
Initiate drag on an event/command, such as mouse down, in the following way:
//(This example is uses WPF/System.Windows.DragDrop)
//Create temporary file
string fileName = "DragDropSample.txt";
var tempPath = System.IO.Path.GetTempPath();
var tempFilePath = System.IO.Path.Combine(tempPath, fileName);
System.IO.File.WriteAllText(tempFilePath, "Testing drag and drop");
//Create DataObject to drag
DataObject dragData = new DataObject();
dragData.SetData(DataFormats.FileDrop, new string[] { tempFilePath });
//Initiate drag/drop
DragDrop.DoDragDrop(dragSourceElement, dragData, DragDropEffects.Move);
For WinForms example and more details see:
Implement file dragging to the desktop from a .net winforms application?
If you want it to be useful through "drag and drop" you would require some sort of graphical interface that displays the files in a container where they are and then another container to where you want to move them. When you highlight an item with your mouse you add them to your itemList and when your drop them you copy them. Just make sure the List is emptied once in case you remove the highlight.

How to add X image over the image for remove in ImageListview dll

In my windows application I am using the ImageListView.dll displaying selected images.
I used the following code for Adding the selected images to ImageListView
string folder = Photo_Care.Properties.Settings.Default.LastFolder;
if (Directory.Exists(folder))
openFileDialog1.InitialDirectory = folder;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
folder = Path.GetDirectoryName(openFileDialog1.FileName);
Properties.Settings.Default.LastFolder = folder;
Properties.Settings.Default.Save();
imageListView1.Items.AddRange(openFileDialog1.FileNames);
}
And I can able to remove the selected Image from Image List using the below code.
imageListView1.SuspendLayout();
// Remove selected items
foreach (var item in imageListView1.SelectedItems)
imageListView1.Items.Remove(item);
// Resume layout logic.
imageListView1.ResumeLayout(true);
But Now I want to add the close button right top of the added image instantly for removing the image. Like Below Image

C# List text files in a listbox and let user open them in a textbox

I'am pretty new to C# and I want a listbox with all text files in a folder and if the user dubbleclicks on the listed file the file will display in a textbox.
I don't want to use the openFileDialog function because the textfiles are on a webserver which I access using username:password#server.com/folder.
Kinda like a texteditor limited to edit only files in 1 folder :)
If this is possible using openFileDialog please tell me how.
I hope you can understand what I want to do.
Greetings,
From what I understand you are after, you want to iterate through the files in a specific directory and then allow them to be edited once opened with a double click in a listbox.
This can be done using the var Files = Directory.GetFiles("path", ".txt");
Files would be a string[] of the file names.
Then filling the Listbox with the files something like this:
ListBox lbx = new ListBox();
lbx.Size = new System.Drawing.Size(X,Y); //Set to desired Size.
lbx.Location = new System.Drawing.Point(X,Y); //Set to desired Location.
this.Controls.Add(listBox1); //Add to the window control list.
lbx.DoubleClick += OpenFileandBeginEditingDelegate;
lbx.BeginUpdate();
for(int i = 0; i < numfiles; i++)
lbx.Items.Add(Files[i]);
lbx.EndUpdate();
Now your event delegate should look something like this:
OpenFileandBeginEditingDelegate(object sender, EventArgs e)
{
string file = lbx.SelectedItem.ToString();
FileStream fs = new FileStream(Path + file, FileMode.Open);
//Now add this to the textbox
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
tbx.Text += temp.GetString(b);//tbx being the textbox you want to use as the editor.
}
}
Now to add an event handler through the VS window editor click on the control in question and go to the properties pane for that control. You will need to then switch to the events pane and scroll untill you find the DoubleClick event if you use that the designer should auto-insert a valid delegate signature and allow you to write the logic for the event.

listview in c# with images [duplicate]

This question already has answers here:
Images in ListView subitem
(2 answers)
Closed 9 years ago.
I wish to make a list of items with pictures, the amount of items can vary from 1-60 and for each item I wish to also show data.
I believe the best way of going about this is using the ListView in c#.
is this true and if so how would I go about doing this?
i have also thought about using interactive images within a scrolling window
If you want to do this in the designer, you can take the following steps to add the
images to the ListView control:
Switch to the designer, click on the ImageList component on the Component Tray,
there will be a smart tag appear on the top-right corner of the ImageList.
Click the smart tag, and click "Choose Images" on the pane.
On the pop-up Image Collection Editor dialog, choose the images from the folder
your want.
Click OK to finish adding images to the ImageList.
Click the ListView on the form, there will be a smart tag appear on the top-right
corner.
Click the smart tag, you will find there're three ComboBoxes there, choose a
ImageList from the list as you want.
Click the "Add items" option on the smart tag, a ListViewItem Collection Editor
will appear, you can add items to the ListView, it's important here to set the
ImageIndex or ImageKey property, or the image won't appear.
Click OK to finish item editing, now you'll find the images are displayed on the
ListView.
If you want to add the images to the ListView by code, you can do something like this`
Code Snippet
private void Form10_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(#"c:\pic");
foreach (FileInfo file in dir.GetFiles())
{
try
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
catch{
Console.WriteLine("This is not an image file");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}
Source

adding image to listView

I have this problem. I want to add image to listView. Exactly I want use openFileDialog for choose image on disc, load file to aplication and show them in listView.
Now I do it like this:
openFileDialog1.Filter = "png (*.png)|*.png";
openFileDialog1.Multiselect = true;
if ( openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
foreach ( var pngFile in files ) {
try {
Bitmap image = new Bitmap( pngFile );
imageList1.Images.Add( image );
} catch {
}
}
listView1.LargeImageList = imageList1;
listView1.Refresh();
}
But it doesn't work. What do I make wrong?
edit
I get blank listView. Nothing error.
Well, that's fine. But you only added an image to the image list. You haven't modified an item in the list view that actually uses that added image. Add this line of code and tweak as necessary:
listView1.Items.Add(new ListViewItem("Added an image", imageList1.Images.Count - 1));
Also ensure that listView1.LargeImages = imageList1. You set that in the designer.

Categories

Resources