のように更新しました。完了時に、あるフォームでユーザの帰属データを含む.txt
ドキュメントを作成し、そのフォームをrichTextBox
に開くことができるWindowsフォームがあります。選択ツールとしてComboBox
を使用する別のフォーム。WinFormのComboBoxディレクトリをC#
私が午前問題はComboBox
が新しい.txt
が作成された後に.txtの文書が保存されているディレクトリのリストを更新していないということですので、ユーザはそれがComboBox
リストに表示する前に、プログラムを再起動する必要がありますこれを解決する方法が不思議です。ボタンのリストonClick
をリフレッシュするようにComboBox
を強制する可能性がありますか? 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);
}
私はダイアログオープン/セーブファイル方式を使用して、私はこれを行うためにコンボボックスを使用しています理由ですを避けたい追加します。
ありがとうございます。 、すべての
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