编辑:下面的代码是我创建的类,以便有一个拖放richtextbox,其中将文件拖入其中会导致文件内的信息出现在richtextbox中。这很好用,我甚至做到了这样,128以上的十进制字符显示为二进制,所以我可以在某种程度上看到位图图像。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Troy_PCL_Utility
{
public class DragDropRichTextBox : RichTextBox
{
public DragDropRichTextBox()
{
this.AllowDrop = true;
this.DragDrop += DragDropRichTextBox_DragDrop;
}
public static class BinaryFile
{
private static string[] __byteLookup = new string[300];
static BinaryFile()
{
//CHARACTERS: All Control ASCII Characters
for (int i = 0x00; i < 0x1E; i++) { __byteLookup[i] = ((char)i).ToString(); }
// Display printable ASCII characters
for (int i = 0x1E; i < 0x7E; i++) { __byteLookup[i] = ((char)i).ToString(); }
//BITMAPS: Display non-printable ASCII characters
//for (int i = 0; i <= 0x00; i++) { __byteLookup[i] = "\\" + i.ToString(); }
for (int i = 0x7E; i <= 0xFF; i++) { __byteLookup[i] = Convert.ToString(i, 2); } //As Binary
//for (int i = 0x00; i <= 0xFF; i++) { __byteLookup[i] = "\\" + i.ToString(); } //As Characters
}
public static string ReadString(string filename)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filename);
return String.Join("", (from i in fileBytes select __byteLookup[i]).ToArray());
}
}
public void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
{
//string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];
string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];
if (fileText != null)
{
foreach (string name in fileText)
{
try
{
this.AppendText(BinaryFile.ReadString (name) + "\n -------- End of File -------- \n\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}
但是,除了将内容拖放到此richtextbox之外,我还希望主窗体上的textbox2填充文件来自的路径,因此如果用户正在处理多个文件,他们可以仔细检查看看他们正在查看哪一个以及从哪个目录。
所以我有这个类(DragDropRichTextBox:RichTextBox),我有另一个类(Main Form)。我不确定如何完成这项任务。将目录复制到textbox2的代码是否属于我的DragDrop类(上图),是否可能?或者我应该从我的主文件中的richtextbox创建一个事件吗?
我尝试点击richtextbox(从我的DragDropRichTextBox类派生),然后创建了一个拖放事件,我尝试将其拖放到文本路径中,然后将文件路径复制到textbox2,但我没有运气。这是我试过的代码。我对此并不自信:
private void dragDropRichTextBox1_DragDrop(object sender, DragEventArgs e)
{
string fileName = (string) e.Data.GetData(DataFormats.FileDrop, false);
textBox2.Text = fileName;
}
有没有人对新人提出任何建议?谢谢您的帮助。