有没有人知道如何在使用特定编码从原始字节读取文本后编辑文本框中的文本?
只有似乎允许的密钥是:Home,End,Page Up / Down,Delete ...
如果您尝试按下任何键,例如:A-Z它将不显示,文本框的行为类似于按键事件被覆盖,并且e.Suppress设置为true。
代码: `
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
byte[] _buffer;
public Form1()
{
InitializeComponent();
Text = "Encoding (Test)";
textBox1.Text = "1252"; // Western European (Windows)
}
private void button1_Click(object sender, EventArgs e)
{
if (_buffer == null)
{
MessageBox.Show("Select file to read from!");
buttonOpenFile_Click(null, null);
return;
}
var encoding = Encoding.GetEncoding(int.Parse(textBox1.Text));
textBox2.Text = encoding.GetString(_buffer);
}
private void buttonOpenFile_Click(object sender, EventArgs e)
{
using (var of = new OpenFileDialog())
{
if (of.ShowDialog() == DialogResult.OK)
{
Text = of.SafeFileName;
ReadToBuffer(of.FileName);
}
}
}
private void ReadToBuffer(string file)
{
using (var fileIO = new FileStream(file, FileMode.Open, FileAccess.Read))
{
_buffer = new byte[fileIO.Length];
fileIO.Read(_buffer, 0, (int)fileIO.Length);
}
}
}
}
`
答案 0 :(得分:1)
您尝试打开的文件似乎比TextBox.MaxLength大。我能够通过创建一个简单的文本文件来重现这个问题,该文件填充了超过TextBox控件的MaxLength的随机字符。