C#将项目从其他类添加到MainForm ListBox

时间:2018-10-08 21:32:12

标签: c#

我对编程还是很陌生,并开始了一个项目,在该项目中,我试图将程序的功能分为几个类,每个类处理与该程序的特定部分相关的大部分内容。

我有一个名为DirectoryMonitors的类,该类创建一个对象,该对象使用FileSystemWatcher监视目录。我正在尝试从此DirectoryMonitors类的实例向MainForm上的列表框添加项目。但是,除非它是静态的,否则似乎无法在MainForm中调用该方法-但是,如果它是静态的,则无法访问我的ListBox。

我的DirectoryMonitor类的相关部分:

public class DirectoryMonitorData
{
    public bool WatcherActive { get; set; } = true;
    public string EQVersion { get; set; }
    public string FolderLocation { get; set; }
}

public class DirectoryMonitor : DirectoryMonitorData
{

    private void FolderWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        FileInfo fi = new FileInfo(e.FullPath);
        if (!IsFileLocked(fi))
        {
            int startPos = e.FullPath.LastIndexOf("\\") + 1;
            int endPos = e.FullPath.IndexOf("-Inventory") - startPos;
            string character = e.FullPath.Substring(startPos, endPos);
            MessageBox.Show(character);

            string[] delimiters = { ControlChars.Tab.ToString() };
            using (TextFieldParser parser = Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFieldParser(e.FullPath, delimiters))
            {
                // Process the file's lines.
                while (!parser.EndOfData)
                {
                    try
                    {
                        string[] fields = parser.ReadFields();
                        MainForm.addLogToListBox(fields[0]);
                        for (int i = 1; i <= 5; i++)
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }

    private bool IsFileLocked(FileInfo file)
    {
     ...
    }
}

我的MainForm类的相关部分:

    public partial class MainForm : Form
{


    public MainForm()
    {
        InitializeComponent();
    }


    public void addLogToListBox(string logMessage)
    {
        logsListBox.Items.Insert(0, logMessage);
    }
}

更新的代码:

    public FileSystemWatcher FolderWatcher = new FileSystemWatcher();

        public DirectoryMonitor()
        {
            FolderWatcher.NotifyFilter = NotifyFilters.LastWrite;
            FolderWatcher.Filter = "*-Inventory.txt";
            FolderWatcher.Changed += FolderWatcher_Changed;
        }

public void setupDirectoryMonitorList()
        {
            foreach (DirectoryMonitorData dmd in MainForm.directoryMonitorList)
            {
                DirectoryMonitor dm = new DirectoryMonitor()
                {
                    WatcherActive = dmd.WatcherActive,
                    FolderLocation = dmd.FolderLocation,
                    EQVersion = dmd.EQVersion
                };

                if (dm.WatcherActive)
                {
                    dm.FolderWatcher.Path = dm.FolderLocation;
                    dm.FolderWatcher.EnableRaisingEvents = true;
                    dm.FolderWatcher.NotifyFilter = NotifyFilters.LastWrite;
                    dm.FolderWatcher.Filter = "*-Inventory.txt";
                    dm.FolderWatcher.Changed += FolderWatcher_Changed;
                }
                MainForm.directoryMonitorObjects.Add(dm);
            }
        }

2 个答案:

答案 0 :(得分:2)

向您的DirectoryMonitorData类添加属性,并向其传递列表框:

public class DirectoryMonitorData
{
    public bool WatcherActive { get; set; } = true;
    public string EQVersion { get; set; }
    public string FolderLocation { get; set; }
    public ListBox Logs {get; set;}
}

然后:

DirectoryMonitor monitor = new DirectoryMonitor  { Logs = logsListBox };

现在在您的课程中,您只需将任何东西添加到该列表框中即可:

Logs.Items.Add(Something);

答案 1 :(得分:1)

我通常的做法是向类中添加一个constructor参数,然后将'MainForm'参数保存到该类中。

'MainForm'

现在,您可以使用字段public class DirectoryMonitor : DirectoryMonitorData { public DirectoryMonitor(MainForm form) { this.mainForm = form; } private MainForm mainForm; } 来访问所有public属性为MainForm的方法。

替代

在课堂上创建一个mainForm(使用自定义eventhandler)。然后从您的“ MainForm”订阅该事件。现在,该类不必了解表单。您只需要在课程中EventArgs使用事件处理程序即可。