I have a program in which information is stored into a text file in one form and read by another form then writes some information onto a
ListBox
. The way it works is that the form writing to the text file activates the function in the other form.
My problem is that when this function is called by the form, the information that should be added to the
ListBox
is not working. This only seems to happen when activated by the other form. When activated by the form it's written in, it works perfectly fine. Futhermore, I tried using breakpoints to moniter its progress and variables. The function appears to be working as it should. The variables are building up as normal, but the
ListBox
just doesn't want to work correctly. I've posted the code for the problem below. Thank you for taking the time to at least look at my question. :)
Form that's writing to the text file.
string Path = @"C:\Users\Liam\txt.txt";
try
{
StreamWriter MacroScript = new StreamWriter(Path, true);
MacroScript.WriteLine("MACRONAME:" + MacroName);
MacroScript.WriteLine("KEYBIND:" + Keybind);
foreach (string str in listSteps.Items)
{
MacroScript.WriteLine(str);
}
MacroScript.WriteLine("ENDMACRO");
MacroScript.WriteLine("\n\n");
MacroScript.Dispose();
MessageBox.Show("Macro sucessfully created!", "Macro achieved");
F1.test = 0;
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("Failed to create macro.\nError Message: "
+ ex.Message, "Macro failed");
}
Form that's reading the text file.
string FilePath = @"C:\Users\Liam\txt.txt";
try
{
if (File.Exists(FilePath))
{
listMacroListing.Items.Clear();
string[] Lines = File.ReadAllLines(FilePath);
int i;
if (StartNumbers.Count != 0) i = StartNumbers.Count;
else i = 0;
for (int ii = 0; ii < Lines.Length; ii++)
{
try
{
if (Lines[ii].Substring(0, 10) == "MACRONAME:")
{
StartNumbers.Add(i);
listMacroListing.Items.Add(Lines[ii].Substring(10));
MessageBox.Show(Lines[ii]);
}
i++;
}
catch { }
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}