1

正如标题所说,我有一个 application.exe,它运行并将数据从数字万用表提取到控制台输出中,之后我成功地将控制台输出重定向到 C# windows 窗体中的richTextBox。现在,我要问的是是否可以检查/验证特定的字符串、浮点数、双精度、整数,以便我可以根据它设置通过/失败条件。

代码如下;

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.Diagnostics;
using System.IO;


namespace xxxx
{
    public partial class Main : Form
    {
        //Path of the respective folder
        string mainFolder = "C:\\xxx\\xxx\\";
        string fileDateTime = DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss");

        public Main()
        {
            InitializeComponent();
            this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            this.textBox2.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //'Limit to 8 Numbers
            e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == 8);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //'Limit to 16 alphanumeric characters
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == 8) && !(char.IsDigit(e.KeyChar) || e.KeyChar == 8);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                //'Error Message
                MessageBox.Show("xxx ID cannot be empty!");
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                //'Error Message
                MessageBox.Show("xxx No cannot be Empty!");
            }
            else
            {
                //'Start the .exe file to generate readings
                ProcessStartInfo DMM = new ProcessStartInfo("BasicFunctions.exe");
                DMM.UseShellExecute = false;
                DMM.RedirectStandardOutput = true;
                DMM.CreateNoWindow = true;
                var proc = Process.Start(DMM);

                //'Read value from the console output
                string value = proc.StandardOutput.ReadToEnd();

                //'Redirect console output to richtextbox
                richTextBox1.Text = value;


                //'Read selected voltage values from richtextbox
                string selectedvalue = richTextBox1.SelectedText;
                richTextBox1.SelectionStart = 100;
                richTextBox1.SelectionLength = 1000;


                //'Check for xxx folder existence and create it if not
                System.IO.Directory.CreateDirectory(mainFolder);


                //'Append richtextbox input to logfile
                using (StreamWriter writer = new StreamWriter(mainFolder + textBox2.Text + ".txt", true))
                {
                    //File.WriteAllText(textBox2.Text, "\r\n" + richTextBox1.Text);
                    writer.Write(label2.Text + " " + textBox1.Text + "\r\n" + "Date/Time: " + fileDateTime + "\r\n" +                       richTextBox1.Text + "\r\n");
                }

                //'Condition for Pass/Fail 
                if ((selectedvalue == "0.0005") || (selectedvalue == "0.0006") || (selectedvalue == "0.0007") ||                           (selectedvalue == "0.0008") || (selectedvalue == "0.0009") || (selectedvalue == "0.0010") ||                           (selectedvalue == "0.0011") || (selectedvalue == "0.0012") || (selectedvalue == "0.0013") ||                           (selectedvalue == "+1.5"))
                {
                    label4.Visible = true;
                }
                else
                {
                    label5.Visible = true;
                }
            }

            /*Send code status to xxx
             * 
            if (label4.Visible == true)
            {
                string args = (textBox1.Text + "x" + textBox2.Text + "xxx");

                Process.Start("cmd.exe", "/c xxx.exe " + args + " && pause");
            }
            else if (label5.Visible == true)
            {
                string args = (textBox1.Text + "x" + textBox2.Text + "xxx");

                Process.Start("cmd.exe", "/c xxx.exe " + args + " && pause");
            }
            else
            {
                MessageBox.Show("Sending to xxx Server has failed, please try again", MessageBoxButtons.OK,                            MessageBoxIcon.Error);
            }
             */
        }
        private void Main_Load(object sender, EventArgs e)
        {
            //'Make Pass/Fail label invisible at start
            label4.Visible = false;
            label5.Visible = false;

            //'Focus on Operator textbox
            textBox1.Select();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Sending to xxx has been turned on...", "Note", MessageBoxButtons.OK,                                MessageBoxIcon.Information);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Sending to xxx has been turned off...", "Note",                                                      MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

}
4

0 回答 0