2016-04-03 5 views
0

のように更新しました。完了時に、あるフォームでユーザの帰属データを含む.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(); 
     } 
    } 

答えて

0

まずでコンボボックスの人口のロジックをカプセル化:

新しい.txtのドキュメントを作成するためのフォーム(私はちょうど参考のためにそれを追加したものと本質的に必要なこれが表示されていません)方法。

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方法
を使用してダイアログフォームとしてフォームを開き、button5_Click方法でDialogResultDialogResult.OKに設定することをお勧めします。

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