Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need some Guidance on comparing listboxes in Winforms

listbox compare

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 Sonic1980

Sonic1980

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 January 2016 - 10:19 AM

I have created a GUI(windows form) that has two listboxes that will display files opened via filebrowserdialogs. I want to compare the file names that appear in both listboxes and display the results in a third listbox by pressing a button. Any help would be greatly appreciated. For Example:

 

A file name could be called:

 

Listbox1

(XXXX_YYYY_ZZZZ_6R_4KL_CM12345_L_88%_0.01_v1)

 

Listbox2

(XXXX_YYYY_ZZZZ_6R_4KL_CM12346_L_88%_0.01_v1)

 

Listbox3

should display the differences between both strings for each item. If there is none

then maybe a message that says no difference along side.

 

I am not sure exactly how to approach this if I have to sort the list first and then iterate th

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

 

namespace WindowsFormsApplication3

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog FBD = new FolderBrowserDialog();

 

            if (FBD.ShowDialog() == DialogResult.OK)

            {

 

                listBox1.Items.Clear();

                string[] files = Directory.GetFiles(FBD.SelectedPath);

                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);

 

                foreach (string file in files)

                {

                    listBox1.Items.Add(file);

                }

 

                foreach (string dir in dirs)

                {

                    listBox1.Items.Add(dir);

                }

 

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog FBD = new FolderBrowserDialog();

            if (FBD.ShowDialog() == DialogResult.OK)

            {

                listBox2.Items.Clear();

                string[] files = Directory.GetFiles(FBD.SelectedPath);

                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);

 

                foreach (string file in files)

                {

                    listBox2.Items.Add(file);

                }

 

                foreach (string dir in dirs)

                {

                    listBox2.Items.Add(dir);

                }

            }

            {

            }

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

 

        }

    }

}

 

 

 

 

 



#2 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 30 January 2016 - 07:04 PM

You should not be using UI components for any of the program logic, period. How do you populate these listboxes? UI components are mainly intended for display only. If you have some checkboxes and such, you should create an interface for your program to use that obtains booleans and other settings values rather than having code in tons of other places reference the checkbox directly to see if it is checked or not. It's called separation of concerns. The obvious idea is to loop for each value in the first collection of data to compare against every other value in the secondary collection of data, no need to sort. This can be done for you via LINQ or a couple for loops.


Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image