2016-06-20 24 views
0

私はビジネスに就職するためのクロックを作ります。ここに私のコードは次のとおりです。C#でCSVファイルに書き込む

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace WindowsFormsApplication2 
{ 
public partial class Form1 : Form 
{ 
    String Code; 
    String Name; 
    String InOut; 
    Boolean Luke = true; 
    String csvPath = "C:/users/luke/documents/C#/csvProject.csv"; 
    StringBuilder Header = new StringBuilder(); 
    StringBuilder csvData = new StringBuilder(); 



    public Form1() 
    { 
     InitializeComponent(); 
     FormBorderStyle = FormBorderStyle.None; 
     WindowState = FormWindowState.Maximized; 
     TopMost = true; 

     Header.AppendLine("Timestamp, Name"); 
     File.AppendAllText(csvPath, Header.ToString()); 
     textBox1.Font = new Font("Arial", 30, FontStyle.Bold); 


    } 

    private void button_Click(object sender, EventArgs e) 
    { 
     Button button = (Button)sender; 
     Code = Code + button.Text; 
     textBox1.Text = Code; 
    } 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Escape) 
     { 
      FormBorderStyle = FormBorderStyle.Sizable; 
      WindowState = FormWindowState.Normal; 
      TopMost = false; 
     } 
    } 

    private void button13_Click(object sender, EventArgs e) 
    { 
     //clear 
     Code = null; 
     textBox1.Text = Code; 
    } 

    private void button10_Click(object sender, EventArgs e) 
    { 
     //in or out 
     DateTime timeStamp = DateTime.Now; 
     if (Code == "123") 
     { 
      Name = "Luke"; 
     } 
     Button button = (Button)sender; 
     csvData.AppendLine(timeStamp + "," + Name + "," + button.Text); 
     File.AppendAllText(csvPath, csvData.ToString()); 
     Code = null; 
     textBox1.Text = Code; 
    } 

    private void button14_Click(object sender, EventArgs e) 
    { 

    } 
} 
} 

私のレイアウトは、ボタン、およびアウトボタンに、数字パッドで構成されています。ユーザーがコードを入力した後に[入力]ボタンを押すと、プログラムはCSVファイルにタイムスタンプ、名前、入力を書き込む必要があります。クロック入力でコードをテストすると、プログラムはある行を正しく書き込みます。私が時計を入れて時計を切ると、それは私の時計が2列、時計が1列になります。コード内で何がうまくいかないのか誰かが助けてくれるかどうか疑問に思っていました。ありがとう。

答えて

0

csvDataをファイルに書き込んだ後で空にする必要があります。

+0

私はcsvData = nullを追加しました。 button10_Clickの最後に、私がクロックアウトするとプログラムがクラッシュします。私が時計を入れるとうまく動作します。 – LUKER

+0

あなたはどんなエラーが発生しましたか?値をnullに設定するのではなく、それをクリアする方がよいでしょう。 –

+0

csvData.Clear();それをnullに設定するのではなく、 –

関連する問題