我有一个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();
}
}
不使用Dialog的方法的任何想法? – TCD
@TCD您可以在没有使用Dialog的情况下执行此操作,只需订阅Closing事件即可。 – Valentin