表单之间的ListBox问题. [英] ListBox issues between forms.

查看:44
本文介绍了表单之间的ListBox问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中信息以一种形式存储到文本文件中,然后由另一种形式读取,然后将一些信息写入ListBox中.它的工作方式是,将表单写入文本文件会激活另一种形式的功能.

我的问题是,当窗体调用此函数时,应添加到ListBox的信息不起作用.这似乎只有在通过其他形式激活时才会发生.当通过它所写的形式激活时,它可以很好地工作.此外,我尝试使用断点来监视其进度和变量.该功能似乎正在正常工作.变量正常建立,但是ListBox just不想正常工作.我已经在下面发布了该问题的代码.感谢您抽出宝贵的时间至少看看我的问题. :)

正在写入文本文件的表格.

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;
        //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); 
}

推荐答案

可能是您尚未关闭streamwriter对象,因为它尚未关闭,因此文件仍在流中打开.这就是为什么它无法正确读取文件的原因.关闭流写入器后再试.
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.


没关系.我已经找出了问题所在.激活该功能时,我已经初始化了一个新的Form1,这意味着旧表单已过时,因此无法更新.
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.


Hari使用StreamWriter.Dispose()函数执行与关闭文件一样,几乎是一样的.文件读取似乎不是问题.当我设置断点时,该文本文件被加载到数组"Lines"中,并包含刚刚由其他表单添加的文本.我的问题是,出于某种奇怪的原因,它不会将更新后的文本写入表单上的ListBox.就像我之前说的,ListBox在第二种形式使用该函数时更新,而在第一种形式使用该函数时不更新.
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.


这篇关于表单之间的ListBox问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆