0

我不确定在此 If/Else 语句中调用函数时要传递哪些参数。

If/Else 语句正在调用 2 个函数中的 1 个,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;
            }
        }
}

任何帮助表示赞赏!

4

2 回答 2

3

您应该做的是获取Online_Register/Offline_Register事件处理程序的代码并将其放入名为的不同方法中:例如,您可以这样做OnlineRegisterOfflineRegister

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.

于 2013-09-26T10:50:48.120 回答
-1

检查 FullFileName 变量值,方法看起来很完美。始终隔离 UI 和功能事件。

于 2013-09-26T10:55:57.073 回答