Listview without an Image list? - c#

once again i have a problem that i can't quite seem to come up with a solution to. so here it is, I have a ListView displaying the directories of Image files,i want the listview to display these images for these files, the problem is I also need the images to be modified by the program at a per-pixel level so i have this done on a separate thread, so what i want to do is take my already existing PictureBox list of the modified Images and match up the names of the files with the corresponding image. Any ideas on how to do this?
here is what i have so far
public static List<PictureBox> ContentItems = new List<PictureBox>();
...
public static string ContentDirectory = "";
private void FileTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
FileList.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
item = new ListViewItem(file.Name);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
FileList.Items.Add(item);
}
FileList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}

I Did have to use an image list after all Heres how i got it to work:
void FileTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
FileList.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
ContentImg.Images.Clear();
int CurrentImg = 0;
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
string fileName = file.Name;
foreach (PictureBox PB in ContentItems)
{
if (fileName == PB.Name)
{
//Get Image
ContentImg.Images.Add(PB.Image);
item = new ListViewItem(file.Name, CurrentImg);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
FileList.Items.Add(item);
CurrentImg += 1;
}
}
}

Related

Get and return filepath by foreach

I want to make a program to able to save every file path which the user selected.
after that do some prosses for each file. for example, convert video file one by one.
Could you tell me why foreach does not work?
private void btnInput_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogInput = new OpenFileDialog();
openFileDialogInput.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
openFileDialogInput.Filter = "Video Files|*.mp4|TS Files|*.ts";
openFileDialogInput.Multiselect = true;
openFileDialogInput.FilterIndex = 1;
DialogResult result = openFileDialogInput.ShowDialog();
string [] inputPath = openFileDialogInput.FileNames;
foreach (var item in inputPath)
{
item;
}
}
inputPath gets all file paths that the user selected. but I don't know how can I get them, one by one and make some prosses on them.
You Can try this:
private void AddWatermark(string videoFilePath)
{
// Add your logic here to add watermark
}
And in the foreach loop:
foreach (var item in inputPath)
{
AddWatermark(item);
}

C# Open listView Item from treeView item location

I'm needing a way to allow the files that are displayed in the ListView to be opened. The Items In the ListView are displayed from the TreeView. Take a look at my code below to see in more detail.
Code for this form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace OSTP
{
public partial class User1FileExplorer : Form
{
public User1FileExplorer()
{
InitializeComponent();
PopulateTreeView();
this.treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
}
private void User1FileExplorer_Load(object sender, EventArgs e)
{
}
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(#"C:\Users\Oliver\Documents\.OSTP\User1\Files\Documents");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
}
}
As you can see from my code I use a TreeView to select the directory for the files then the ListView to display the files inside of the directory.
I would like it so when the user double clicks the file in the ListView it opens. And when I say open I'm not meaning open a text file as such. So lets say the user double clicks text file 1 in the ListView, I would like it to show User1TextFile1.cs. This is because the text files are loaded into a text box.
I know this is a little complicated, so If I have missed anything please drop a comment.
Thanks.
UPDATE
http://pastebin.com/BgVdLavL
UPDATE2
When I add MessageBox.Show("" + lvHti);
To find out which ListViewItem was clicked or doubleclicked use the MouseClick or the MouseDoubleClick event.
Here you can either code simply:
ListViewItem lvItem = null;
if (listView1.SelectedItems.Count > 0)
lvItem = listView1.SelectedItems[0];
provided the ListView has MultiSelect = false.
If you allow multiple selections you need to do a HitTest to find out where the user has clicked:
ListViewItem lvItem = null;
ListViewHitTestInfo lvHti = listView1.HitTest(e.Location);
if (lvHti.Item != null) lvItem = lvHti.Item;
Now you can access the Item and/or its SubItems to process the choice.

Show filename in listView and imagelist. Show filename of selected item

I have setup the imageList and the listView. Ideally I would like the filename to show under the image.
UPDATED
using the item.Tag mentioned below I was able to move some things around. I now have the file name tracking which file is selected. Now I just have to see if I can get the file name to show up in the listview.
Here's the new code:
private void GetImages()
{
DirectoryInfo dir = new DirectoryInfo(#"c:\pics");
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(67, 100);
this.listView1.LargeImageList = this.imageList1;
int j = 0;
foreach (FileInfo file in dir.GetFiles())
{
try
{
imageList1.Images.Add(file.Name, Image.FromFile(file.FullName));
ListViewItem item = new ListViewItem(file.Name);
item.Tag = file.Name;
item.ImageIndex = j;
this.listView1.Items.Add(item);
j++;
}
catch
{
Console.WriteLine("This is not an image file");
}
}
}
private void listView1_Click(object sender, EventArgs e)
{
ListViewItem item = listView1.SelectedItems[0];
label1.Text = item.Tag.ToString();
int itemIndex = listView1.SelectedIndices[0];
Image img = imageList1.Images[itemIndex];
pictureBox1.Image = img;
}
Ok I think I've simplified some of the other answers. With this way I'm able to get the filename to show up under the image. Also when the image is selected I'm able harness it.
I'm using the item.tag to hold the file name.
private void GetImages()
{
DirectoryInfo dir = new DirectoryInfo(#"c:\pics");
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(100, 100);
this.listView1.LargeImageList = this.imageList1;
int j = 0;
foreach (FileInfo file in dir.GetFiles())
{
try
{
//this.imageList1.Images.Add(Image.FromFile(file.FullName));
imageList1.Images.Add(file.Name, Image.FromFile(file.FullName));
ListViewItem item = new ListViewItem(file.Name);
item.Tag = file.Name;
item.ImageIndex = j;
this.listView1.Items.Add(item);
j++;
}
catch
{
Console.WriteLine("This is not an image file");
}
}
}
Now in either the listview1_Click or some other call to action I am able to use the follow to harness the filename tag.
ListViewItem item = listView1.SelectedItems[0];
txtNewWM.Text = item.Tag.ToString();

Treeview - Adding images files

I have code and i need add to treeview only (image files) or image files higlights by other color. Do you have any ideas ? I will like for every advice. I am adding for example Download Folder, in which i have some jpeg, some avi etc. I need images with other color or add just jpeg,png and other image files.
System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
Image img = new Image();
public MainWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
dlg.Description = "Vyberte složku, kterou přidat";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtbox1.Text = dlg.SelectedPath;
ListDirectory(treeView1, dlg.SelectedPath);
}
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
TreeViewItem SelectedTreeViewItem = treeView1.SelectedItem as TreeViewItem;
string FileName = "";
if (SelectedTreeViewItem != null)
{
FileName = SelectedTreeViewItem.Header.ToString();
}
{
canvas1.Children.Remove(img);
img.Source = new BitmapImage(new Uri(dlg.SelectedPath + "\\" + FileName));
img.Width = 250;
img.Height = 185;
canvas1.Children.Add(img);
}
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
canvas1.Children.Remove(img);
}
private void ListDirectory(TreeView treeView, string path)
{
treeView.Items.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Items.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeViewItem { Header = directoryInfo.Name };
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Items.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles())
directoryNode.Items.Add(new TreeViewItem { Header = file.Name});
return directoryNode;
}`
You can use LINQ to filter appropriate files:
private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeViewItem { Header = directoryInfo.Name };
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Items.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles().Where(x=> x.Extension == ".jpg" || x.Extension == ".png"))
directoryNode.Items.Add(new TreeViewItem { Header = file.Name });
return directoryNode;
}
Following code is responsible for selection only pictures:
directoryInfo.GetFiles().Where(x=> x.Extension == ".jpg" || x.Extension == ".png")

How can I load a folders files into a ListView?

I'd like to have a user select a folder with the FolderBrowserDialog and have the files loaded into the ListView.
My intention is to make a little playlist of sorts so I have to modify a couple of properties of the ListView control I'm assuming. What properties should I set on the control?
How can I achive this?
Surely you just need to do the following:
FolderBrowserDialog folderPicker = new FolderBrowserDialog();
if (folderPicker.ShowDialog() == DialogResult.OK)
{
ListView1.Items.Clear();
string[] files = Directory.GetFiles(folderPicker.SelectedPath);
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem item = new ListViewItem(fileName);
item.Tag = file;
ListView1.Items.Add(item);
}
}
Then to get the file out again, do the following on a button press or another event:
if (ListView1.SelectedItems.Count > 0)
{
ListViewItem selected = ListView1.SelectedItems[0];
string selectedFilePath = selected.Tag.ToString();
PlayYourFile(selectedFilePath);
}
else
{
// Show a message
}
For best viewing, set your ListView to Details Mode:
ListView1.View = View.Details;
A basic function could look like this:
public void DisplayFolder ( string folderPath )
{
string[ ] files = System.IO.Directory.GetFiles( folderPath );
for ( int x = 0 ; x < files.Length ; x++ )
{
lvFiles.Items.Add( files[x]);
}
}
List item
private void buttonOK_Click_1(object sender, EventArgs e)
{
DirectoryInfo FileNm = new DirectoryInfo(Application.StartupPath);
var filename = FileNm.GetFiles("CONFIG_*.csv");
//Filename CONFIG_123.csv,CONFIG_abc.csv,etc
foreach(FileInfo f in filename)
listViewFileNames.Items.Add(f.ToString());
}

Categories