Windows Develop Bookmark and Share   
 index > Windows Forms General > Combine Two ListBoxes And Write a File
 

Combine Two ListBoxes And Write a File

Hello,
 I'm learning C# and i want to know one thing, how i can combine two ListBoxes and write a file, like this:

For each line of listBox1 we have another for listBox2, when i click in the button1(Save File) it saves a file using a SaveFileDialog and inside the file needs to be like this:

Item1(From ListBox1) Item1(From ListBox2)
Item2(From ListBox1) Item2(From ListBox2)
Item3(From ListBox1) Item3(From ListBox2)
Item4(From ListBox1) Item4(From ListBox2)

Ignore the parentesis, they are just to organize the idea.

Best Regards,
 Nathan Paulino Campos

  • Moved byTaylorMichaelLMVPMonday, October 05, 2009 1:49 PMWinForms related (From:Visual C# General)
  •  
Nathan Campos  Monday, October 05, 2009 1:47 PM
Hi Nathan,

For this format number of items in both listboes should be equal.

Here is the code.

            if(listBox1.Items.Count!=listBox2.Items.Count)
            {
                MessageBox.Show("Listboxes Item Count Should Be Equal");
            }
            using (SaveFileDialog fd = new SaveFileDialog())
            {
                fd.ShowDialog();
                if (fd.FileName != null)
                {
                    StreamWriter sw = new StreamWriter(fd.FileName);
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        sw.WriteLine(listBox1.Items[i].ToString() + " " + listBox2.Items[i].ToString());//You can use string builder here
                    }
                    sw.Close();
                    sw.Dispose();
                }
            }
Tamer Oz  Monday, October 05, 2009 1:57 PM
Hi Nathan,

For this format number of items in both listboes should be equal.

Here is the code.

            if(listBox1.Items.Count!=listBox2.Items.Count)
            {
                MessageBox.Show("Listboxes Item Count Should Be Equal");
            }
            using (SaveFileDialog fd = new SaveFileDialog())
            {
                fd.ShowDialog();
                if (fd.FileName != null)
                {
                    StreamWriter sw = new StreamWriter(fd.FileName);
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        sw.WriteLine(listBox1.Items[i].ToString() + " " + listBox2.Items[i].ToString());//You can use string builder here
                    }
                    sw.Close();
                    sw.Dispose();
                }
            }
Tamer Oz  Monday, October 05, 2009 1:57 PM
Thanks very much Tamer!
Sorry, i posted in the wrong forum.
Nathan Campos  Monday, October 05, 2009 2:21 PM

You can use google to search for other answers

Custom Search

More Threads

• dynamic controls
• Closing a mutex I don't own (VB2005)
• Flicker Main Window when closing a second window
• Get Cursor Location In A Grid Cell
• How to use a windows form as active desktop?
• WebBrowser control and Stylesheet
• where can I download AxSHDocVw.dll ?
• pixel color depth on the primary display device
• Splash Screen in VB 2005
• Office xp PIA Placing two documents together