如何最好地对“现场着色器”进行编程程序 - 非常简单!

发布于 2024-10-24 19:32:26 字数 321 浏览 9 评论 0原文

抱歉,这个奇怪的标题,我想不出更好的了!

无论如何,我正在编写一个程序(Windows Forms App),该程序读取固定宽度的文件,从用户输入中收集字段长度,然后应该显示第一行的每一列文件的颜色不同...你知道我的意思吗?它基本上是使用颜色来区分固定宽度文件中的不同字段。

我想问的是解决这个问题的最佳方法是什么?因为我遇到了很多麻烦,当我知道有更好的解决方案时,我不断遇到问题并实施令人厌恶的解决方案。

显然你不必给我一个完整的程序,只需给我一些更好的方法来解决这个问题的想法 - 因为我的解决方案太可怕了。

提前谢谢大家!

Sorry for the weird title, I could't think of anything better!

Anyways, I'm half way through writing a program (Windows Forms App) that reads in a fixed-width file, gathers field lengths from user input, then it's supposed to display each column from the first line of the file in a different color... Do you know what I mean? It's basically to differentiate between the different fields in a fixed-width file using color.

What I wanted to ask was what was the best way to go about this? Because I'm having a lot of trouble, and I keep running into things and just implementing disgusting solutions when I know there is a much better one.

Obviously you don't have to give me a whole program, just some ideas of better ways to go about this - because my solution is just horrendous.

Thank you all in advance!

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

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

发布评论

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

评论(1

作业与我同在 2024-10-31 19:32:26

我会使用 RichTextBox。这是更改文本颜色的简单方法。下面是一个示例,其中我有 3 个来自用户的输入,用于说明每列的宽度。然后它读入文件并适当地为宽度着色。希望这会给您带来更多想法。

public partial class Form1 : Form
{
  public Form1()
  {
     InitializeComponent();
     ReadFile();
  }

  private void ReadFile()
  {
     // Assumes there are 3 columns (and 3 input values from the user)
     string[] lines_in_file = File.ReadAllLines(@"C:\Temp\FixedWidth.txt");
     foreach (string line in lines_in_file)
     {
        int offset = 0;
        int column_width = (int)ColumnWidth1NumericUpDown.Value;
        // Set the color for the first column
        richTextBox1.SelectionColor = Color.Khaki;
        richTextBox1.AppendText(line.Substring(offset, column_width));
        offset += column_width;

        column_width = (int)ColumnWidth2NumericUpDown.Value;
        // Set the color for the second column
        richTextBox1.SelectionColor = Color.HotPink;
        richTextBox1.AppendText(line.Substring(offset, column_width));
        offset += column_width;

        column_width = (int)ColumnWidth3NumericUpDown.Value;
        // Make sure we dont try to substring incorrectly
        column_width = (line.Length - offset < column_width) ?
            line.Length - offset : column_width; 
        // Set the color for the third column
        richTextBox1.SelectionColor = Color.MediumSeaGreen;
        richTextBox1.AppendText(line.Substring(offset, column_width));

        // Add newline
        richTextBox1.AppendText(Environment.NewLine);
     }
  }
}

I would use a RichTextBox. This has an easy way to change the color of text. Here is an example where I have 3 inputs from the user that tells how wide each column should be. Then it reads in a file and colors the widths appropriately. Hopefully this will give you some more ideas.

public partial class Form1 : Form
{
  public Form1()
  {
     InitializeComponent();
     ReadFile();
  }

  private void ReadFile()
  {
     // Assumes there are 3 columns (and 3 input values from the user)
     string[] lines_in_file = File.ReadAllLines(@"C:\Temp\FixedWidth.txt");
     foreach (string line in lines_in_file)
     {
        int offset = 0;
        int column_width = (int)ColumnWidth1NumericUpDown.Value;
        // Set the color for the first column
        richTextBox1.SelectionColor = Color.Khaki;
        richTextBox1.AppendText(line.Substring(offset, column_width));
        offset += column_width;

        column_width = (int)ColumnWidth2NumericUpDown.Value;
        // Set the color for the second column
        richTextBox1.SelectionColor = Color.HotPink;
        richTextBox1.AppendText(line.Substring(offset, column_width));
        offset += column_width;

        column_width = (int)ColumnWidth3NumericUpDown.Value;
        // Make sure we dont try to substring incorrectly
        column_width = (line.Length - offset < column_width) ?
            line.Length - offset : column_width; 
        // Set the color for the third column
        richTextBox1.SelectionColor = Color.MediumSeaGreen;
        richTextBox1.AppendText(line.Substring(offset, column_width));

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