Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
There are 2 listboxes......listbox1 and listbox2

listbox1.datasource=datafile "datafile is file with items"

listebox1 has those items:

BBBBBB
------
CCCCC
------
AAAAAA
------

and listbox2 is empty.

now I want those data from listbox1 to be sorted i listbox2 but without string "----"
I have code for sorting them, but I dont know how to write code "sort them but don't take string "-----------" so sort them like that:
AAAAAA
BBBBBB
CCCCCC

always think that there is listbox1.datasource not listbox1.item


Thankyou !


Edit: Additional message of the OP moved here from solution
my solution is that, bur stil dooesnt work

C#
 string[] dataFromFile = lstQiftet.Items.Cast().ToArray();

 Array.Sort(dataFromFile);
 listBox1.DataSource = dataFromFile;

 ArrayList arr = new ArrayList();
  
 foreach (string s in dataFromFile)
 if (s != "-----------------")
arr.Add(s);

 arr.Sort(); 
listBox2.DataSource = arr;


why doesn't work

[Edit]Moved from non-solution of OP
THnx Thnx that work, but now after I delete "-----" , in first line is a space, I Try to delet that space like your code it isn't working.....

if (!s.StartsWith("-")&&!s.StartWith(" " ))..... is here any mistake or should be another code
[/Edit]

[Edit] Another non solution
thnx man friend I think I didnt explain as well you as I should

AAAA
----
BBBB
----
CCCC
----


afer i delet '-', then after sorted :

1
2. AAAA
3. BBBB
4. CCCC

the first line than is space.......so I dont want to be that space
[/Edit]
Posted
Updated 22-Apr-12 4:13am
v4
Comments
Sandeep Mewara 21-Apr-12 7:57am    
What have you tried so far? Where are you stuck?
h7h7h7 21-Apr-12 17:50pm    
I am stuck, there that I am using a lot of codes and I cant remove this string "------"..... look codes above ore below maybe you can help me

The conditions can't be both true at the same time so you should use "or" instead of "and". This translates into || instead of &&.

C#
if (!s.StartsWith("-") || !s.StartsWith(" " ))
...


Regards,

Manfred
 
Share this answer
 
v3
Comments
Espen Harlinn 22-Apr-12 7:20am    
Well answered Manfred :-D
VJ Reddy 22-Apr-12 11:09am    
Good point. 5!
Get all the Items of listbox1 and from this collection remove all the unwanted strings and then apply your sort alogrithm.
 
Share this answer
 
Comments
h7h7h7 21-Apr-12 7:13am    
hmmm do you minde to give a small example pls
ok.. Just try this... All in one..

Code for reading data from file..
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string[] dataFromFile = System.IO.File.ReadAllLines(openFileDialog.FileName);
Array.Sort(dataFromFile);
listBox1.DataSource = dataFromFile;

Code for removing "----" and to add that in Array List
ArrayList arr = new ArrayList();

foreach (string s in dataFromFile)
  if (s != "-----")
    arr.Add(s);

Code for sorting
arr.Sort();

Your final output in listBox2
listBox2.DataSource = arr;
 
Share this answer
 
Comments
Lakamraju Raghuram 21-Apr-12 9:04am    
Good.
h7h7h7 21-Apr-12 12:43pm    
string[] dataFromFile = lstQiftet.Items.Cast().ToArray();

Array.Sort(dataFromFile);
listBox1.DataSource = dataFromFile;

ArrayList arr = new ArrayList();

foreach (string s in dataFromFile)
if (s != "-----------------")
arr.Add(s);

arr.Sort();
listBox2.DataSource = arr;


why doesn't wor
Instead of

C#
if (s != "-----------------")


try using

C#
if (!s.StartsWith("-"))
 
Share this answer
 
Comments
h7h7h7 22-Apr-12 4:40am    
THnx Thnx that work, but now after I delete "-----" , in first line is an space, I Try to delet that space like your code it isn't working.....
if (!s.StartsWith("-")&&!s.StartWith(" " ))..... is here any mistake or should be another code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900