2016-04-03 73 views
0

我有一个Windows窗体,在完成时创建一个.txt文档,其中包含用户在一个窗体上的推算数据,然后该窗体可以打开为richTextBox另一种形式是使用ComboBox作为选择工具。刷新WinForm中的组合框目录列表,onClick for C#

我遇到的问题是,ComboBox不刷新,其中一个新.txt已创建并让用户有它的ComboBox清单显示之前重新启动程序后中的.txt文件的保存目录列表,想知道如何解决这个问题。可能强制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(); 
     } 
    } 

回答

0

首先,封装组合框人口的逻辑一个方法。

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(); 
} 
+0

不使用Dialog的方法的任何想法? – TCD

+0

@TCD您可以在没有使用Dialog的情况下执行此操作,只需订阅Closing事件即可。 – Valentin

0

我建议打破LoadComboBox为瓦伦丁建议,但使用实例化,并用表格来监控模块的目录中并LoadComboBox任何创建/删除设置在FileSystemWatcher的。

+0

你能否详细说明一下吗?听起来像我一直在寻找的解决方案! – TCD