WinForms 文本框更改其边距?

发布于 2024-10-11 04:12:30 字数 1027 浏览 6 评论 0原文

所以我一直在拼凑一个游戏,并决定我想要一个小程序来显示一个文件,其中每个字符都被其等效字节替换,以便处理编码保存等。以为是上篮。三小时后,我绞尽脑汁试图弄清楚这一点。

当我加载一个小(或者也许短是更好的术语)文件时,它看起来像顶部的窗口。当我加载较大的文件时,它看起来像底部的窗口。
http://dl.dropbox.com/u/16985121/Images/ViewAsBytes.PNG

这是 10pt Courier New,但我尝试的任何字体似乎都会发生这种情况。总会有额外的列,如果没有足够的空间容纳该列,它就会在以前未使用的空间中挤入任何可能的东西。我尝试过调整各种变量,以及比较添加文件中的文本之前和之后的文本框(从 FileStream 中读取字节,然后输入 StringBuilder),但似乎没有任何变化尽管有些东西明显不同。

我可以为此想出一堆不同的解决方法,但现在我更感兴趣的是 TextBox 认为它正在做什么,而不是完成我的程序。有人知道吗?

以下是读取数据并将其放入文本框的代码:

FileStream stream = new FileStream(files[0], FileMode.Open);

StringBuilder sb = new StringBuilder();

int byteIn = stream.ReadByte();
while (byteIn != -1)
{
    sb.Append('[');

    if (byteIn < 100)
        sb.Append('0');
    if (byteIn < 10)
        sb.Append('0');

    sb.Append(byteIn.ToString());

    sb.Append(']');

    byteIn = stream.ReadByte();
}

txtView.Text = sb.ToString();

stream.Close();

So I've been working on cobbling together a game and decided I'd like to have a little program to show a file with each character replaced by its byte equivalent for working with coding saves and whatnot. Figured it'd a layup. Three hours later, I've been wracking my brain trying to figure this out.

When I load a small (or perhaps short is the better term) file it looks like the window on top. When I load a larger file, it looks like the window on the bottom.
http://dl.dropbox.com/u/16985121/Images/ViewAsBytes.PNG

That's 10pt Courier New, but it seems to happen with any font I try. There's always that extra column, and if there wasn't enough room for the column, it'd just squeeze in whatever it could in that space that it previously didn't use. I've tried tweaking all kinds of variables, as well as comparing the textbox before and after it adds the text from the file (which is read in just as bytes from a FileStream and then fed into a StringBuilder) but nothing seems to change even though something is clearly different.

I can think of a bunch of different workarounds for this, but now I'm just more interested in what TextBox thinks it's doing exactly than getting my program done. Anyone got any idea?

Here's the code that reads in the data and puts that to the textbox:

FileStream stream = new FileStream(files[0], FileMode.Open);

StringBuilder sb = new StringBuilder();

int byteIn = stream.ReadByte();
while (byteIn != -1)
{
    sb.Append('[');

    if (byteIn < 100)
        sb.Append('0');
    if (byteIn < 10)
        sb.Append('0');

    sb.Append(byteIn.ToString());

    sb.Append(']');

    byteIn = stream.ReadByte();
}

txtView.Text = sb.ToString();

stream.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-10-18 04:12:31

这是因为您将 WordWrap 属性设置为 True。将其设置为 False,将 Multiline 设置为 True,将 ScrollBars 设置为 Both。将 Environment.NewLine 附加到您生成的字符串中,每 16 个字节是十六进制查看器的标准。使用 byte.ToString("X2") 生成十六进制字符串而不是十进制字符串。

您现在拥有数据的完整可滚动视图,支持任何数量。允许用户调整窗口大小,这样她就不必水平滚动。或者只是让它足够大。

This is because you set to the WordWrap property to True. Set it to False, set Multiline to True and ScrollBars to Both. Append Environment.NewLine to the string you generate, every 16 bytes is the norm for hex viewers. Use byte.ToString("X2") to generate a hex string instead of a decimal string.

You now have a full scrollable view of the data, any amount is supported. Allow the user to resize the window so she won't have to scroll horizontally. Or just make it big enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文