繁体   English   中英

刷新WinForm中的ComboBox目录列表,onClick for C#

[英]Refresh ComboBox directory listing in WinForm, onClick for C#

我有一个Windows窗体设置,完成后会在一个窗体上创建一个包含用户估算数据的.txt文档,然后可以使用ComboBox作为选择工具将该窗体打开到另一窗体上的richTextBox

我遇到的问题是,在创建新的.txt之后, ComboBox不会刷新保存.txt文档的目录列表,因此用户必须重新启动程序才能在ComboBox列表中显示该程序。解决这个问题。 可能迫使ComboBox刷新列表onClick按钮?

带有ComboBox选择方法的表单:

 public Default()
        {
            InitializeComponent();
        string[] files = Directory.GetFiles(@"C:\Modules");
        foreach (string file in files)
            ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
    }

    private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
    {
        NewModule newmodule = new NewModule();

        newmodule.Show();
    }

    private void ModuleSelectorComboBox_SelectedValueChanged(object sender, EventArgs e)
    {
        richTextBox1.Clear(); //Clears previous Modules Text
        string fileName = (string)ModuleSelectorComboBox.SelectedItem;
        string filePath = Path.Combine(@"C:\Modules\", fileName + ".txt");

        if (File.Exists(filePath))
            richTextBox1.AppendText(File.ReadAllText(filePath));
        else
            MessageBox.Show("There's been a problem. Please restart the program. \nError 1", "Error 1", //error 1 is file deleted while the program is running
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation,
            MessageBoxDefaultButton.Button1);
    }

要添加,我想避免使用对话框保存/打开文件方法,这就是为什么我使用ComboBox来做到这一点。

提前致谢。

用于创建新的.txt文档的表单(我认为这基本上不是必需的,我只是将其添加为参考):

private void button5_Click(object sender, EventArgs e)
    {

            RichTextBox newbox = new RichTextBox();
        {
            String Saved_Module = Path.Combine("C:\\Modules", txtModuleName.Text + ".txt");
            newbox.AppendText(txtModuleName.Text + "\n" + ModuleDueDate.Text + "\n" + txtModuleInfo.Text + "\n" + txtModuleLO.Text);
            newbox.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);

            Directory.CreateDirectory(Path.Combine(@"C:\Modules", txtModuleName.Text));
            this.Close();
        }
    }

首先,用一种方法封装组合框填充的逻辑。

public Default()
{
    InitializeComponent();
    LoadComboBox();
}

void LoadComboBox()
{
    ModuleSelectorComboBox.Items.Clear();
    string[] files = Directory.GetFiles(@"C:\Modules");
    foreach (string file in files)
        ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}

我建议使用ShowDialog方法将窗体打开为对话框窗体
并设置DialogResultDialogResult.OKbutton5_Click方法。

private void button5_Click(object sender, EventArgs e)
{
    RichTextBox newbox = new RichTextBox();
    String Saved_Module = Path.Combine("C:\\Modules", txtModuleName.Text + ".txt");
    newbox.AppendText(txtModuleName.Text + "\n" + ModuleDueDate.Text + "\n" + txtModuleInfo.Text + "\n" + txtModuleLO.Text);
    newbox.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);

    Directory.CreateDirectory(Path.Combine(@"C:\Modules", txtModuleName.Text));
    this.DialogResult = DialogResult.OK;
    this.Close();
}

然后,基于DialogResult加载或不加载moduleToolStripMenuItem_Click方法中组合框的项目。

private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
{
    NewModule newmodule = new NewModule();

    newmodule.ShowDialog();
    if (result == DialogResult.OK)
    {
        LoadComboBox();
    }
}

更新:

如果不想使用对话框,则可以订阅FormClosing事件,并将处理程序中的组合框更新为该事件。

private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
{
    NewModule newmodule = new NewModule();
    newmodule.FormClosing += F_FormClosing
    newmodule.Show();
}

private void F_FormClosing(object sender, FormClosingEventArgs e)
{
    LoadComboBox();
}

我建议按照Valentin的建议分解LoadComboBox,但使用实例化并随表格一起放置的FileSystemWatcher来监视Modules目录,并在任何创建/删除操作中调用LoadComboBox。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM