Filter an Array  
Author Message
TRTwiggy





PostPosted: Visual C# General, Filter an Array Top

I have an array that is loaded with any files that the user has selected in a file browser dialogue. Right now the array gets loaded with everything in the folder, but I just want it to load image files that can be used as a wallpaper. Also I would like it to grab anything that is in any sub directories as well.

Visual C#10  
 
 
ahmedilyas





PostPosted: Visual C# General, Filter an Array Top

can you show the code you are viewing

in regards to showing anything in the subdirectories for files....

string[] theFiles = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

in regards to just showing the image, why are you storing the file in an array why dont you just do an Image.FromFile(fileOpenDialog.FileName) on the pictureBox

this.thePictureBox.Image = Image.FromFile(fileOpenDialog.FileName);

 



 
 
TRTwiggy





PostPosted: Visual C# General, Filter an Array Top

Ok here is the code that I have that adds it to the array. I am trying to make a program that will change the users wallpaper to a random image out of all the ones that they selected. As far as putting it in a picture box like you show the only one that I want in the picture box is the one that the user clicks on in the Checked List Box. Do you suggest another method to add files to the checked list box instead of using an array first

private void SelectWallpaperDirectory()
{

FolderBrowserDialog fbdWallpaperDirectory = new FolderBrowserDialog();

fbdWallpaperDirectory.Description = "Select the folder containg you wallpapers.";

if (fbdWallpaperDirectory.ShowDialog() == DialogResult.OK)
{
string[] selectedwallpapers = Directory.GetFiles(fbdWallpaperDirectory.SelectedPath.ToString());

foreach (string wallpaper in selectedwallpapers)
{

clbSelectedWallpapers.Items.Add(wallpaper,CheckState.Checked);
}

}


 
 
ahmedilyas





PostPosted: Visual C# General, Filter an Array Top

you could also do this:

foreach(string currentFile in Directory.GetFiles(fbdWallpaperDirectory.SelectedPath))

{

   this.clbSelectedWallpapers.Items.Add(currentFile, false);

}

 

this will not select the items initially (unchecked)

when the user checks them, perhaps place a button which does the randomization.

private void cmdPickPicture_Click(object sender, EventArgs e)

{

   Random theRandomGen = new Random();

   int valueChosen = theRandomGen.Next(0, this.clbSelectedWallpapers.CheckedItems.Count);

   this.thePictureBox.Image = Image.FromFile(this.clbSelectedWallpapers.CheckedItems[valueChosen].ToString());

}

 

does this work is this what you are after



 
 
TRTwiggy





PostPosted: Visual C# General, Filter an Array Top

OK that first part looks good, I will try the random part when i get there I wanna finish the adding the files part first. I guess the reason I added it to the array first was because I want to add Drag & Drop funtionality to it as well so the user can also drag images incase they aren't all in the same folder. I am at work right now so I can't try that out yet until I get home, but as soon as i get off I will try it and let you know how it worked for me. Thank you for the response.


 
 
ahmedilyas





PostPosted: Visual C# General, Filter an Array Top

no worries at all. Hope it does help in some way. I'm sure other people will help too, since I will be away for a few days but will be trying to still help whilst on the move!

Good luck