ファイルを richTextBox にドラッグ アンド ドロップすると問題が発生します。テキスト ファイルをその上にドラッグすると、その下に名前が表示されたテキスト ファイルの画像に変わります。ファイルをダブルクリックすると、システムのデフォルト アプリケーション (テキスト ファイル用のメモ帳など) を使用してファイルが開きます。基本的に、ファイル内のテキストを読みたいときに、richTextBox にショートカットを作成します。
このコードに基づいて、ファイルからのテキストがrichTextBox1に抽出されます。
class DragDropRichTextBox : RichTextBox
{
public DragDropRichTextBox()
{
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(DragDropRichTextBox_DragDrop);
}
private void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
{
string[] fileNames = e.Data.GetData(DataFormats.FileDrop) as string[];
if (fileNames != null)
{
foreach (string name in fileNames)
{
try
{
this.AppendText(File.ReadAllText(name) + "\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
これを機能させる方法についてのアイデアはありますか?