Mp3 player in c#. Problems with the playlist - c#

I´m currently working in a mp3 player and I want to make a playlist using a Listbox, this is what I have:
private void addSongToPlaylist_Click(object sender, EventArgs e)
{
OpenFileDialog dialog2 = new OpenFileDialog(); //Dialog to choose songs
dialog2.Multiselect = true; // To choose multiple songs in the same dialog
if (dialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files= dialog2.SafeFileNames;
paths = dialog2.FileNames;
for (int i = 0; i < files.Length; i++) // Add song to the playlist
{
listBox1.Items.Add(files[i]);
}
}
When I run the code it works properly if I select one or multiple songs in one single dialog.
The problem is that every time I press the "Add Song to playlist" button (to add multiple songs separately), the counter variable is reset to zero and therefore I´m not able to add more than one song separately.
I´ve tried declaring a global int variable named "songCount"
int songCount = 0;
To use it like this:
for (int i = 0; i < files.Length; i++) // Add song to the playlist
{
listBox1.Items.Add(files[songCount]);
}
songCount++;
When I run the code and I add a second song to the playlist, I get a 'System.IndexOutOfRangeException', I don´t know how to solve the problem. Please help.

Related

How to fix ListView.LargeImageList showing images twice

I am working on a piece of software, which compares memes and helps users organize memes on their computer. As a part of this I am using Windows.Forms to build a UI. This UI lets the user add folders to be checked for images, which can be compared to a set of known meme templates.
My issue arises when I try to show the user the found images. To do this I am using a ListView and the property LargeImageList to contain a tuple of the image and the name of the image file.
Here is the piece of code in question:
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (var file in files)
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(Image.FromFile(file));
}
foreach (var file in files)
{
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
This is an example of what the user sees when they first load in a folder. When the user tries to load in another folder this happens. It shows the images from the first folder, with the names of the image files from the second folder.
Does anyone know a fix for this issue? I have tried a variety of options in order to get around the issue. All from trying to clear the ImageList used to contain the images, to trying my hand at controlling when the ListView updates. None of this has worked. I have also tried googling the issue, but with no luck of finding a fix.
Thank you in advance.
If you want to show the content of a single folder at the time, then dispose of the objects in your ImageList.
If you instead want to show the content of more than one folder, you need to specify the new index of the image added. You're instead adding a new Item in the ListView using the same index reference:
int i = 0;
//(...)
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
The indexer (i) always starts from 0, thus the ListView Item will use the images in your Imagelist starting from the Image at Index[0] each time. The new images won't ever be shown.
You can use the ImageList.Images.Count value, representing the number of Images already added to the ImageList, as base and increment the indexer starting from this value:
private void button1_Click(object sender, EventArgs e)
{
int i = imageList1.Images.Count;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
foreach (var file in Directory.GetFiles(fbd.SelectedPath))
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(new Bitmap(file, true));
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
If you allow to remove an Image from the ListView, you should also remove it from the ImageList: this implies that you need to re-index all the ListView Items starting from the Item that follows the one removed.
Remember to dispose of the Images you remove from the ImageList.

Unity Button Delegates (AddListener)

I'm trying to instantie several button using prefab.
But I've got a problem to Delegate variable.
for(int i = 0; i < _partnerList.partner.Count; i++)
{
GameObject _instance_partner_button = Instantiate(_prefab_partner_button);
_instance_partner_button.name = "_button_"+_partnerList.partner[i].partner_name;
_instance_partner_button.transform.SetParent(GameObject.Find("_UI_menu_partner").transform, false);
// Change Value of The Button ----------------------------------------------------------------
Text _instance_partner_button_text = _instance_partner_button.GetComponentInChildren<Text>();
_instance_partner_button_text.text = _partnerList.partner[i].partner_name;
Button _instance_partner_button_button = _instance_partner_button.GetComponent<Button>();
int _tempInt = i;
// Delegate to Another Script ---------------------------------------------------------------
_instance_partner_button_button.onClick.AddListener(() => _scripts_myScripts_ui.AnimationMenuPartner(_tempInt));
// -------------------------------------------------------------------------------------------
}
The function AnimationMenuPartner() just Debug.Log(_tempInt);
No matter which button I press the result is the same.
I'm maybe missing something, as I see in this link it must be working correctly Link
Thanks you for your help.
Edit :
The Another Script with the function AnimationMenuPartner
public void AnimationMenuPartner(int _tempInt)
{
Debug.Log(_tempInt));
}
The log output print : "4"
Which is the length of the list.

How can i refresh one specific hWnd window every X minutes?

What i'm doing is capturing a screenshot of windows in the background.
In a ListBox i have items of windows i'm taking screenshots.
The idea is that the windows are in the background so i don't need to move the windows to the foreground each time.
In form1 constructor:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
Then in form1 i also have a RefreshWindowsList method:
private void RefreshWindowsList()
{
Graphics g;
g = Graphics.FromImage(img);
g.Clear(Color.Transparent);
buttonSnap.Enabled = true;
this.listBoxSnap.Items.Clear();
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList[i] = tt.Substring(0, index);
}
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
}
listBoxSnap.Select();
}
The problem is that if i will make a refresh if i will call this method every X seconds/minutes the ListBox will blink.
So i was wondering if there is a way to refresh only a specific window form the WindowSnap.GetAllWindows collection and not to refresh the ListBox.
So if i refresh the specific hWnd window and save the window image as screenshot on the hard disk i will not see any changes on the ListBox but i will have a collection of images saved on the hard disk.
The class WindowSnap is a bit long so i will give a link for it i don't want to add it all here:
WindowSnap.cs
And it's using also the class WindowSnapCollection.cs
WindowSnapCollection.cs
The idea again is somehow to refresh specific hWnd window frol the collection and to refresh the ListBox.
To disable flicker while clearing and reloading items of ListBox, you can use BeginUpdate() and EndUpdate() methods of ListBox control, for example in a timer with interval 1000 I wrote this code and there was no flicker in ListBox:
private void timer1_Tick(object sender, EventArgs e)
{
this.listBox1.BeginUpdate();
this.listBox1.Items.Clear();
for (int i = 0; i < 100; i++)
{
this.listBox1.Items.Add(i);
}
this.listBox1.EndUpdate();
}
Also you can put your new items in a list and then, add them using :
listBox1.Items.AddRange(yourItemsList.Cast<object>().ToArray());

C# listbox add data from another form

I'm a C# student and I'm a little stuck at on my midterm project.
I dropped my project and spec here: https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf
If you run the program, it will come to the last area I left off at..
private void btnAddScore_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList = tempScore; (was Form1.scoreList[i] = tempScore;)
}
txtScoresList.Text += Convert.ToString(tempScore) + " ";
}
There's a main form, a secondary add form, and a third and fourth form, all the controls are in place, just the wiring is what's left over.
(1) In the above code, there are supposed to be 3 scores passed to the main form, which, along with a student name string, are to populate the ListBox on the main form. I can't figure out how to access that ListBox, anytime I type "listStudents" nothing happens.
(2) I'm also not sure how to limit an input of only 3 scores when I'm clicking the "add" button 1 time, which means I know my for loop is probably completely wrong. I don't know if I should save those scores to an array, list, or individual vars, being that it can be 3 (or more, but 3 is fine) scores.
(3) When I hit "OK" on the AddNewStudent form, do I write my code there to populate the main form ListBox, or does it go in the main form?
Update:
private void Form1_Load(object sender, EventArgs e)
{
lbStudents.Items.Clear();
//something like
foreach (decimal i in scoreList2)
{
scoreList = scoreList2.ToString(); //gives me a cannot implicitly convert error
}
lbStudents.Items.Add(tempInfo1 + " " + scoreList2);
}
//I want the listbox to populate like "Name - |100| |90| |80|"
This code seems to me, to be correct, for getting the ListBox populated, but I'm unsure of how to add the entire contents of the list to a string, and then add that to the listbox.
This will get your code building and running.
Change the following declaration in form1
public static decimal[] scoreList = new decimal[3];
to
public static List<decimal> scoreList = new List<decimal>();
and update your btnAddScore_Click handler to
//save scores to temp static var, populate noread txtbox txtScoresList with scores
for (int i = 0; i < 3; i++)
{
//save score to static var for trans-form data sending
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList.Add(tempScore);
}
The rest is not too difficult, you should be able to work it out.

How to get the MediaPlayer Queue

In my application I am using the MediaPlayer to play a selection of songs, however it would be good to be able to get the songs that are going to be played next and display them to the user, either for information, or so they can jump several songs in the queue.
So how can I get a list of the Songs that are coming up in the MediaPlayer queue?
So that I can have a list of them which can be displayed to the user, in some sort of container, probably a ListBox.
I have tried using MediaPlayerQueue however this operator doesn't seem to have any form of functionality?
Thank you for your time and help.
media player in windows phone 7 displaying next song
MediaPlayer.Queue[n]
will give you the required Song instance
You can try this one. It work for wp8.1 silverlight.
SongModel is my custom class
App.Handler.MediaPlayerQueqeLists = new ObservableCollection<SongModel>();
for (int i = 0; i < MediaPlayer.Queue.Count; i++)
{
App.Handler.MediaPlayerQueqeLists.Add(new SongModel
{
Index = i,
Title = MediaPlayer.Queue[i].Name,
Artist = MediaPlayer.Queue[i].Artist.Name,
StringDuration = (new DateTime(MediaPlayer.Queue[i].Duration.Ticks)).ToString("mm:ss")
});
}
llsPlaylist.ItemsSource = App.Handler.MediaPlayerQueqeLists;

Categories