I have created a GUI (Windows Form) that has FileBrowserdialogs in which you can choose the folder and display the contents on each listbox respectively. However, I want to compare both list boxes and display the differences in the strings on the third list box by pressing a button. For example:
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)
{
}
}
}
Sujeet SumanPosted Jan 29, 2016, 10:12 PM
Rudy EscamillaPosted Jan 29, 2016, 2:58 PM
I received the string (collection) on the third listbox instead of the differences between both.
Here is the full code:
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)
{
for (int intCount = 0; intCount < listBox1.Items.Count; intCount++)
{
for (int intSubCount = 0; intSubCount < listBox2.Items.Count; intSubCount++)
{
string s1 = listBox1.Items[intCount].ToString();
string s2 = listBox2.Items[intSubCount].ToString();
IEnumerable<string> set1 = s1.Split('_').Distinct();
IEnumerable<string> set2 = s2.Split('_').Distinct();
if (set2.Count() > set1.Count())
{
listBox3.Items.Add(set2.Except(set1).ToList());
}
}
{
}
}
}
}
}
Sujeet SumanPosted Jan 29, 2016, 1:45 PM