Hi, now i do the sorted list for the combination i did. I want to read the data to the sorted list. But my foreach loop always had error. i fix it many times already but still can not solve it successfully. I will attach my coding. Hope someone can help me to solve the problem...
here is my coding. i will attach a full coding.
private void Browse_Click(object sender, EventArgs e)
{
using (OpenFileDialog fileOpen = new OpenFileDialog())
{
try
{
fileOpen.Filter = "All files (*.*)|*.*";
fileOpen.InitialDirectory = ".\\";
fileOpen.Title = "Open";
if (fileOpen.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(fileOpen.FileName, Encoding.Default);
str = sr.ReadToEnd();
sr.Close();
ContentBox.Text = str;
filePath.Text = fileOpen.FileName;
}
SplitItems = (str).Split(new string[] { "\n\r\t", " " }, StringSplitOptions.None);
foreach (string listOut in SplitItems)
{
ListSplitItems += "\r\n" + listOut;
}
//txtCaseInputs.Text = ListSplitItems.Trim();
}
catch (Exception o)
{
string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (File.Exists(fileOpen.FileName))
{
StreamReader file = null;
try
{
file = new StreamReader(fileOpen.FileName);
while ((line = file.ReadLine()) != null)
{
//MessageBox.Show(line.ToString());
ListSplitLineByLine = "";
//String[] SplitItemLineByLine1;
//sb.AppendLine(line);
// txtCaseInputs.Text = sb.ToString().Trim();
//SplitItemLineByLine. = null;
line = line.Replace("\r\n"," ");
String[] SplitItemLineByLine1 = (line).Split(' ');
foreach (string lineByLine in SplitItemLineByLine1)
{
ListSplitLineByLine += "\r\n" + lineByLine;
s.AppendLine(ListSplitLineByLine);
}
txtCaseInputs.Text = ListSplitLineByLine.ToString().Trim();
GenCombItems(); // 1-mac
//MessageBox.Show("re");
//txtCaseInputs.Clear();
}
}
catch (Exception o)
{
string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}
private void GenCombItems()
{
//lbCombinations.Items.Clear();
//yes
//long n = int.Parse(txtTotalItems.Text);
//long k = int.Parse(txtSubsetSize.Text);
int n = txtCaseInputs.Lines.Length;
//int k = int.Parse(txtSubsetSize.Text);
int k = 2;
txtNumCombinations.Text = Combination.Choose(n, k).ToString();
Combination c = new Combination(n, k);
string[] result = new string[k];
while (c != null)
{
result = c.ApplyTo(txtCaseInputs.Lines);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; ++i)
{
sb.AppendFormat("{0} {1}", result[i], " ");
}
lbCombinations.Items.Add(sb.ToString());
/*@@*/ foreach (String sort in sb.ToString().Trim())
{
if (!(sortList.ContainsKey(sort)){
sortList.Add(sort, 1);
}
else
{
int Count = (int)sortList[sort];
sortList[sort] = Count + 1;
}
}
c = c.Successor();
}
txtCaseInputs.Clear();
DisplayResult();// display result
}
String ShowResult =" " ;
private void DisplayResult()
{
IDictionaryEnumerator enumerator = sortList.GetEnumerator();
while (enumerator.MoveNext())
{
ShowResult += "{"+enumerator.Key.ToString()+"}:" + enumerator.Value.ToString() + "\n";
//ss.AppendLine(ShowResult);
}
// MessageBox.Show(ss.ToString());
listBox.Items.Add(ShowResult.ToString().Trim());
}
private void txtCaseInputs_TextChanged(object sender, EventArgs e)
{
txtTotalItems.Text = (txtCaseInputs.Lines.Length).ToString();
}
private void txtSubsetSize_TextChanged(object sender, EventArgs e)
{
long n = int.Parse(txtTotalItems.Text);
long k = int.Parse(txtSubsetSize.Text);
txtNumCombinations.Text = Combination.Choose(n, k).ToString();
}
private void btnGenerateCombinations_Click(object sender, EventArgs e)
{
/* lbCombinations.Items.Clear();
int n = txtCaseInputs.Lines.Length;
int k = int.Parse(txtSubsetSize.Text);
Combination c = new Combination(n, k);
string[] result = new string[k];
while (c != null)
{
result = c.ApplyTo(txtCaseInputs.Lines);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; ++i)
{
sb.AppendFormat("{0} {1}", result[i], " ");
}
lbCombinations.Items.Add(sb.ToString());
c = c.Successor();
}*/
}
private void txtTotalItems_TextChanged(object sender, EventArgs e)
{
}
private void tabPage6_Click(object sender, EventArgs e)
{
}
private void filePath_TextChanged(object sender, EventArgs e)
{
}
private void ContentBox_TextChanged(object sender, EventArgs e)
{
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
The symbol /*@@*/ is the place that the error occur. Hope someone can help me to fix the error.