Click here to Skip to main content
16,006,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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.
C#
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;
        //foreach (string str in Lines)
        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); 
}
Posted

Nevermind. I've figured out what's wrong. When activating the function, I've initialized a new Form1, which means the old form was rendered obsolete and thus the failure to update.
 
Share this answer
 
Hari, using the StreamWriter.Dispose() function does the same thing as closing the file, pretty much. The file reading doesn't appear to be the issue. When I set up breakpoints, the text file is loaded into the array "Lines" and contains the text that was just added by the other form. My issue is that for some odd reason, it won't write the updated text to the ListBox on my form. Like I said before, the ListBox updates when the second form uses the function, but not when the first form does it.
 
Share this answer
 
May be you have not closed the streamwriter object, as it's not closed the file is still open in the stream. That's why its not able to read the file properly. Try after closing the streamwriter.
 
Share this answer
 
Comments
Liam Cairns Kelly 22-Aug-12 13:01pm    
Hari, using the StreamWriter.Dispose() function does the same thing as closing the file, pretty much. The file reading doesn't appear to be the issue. When I set up breakpoints, the text file is loaded into the array "Lines" and contains the text that was just added by the other form. My issue is that for some odd reason, it won't write the updated text to the ListBox on my form. Like I said before, the ListBox updates when the second form uses the function, but not when the first form does it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900