Problem with Listbox and removing items... [modified]
-
Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:
private void button1_Click(object sender, EventArgs e)
{
// for getting the items ToLower case
string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });listBox1.Items.AddRange(VsiFajli); for (int i = 0; i < listBox1.Items.Count; i++) { // checks if the file is .pdf if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf") { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } } else // if its not pdf ... { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf"); listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } }
-
Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:
private void button1_Click(object sender, EventArgs e)
{
// for getting the items ToLower case
string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });listBox1.Items.AddRange(VsiFajli); for (int i = 0; i < listBox1.Items.Count; i++) { // checks if the file is .pdf if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf") { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } } else // if its not pdf ... { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf"); listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } }
-
The code is almost unreadable, please use the <pre> tags and use proper temporary variables instead of calling
listBox1.Items[j].ToString()
and others a hundred times. Also, did you know there's aEndsWith
method? regardsNot until now, i didnt know about ends with. Thank you. Anyway... yes, the code is a mess here in CP... but still, can sombody give me a clue for a better algorythm or something?
Regards, Matjaž
-
Hi. I'm making an application and have a lot of problems. First of all, i had trouble figuring out - how to get all the items to lower case. Ok, i fixed that... but the program doesnt work the way it should. So, the program should work this way: You start the app. Then u click the button to get all the files from directory and all subdirectories. This goes ok. Ok, but then, i want to remove all doubles in the list. If theres a file "BeBack.JPG" the app. should check if there's also "BeBack.PDF" or if theres "BlackWhite.PDF" and "BlackWhite_1.PDF" it should remove both entries from the listbox. My code doesnt do that :( i dont know why :( It also deosnt remove all .gif files :( Where did i go wrong? Please help? Here's the code:
private void button1_Click(object sender, EventArgs e)
{
// for getting the items ToLower case
string[] fajli = Directory.GetFiles(Application.StartupPath,"*.*",SearchOption.AllDirectories);
string[] VsiFajli = Array.ConvertAll<string,>(fajli, delegate(string s) { return s.ToLower(); });listBox1.Items.AddRange(VsiFajli); for (int i = 0; i < listBox1.Items.Count; i++) { // checks if the file is .pdf if (listBox1.Items\[i\].ToString() == listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4, 4) + ".pdf") { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + "\_1.pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } } else // if its not pdf ... { if (File.Exists(listBox1.Items\[i\].ToString().Remove(listBox1.Items\[i\].ToString().Length - 4,4) + ".pdf")) { string temp = listBox1.Items\[i\].ToString(); listBox1.Items.Remove(temp); listBox1.Items.Remove(temp.Remove(temp.Length - 4,4) + ".pdf"); listBox1.Items.Remove(temp.Remove(temp.Length - 4, 4) + "\_1.pdf"); //listBox1.Items.RemoveAt(i); } }
Perhaps you can make a tiny demo and debug the code. That will be faster to find out the problem.
:) I Love KongFu~