我不确定在调用此If / Else语句中的函数时我想要传递哪些参数。
If / Else语句调用2个函数中的一个,即Online_Version或Offline Version。
代码如下:
public void Form1_Load(object sender, EventArgs e)
{
if (MessageBox.Show("Would you like to run the Event Register?", "Registration Selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
label5.Text = "Event Registration";
textBox1.Select();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
var fileSave = new FileStream(fullFileName, FileMode.Create);
fileSave.Close();
OfflineRegister();
}
else
{
label5.Text = "ICAS Register";
textBox1.Select();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
var fileSave = new FileStream(fullFileName, FileMode.Create);
fileSave.Close();
OnlineRegister();
}
}
public void Online_Register(object sender, KeyPressEventArgs e)
{
OnlineRegister();
}
public void Offline_Register(object sender, KeyPressEventArgs e)
{
OfflineRegister();
}
public void OnlineRegister()
{
SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
Object returnValue;
string txtend = textBox1.Text;
string lastTwoChars = txtend.Substring(txtend.Length - 1);
if (textBox1.Text.Length != 6 && e.KeyChar != '*') return;
//cmd.CommandText = ("SELECT last_name +', '+ first_name +'\t ('+major_key+')\t' from name where id =@Name");
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
//Time = DateTime.Now.ToString("HH:mm");
//TimeIn = "Time In: ";
//TimeOut = "Time Out: ";
returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")";
DBConnection.Close();
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
listBox1.Items.RemoveAt(n);
//listBox1.Items.Add(removelistitem + " " + 'TimeOut' + 'Time');
}
}
}
else
listBox1.Items.Add(returnValue);
textBox1.Clear();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
sw.WriteLine(item.ToString());
sw.Flush();
sw.Close();
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
label6.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
e.Handled = true;
}
public void OfflineRegister()
{
Object returnValue;
string txtend = textBox1.Text;
returnValue = textBox1.Text.Replace(@"*", "");
if (e.KeyChar != '*') return;
{
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
//listBox1.Items.RemoveAt(n);
}
}
}
else
{
listBox1.Items.Add(returnValue);
textBox1.Clear();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
sw.WriteLine(item.ToString());
sw.Flush();
sw.Close();
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
label6.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
e.Handled = true;
}
}
}
感谢任何帮助!
答案 0 :(得分:3)
您应该采用Online_Register
/ Offline_Register
事件处理程序的代码ouf,并将其放在一个名为OnlineRegister
和OfflineRegister
的不同方法中,例如,这样你就可以做到这一点:
public void Form1_Load(object sender, EventArgs e)
{
if (MessageBox.Show("Would you like to run the Event Register?","Registration Selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
label5.Text = "Event Registration";
textBox1.Select();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
var fileSave = new FileStream(fullFileName, FileMode.Create);
fileSave.Close();
OfflineRegister();
}
else
{
label5.Text = "ICAS Register";
textBox1.Select();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
var fileSave = new FileStream(fullFileName, FileMode.Create);
fileSave.Close();
OnlineRegister();
}
}
public void Online_Register(object sender, KeyPressEventArgs e)
{
OnlineRegister();
}
public void Offline_Register(object sender, KeyPressEventArgs e)
{
OfflineRegister();
}
public void OnlineRegister()
{
// Do Stuff
}
public void OfflineRegister()
{
// Do Stuff
}
这当然假设您确实需要KeyPress
事件处理程序。
<强>解释强>
上面代码的底部显示了我刚刚创建的两个方法。这些可以在事件处理程序和Form1_Load
事件中调用。这很有用,因为您不必反复粘贴相同的代码。
<强>改进强>
您可以通过将Register
代码放入另一个类(可能称为RegisterHelper
或其他类)来改进当前方案,这样可以为注册用户提供逻辑。
此外,您可以为表单指定一个更合适的名称,而不是Form1
。
答案 1 :(得分:-1)
检查FullFileName变量值,方法看起来很完美。始终隔离UI和功能事件。