我是C#编程的新手,在弄清楚如何执行此操作时遇到了一些麻烦。 我想在程序中说,如果未填充textBoxesstreamWriter不应将任何内容发送到文件,并且messageBox弹出messageBox 到目前为止,仅messageBox弹出messageBox ,但仍将信息发送到文件中。 我正在尝试使用类似这样的东西if ((fileOut != null))但是我找不到插入它的好地方,或者这是应该使用的东西。 任何帮助将非常感激。 谢谢!

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

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


        // Calculate payroll
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                decimal hoursWorked;     
                decimal hourlyPayRate;    
                decimal basePay;          
                decimal overtimeHours;
                decimal overtimePay;       
                decimal grossPay;        

                // Get the hours worked and hourly pay rate. 
                hoursWorked = decimal.Parse(txtHoursWorked.Text);
                hourlyPayRate = decimal.Parse(txtHourlyRate.Text);

                // Determine the gross pay. 
                if (hoursWorked >= 40)
                {
                    // Calculate the base pay (without overtime). 
                    basePay = hourlyPayRate * 40;

                    // Calculate the number of overtime hours. 
                    overtimeHours = hoursWorked - 40;

                    // Calculate the overtime pay. 
                    overtimePay = overtimeHours * hourlyPayRate * 1.5m;

                    // Calculate the gross pay. 
                    grossPay = basePay + overtimePay;
                }
                else
                {
                    // Calculate the gross pay. 
                    grossPay = hoursWorked * hourlyPayRate;
                }

                // Display the gross pay. 
                lblGrossPay.Text = Convert.ToString(grossPay);
            }
            catch (Exception ex)
            {
                // Display an error message. 

                MessageBox.Show(ex.Message);

            } //Writes text to files

                StreamWriter fileOut = new StreamWriter("employeePay.dat", true);
                //if (!(fileOut != null))//I am trying this. However stated this way        nothing is passed to the file
                {
                    fileOut.Write(txtName.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtNumber.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtHourlyRate.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtHoursWorked.Text);
                    fileOut.Write(",");
                    fileOut.WriteLine(lblGrossPay.Text);
                }
            fileOut.Close();
            LoadContacts();
        }

        // Clear all text from output labels & input textboxes        
        private void btnClera_Click(object sender, EventArgs e)
        {
            // Clear the TextBoxes and gross pay label. 
            txtName.Text = "";
            txtNumber.Text = "";
            txtHoursWorked.Text = "";
            txtHourlyRate.Text = "";
            lblGrossPay.Text = "";

            // Reset the focus. 
            //txtHoursWorked.Focus();
        }

        // End program        
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void LoadContacts()
        {

            try
            {

                lstEmployeePay.Items.Clear();

                string employeePay;


                if (File.Exists("employeePay.dat"))
                {
                    StreamReader fileIn = new StreamReader("employeePay.dat");

                    while (!fileIn.EndOfStream)
                    {

                        employeePay = fileIn.ReadLine();

                        string[] fields = employeePay.Split(',');

                        lstEmployeePay.Items.Add(fields[0]);
                        lstEmployeePay.Items.Add(fields[1]);
                        lstEmployeePay.Items.Add(fields[2]);
                        lstEmployeePay.Items.Add(fields[3]);
                        lstEmployeePay.Items.Add(fields[4]);
                        lstEmployeePay.Items.Add("");

                    }

                    fileIn.Close();   
                }    

            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("The file does not exist, please try again");
            }
            catch (Exception ex)
            {

            }    
        }

    }
}

===============>>#1 票数:0

您所需要的只是显示消息框后的return语句。 喜欢

MessageBox.Show("");
return;

我还要在这里提出其他一些建议。

  1. 您可以执行以下操作检查文本框是否为空

    如果(string.IsNullorWhiteSpace(yourtextboxt.text)

  2. 您应该使用decimal.tryparse进行解析。 它不会引发异常。

===============>>#2 票数:0

您需要将流编写器移到异常处理中,因为catch仍然执行之后的所有操作。

我还考虑将StreamWriter包装在using语句中,以确保函数退出时将其关闭。

try
{
   // other code here

using(var fileOut = new StreamWriter("employeePay.dat", true)
                {
                    fileOut.Write(txtName.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtNumber.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtHourlyRate.Text);
                    fileOut.Write(",");
                    fileOut.Write(txtHoursWorked.Text);
                    fileOut.Write(",");
                    fileOut.WriteLine(lblGrossPay.Text);
                }
}
catch(...)
{
   MessageBox();
}

// any code after the catch will still execute

  ask by floyd translate from so

未解决问题?本站智能推荐: