Skip to content

Problems with TextBox control

Stephen

Asked Apr 15, 2007, 8:33 PM EDT2 answers
Share
LinkedInX
I'm writing a text editor in C#. It is nearly feature complete, but one problem remains: I want it to be able to open any kind of file and display it, regardless of size or format. I even want it to be able to properly open binary files, even if it shows up as strange characters (I.E. if you open an executable file in Notepad). The problem is that when I try to open a file, it only seems to show up properly if it is purely alphabetic and numerical characters. Opening a .exe file only shows the first to characters, basically the file signature, MZ. That being said, here's what I'm doing: 1) User selects a file through a dialog 2) Using StreamReader (or eqivalent, I've tried several, including FileStream, TextReader, etc.) I read the data from the file into a string. Opening a ~ 2.5 MB .exe file, I get a string with a length of 2,390,632. Seems reasonable. 3) I set the textBox1.Text property to the string I just created. The jist of the code is this: String text = stream.ReadToEnd(); MessageBox.Show("" + text.Length); //Debug: Checks the length textBox1.Text = text; MessageBox.Show("" + textBox1.Text.Length); //Debug: Checks the TextBox text length Strangely, even though text length is 2,390,632, setting the TextBox's Text property to text results in a length of 3. How setting the property to a string with the length of 2,390,632 results in the TextBox's text having a length of 3 is beyond me. Any ideas?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    This reaction is predictable... During conversion binary data into string scanning processed until the first zero-byte occured. Forget about using usual textbox to display binary data. If you still want to load binaries then try writing hex-editor (in hex-editor all bytes of binary data are being coverted into pieces of hexadecimal numbers, so these numbers are easy to display and edit as text).
    0
  • 0
    Hmmm. This gives me some ideas. Thanks! At least I understand what's going on now. Maybe I can have it prompt for hex editing if it detects such a character. I did consider adding a hex editor to it at some point.
    0