listview to listbox code?

Discussion in 'C#' started by ricky0819, Jul 10, 2009.

  1. ricky0819

    ricky0819 New Member

    Joined:
    Jul 10, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi guys i have getting problem
    i want to transfer the item in listview to listbox and if the listbox already have that item it will count so that i can avoid double item what will happen to the text is it will display in the side of the item the number of times it was clicked

    i have this code but is seems not working
    how can i improve this?
    Code:
    private void listView1_DoubleClick(object sender, EventArgs e) 
            { 
                //get the string item of the selected item 
                string item = listView1.SelectedItems[0].ToString(); 
                int l = item.Length; 
                l -= 16; 
                string item2 = item.Substring(15,l); 
     
                //write the items from listbox to text file 
                int itemcounter = 0; 
                if (listBox1.Items.Count != 0) 
                { 
     for (string itemlist = listBox1.Items[itemcounter].ToString(); itemcounter <= listBox1.Items.Count; itemcounter++) 
                    { 
                        writer = File.AppendText(tempfile); 
                        writer.WriteLine(itemlist); 
                        writer.Close(); 
                    } 
                } 
     
                reader = new StreamReader(tempfile); 
                string hehe = reader.ReadToEnd(); 
                 
     
                //compute for the Quantity 
     
                int lineCount = 0; 
                string line22 = String.Empty; 
                string line; 
     
                int lines = 1; 
     
     
                 
     
                if (listBox1.Items.Count != 0) 
                { 
                    while (line22 != item2 + "1") 
                    { 
                        while ((line = reader.ReadLine()) != null) 
                        { 
                            lineCount++; 
                            if (lineCount == lines) 
                                line22 = line; 
                            lines++; 
                        } 
                    } 
     
                    lines--; 
                    string removeitem = listBox1.Items[lines].ToString(); 
                    listBox1.Items.Remove(removeitem); 
                    listBox1.Items.Add("new item"); 
                     
                } 
                else 
                {  
                    listBox1.Items.Add(item2 + "1"); 
                } 
                reader.Close(); 
                File.Delete(tempfile); 
                 
            }
     
    Last edited by a moderator: Jul 10, 2009

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice