我一直试图这样做一段时间,只是无法让代码执行我想要的方式。在C#visual studio中运行程序时,我单击“提交”按钮,而不是因为空白文本框而提示错误,它什么也没做。我想知道如何执行errorProvider或" errorCheck"在我的代码中的SUBMIT按钮(txtSubmit),以便它显示错误。
这是我到目前为止的代码:
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; //input output external files (.text)
using System.Text.RegularExpressions; //handles errors
using System.Xml;
namespace H1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
//Exits the application when pressed
Application.Exit();
}
private void btnClear_Click(object sender, EventArgs e)
{
//clears the fields when clicked
txtPid.Text = ""; //Patient Information Fields
txtPname.Text = "";
txtPphone.Text = "";
txtPaddress.Text = "";
txtPemail.Text = "";
txtPhysician.Text = ""; //Last visit fields
txtDate.Text = "";
txtReason.Text = "";
txtIid.Text = ""; //Insurance information fields
txtIcompany.Text = "";
txtIphone.Text = "";
txtIinsured.Text = "";
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string[] dat = new string[12]; //Declare new string array, 12 because 0 is included in count
dat[0] = txtPid.Text; //Patient Information Fields
dat[1] = txtPname.Text;
dat[2] = txtPphone.Text;
dat[3] = txtPaddress.Text;
dat[4] = txtPemail.Text;
dat[5] = txtPhysician.Text; //Last visit fields
dat[6] = txtDate.Text;
dat[7] = txtReason.Text;
dat[8] = txtIid.Text; //Insurance information fields
dat[9] = txtIcompany.Text;
dat[10] = txtIphone.Text;
dat[11] = txtIinsured.Text; //Array is now fully populated
StreamWriter sw = new StreamWriter("PatientInfo.txt"); //Declare a new writer
for(int i = 0; i < dat.Length; i++) //Iterate through the array
{
sw.WriteLine(dat[i]); //Writes the current position in a new line to specified file
}
sw.Close(); //Close our writer, don't need unused junk left open
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("PatientInfo.txt"); //Specifies a new stream reader to read the file "File.dat"
String dat = sr.ReadToEnd(); //Reads the file until end and assigns it to the string "dat"
sr.Close(); //Close reader
dat = dat.Replace("\r", ""); //Removes all instances of carriage return, for handling/compatibility
string[] arr = dat.Split('\n'); //Splits the string into an array based on the new line character
}
private void txtPid_Validating(object sender, CancelEventArgs e)
{
// defines match
Match m = Regex.Match(txtPid.Text, @"\b[0-7]{7}\b"); //regular expressions
if (m.Success == false) //if does not match then provide below message
errorCheck.SetError(txtPid, "Patent I.D. must be 7 characters."); //error message
else errorCheck.SetError(txtPid, ""); //clears
}
private void txtPname_Validating(object sender, CancelEventArgs e)
{
if (txtPname.Text.Length < 3) //checks to see if there are less than 3 characters
{
errorCheck.SetError(txtPname, "Please enter the first and last name.");
}
else if (txtPname.Text.IndexOf(' ') == -1)
{
errorCheck.SetError(txtPname, "Must have both names seperated by a space.");
}
else errorCheck.SetError(txtPname, ""); //clears
}
private void txtPaddress_Validating(object sender, EventArgs e)
{
if (txtPaddress.Text == "")
{
errorCheck.SetError(txtPaddress, "You must enter the address.");
}
else errorCheck.SetError(txtPaddress, ""); //clears
}
private void txtPphone_Validating(object sender, CancelEventArgs e)
{
if (txtPphone.Text != "Valid")
{
errorCheck.SetError(txtPphone, "Telephone Number must be in proper format");
}
else errorCheck.SetError(txtPphone, "");
}
private void txtPemail_Validating(object sender, CancelEventArgs e)
{
if (txtPemail.Text == "") //checking for blanks
{
errorCheck.SetError(txtPemail, "You must enter an e-mail address.");
}
else if (txtPemail.Text == "@") //checking for @
{
errorCheck.SetError(txtPemail, "You must enter a valid e-mail address.");
}
else errorCheck.SetError(txtPemail, "");
}
private void txtIid_Validating(object sender, CancelEventArgs e)
{
Match m = Regex.Match(txtIid.Text, @"\b[0-7]{7}\b");
if (m.Success == false)
errorCheck.SetError(txtIid, "Insurance I.D. must be 7 characters.");
else errorCheck.SetError(txtIid, ""); //clear
}
private void txtIcompany_Validating(object sender, CancelEventArgs e)
{
if (txtIcompany.Text == "") //checking for blanks
{
errorCheck.SetError(txtIcompany, "You must enter an Insurance Company.");
}
else errorCheck.SetError(txtIcompany, "");
}
private void txtIphone_Validating(object sender, CancelEventArgs e)
{
if (txtIphone.Text != "Valid")
{
errorCheck.SetError(txtIphone, "Telephone Number must be in proper format");
}
else errorCheck.SetError(txtIphone, "");
}
private void txtIinsured_Validating(object sender, CancelEventArgs e)
{
if (txtIinsured.Text.Length < 4) // checking entry length
{
errorCheck.SetError(txtIinsured, "Please enter the first and last name.");
}
else if (txtIinsured.Text.IndexOf(' ') == -1) //checks for space
{
errorCheck.SetError(txtIinsured, "Must have both names.");
}
else errorCheck.SetError(txtIinsured, "");
}
private void txtDate_Validating(object sender, CancelEventArgs e)
{
if (txtDate.Text == "")
{
errorCheck.SetError(txtDate, "Must enter a correct date.");
}
else errorCheck.SetError(txtDate, "");
}
private void txtPhysician_Validating(object sender, CancelEventArgs e)
{
if (txtPhysician.Text == "") //checking for blanks
{
errorCheck.SetError(txtPhysician, "Must enter the Physician's name.");
}
else errorCheck.SetError(txtPhysician, "");
}
private void txtReason_Validating(object sender, CancelEventArgs e)
{
if (txtReason.Text == "")
{
errorCheck.SetError(txtReason, "Must enter a reason for visit.");
}
else errorCheck.SetError(txtReason, ""); //clears
}
}
}