使用c#上的按钮启动和停止.txt文件上的数据保存

时间:2015-12-26 05:31:36

标签: c# user-interface filestream

我有一个连接到电脑的传感器,数据像这张图片一样连续:GUI

我想添加两个开始和停止按钮并从开始按钮按下时间保存数据,并在按下停止按钮时停止保存数据。

我写这段代码:

richTextBox1.AppendText(textBox1.Text + "\n");
System.IO.File.WriteAllLines(@"C:\Users\Mohammad_Taghi\Desktop\a.txt",richTextBox1.Lines);

但是此代码将整个数据保存在.txt文件中,并且不可控制。

这是richtextbox2上的部分代码:

public void detectFingers(Leap.Frame frame)

        {
            foreach(Finger finger in frame.Fingers)
            {
                richTextBox2.AppendText("Finger ID: " + finger.Id + Environment.NewLine +
                                        "Finger Type: " + finger.Type + Environment.NewLine +
                                        "Finger Length:" + finger.Length + Environment.NewLine +
                                        "Finger width:" + finger.Width + Environment.NewLine);

                foreach (Bone.BoneType boneType in (Bone.BoneType[])Enum.GetValues(typeof(Bone.BoneType)))
                {
                    Bone bone = finger.Bone(boneType);
                richTextBox3.AppendText("Bone Type: " + bone.Type +Environment.NewLine +
                                        "Bone Length: " +bone.Length  +Environment.NewLine+
                                        "Bone Width : " + bone.Width +Environment.NewLine +
                                        "Previous Joint : "+bone.PrevJoint + Environment.NewLine+
                                        "Next Joint :" + bone.NextJoint + Environment.NewLine+
                                        "Direction : " + bone.Direction + Environment.NewLine+;


                }

            }


        }

1 个答案:

答案 0 :(得分:0)

随着数据的不断进入,您需要在数据存储时保存。当然,打印textBox1.Text会打印所有内容。

按下“开始”按钮后,需要设置一个变量来存储信息,直到按下“停止”按钮。这是一些代码:

private bool isLogging = false;
        private string myLog = "";

        //This is where the input from the sensor arrives
        private void myInput(string s)
        {
            textBox1.Text += s + "\n";
            if (isLogging)
                myLog += s + "\n";
        }
        private void buttonOnStart_Click(object sender, EventArgs e)
        {
            //Clear log string
            myLog = "";
            //Start logging
            isLogging = true;
        }

        private void buttonOnStop_Click(object sender, EventArgs e)
        {
            //Stop logging
            isLogging = false;
            //Pring only the logged messages
            System.IO.File.WriteAllLines(@"C:\Users\Mohammad_Taghi\Desktop\a.txt", myLog);
        }