如果fileOut的语句不能正常工作

时间:2014-04-21 02:59:20

标签: c# streamwriter

我对C#编程很新,并且在查明如何执行此操作时遇到了一些麻烦。我想在程序中说如果textBoxes未填写streamWriter而不是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}}

2 个答案:

答案 0 :(得分:0)

显示消息框后,您只需要一个return语句。像

MessageBox.Show("");
return;

我还想在此提出一些其他建议。

  1. 您可以通过

    检查文本框是否为空

    如果(string.IsNullorWhiteSpace(yourtextboxt.text)

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

答案 1 :(得分: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
相关问题