0

変なタイトルですみません、他にいいのが思いつきませんでした!

とにかく、固定幅ファイルを読み取り、ユーザー入力からフィールド長を収集し、ファイルの最初の行から各列を別の色で表示するプログラム ( Windows Forms App )を作成する途中です。... 分かりますか?基本的には、色を使用して固定幅ファイル内のさまざまなフィールドを区別することです。

私が聞きたかったのは、これについて最善の方法は何でしたか?私は多くの問題を抱えており、物事に出くわし続け、もっと良い解決策があることを知っていても、嫌な解決策を実装しているだけだからです。

明らかに、プログラム全体を提供する必要はありません。これを行うためのより良い方法のいくつかのアイデアだけを提供してください。私の解決策は恐ろしいものだからです。

よろしくお願いします!

4

1 に答える 1

1

私は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);
     }
  }
}
于 2011-03-22T13:14:23.517 に答える