2013-09-26 61 views
0

我不確定在If/Else語句中調用函數時要傳遞哪些參數。由於參數,無法調用方法

If/Else語句正在調用2個函數之一Online_Version或Offline版本。

代碼如下:

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 [email protected]"); 
     cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id [email protected]"); 
     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; 
      } 
     } 
} 

任何幫助表示讚賞!

+2

他們看起來像他們應該用作事件處理程序給我。可能要處理重要新聞事件。像'this.KeyDown + = OfflineRegister;'可能是你正在尋找的東西。然後,當按下某個鍵時,您的'OfflineRegister'處理程序將響應事件的觸發。 – Jodrell

+1

沒有關於這些方法做什麼的更多細節,唯一合理的猜測將是Form1_Load函數,sender和e的參數。 –

+0

我認爲你應該在某些方法的事件處理程序中包裝代碼,並在'if/else'中調用這些方法。 – V4Vendetta

回答

3

你應該做的是採取Online_Register/Offline_Register事件處理程序的代碼OUF並把它放在一個不同的方法叫: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事件處理程序。上述

說明

代碼的底部示出了我剛創建的兩種方法。這些可以從事件處理程序和事件中調用。這很有用,因爲您不必反覆粘貼相同的代碼。

改進 你可以通過採取Register代碼,並把它不同的類中提高你的當前情況下,也可以叫做,RegisterHelper什麼的,其用於註冊用戶提供邏輯的目的。

此外,你可以給你的表單一個更合適的名稱,而不是Form1

+0

當我重新列出這樣的代碼時,我不能使用我的'if(e.KeyChar!='*')return;'因爲我無法通過'e'。這是位於我的在線註冊和離線 –

+0

在這種情況下,您可以通過'e'參數發送到在線和離線方法。只需在方法'char character'中添加一個參數即可。如果表單加載,你可以發送null。在聯機和脫機方法中,您可以檢查它是否爲空。 – Subby

+0

對不起,我不太瞭解C#的參數。我編輯了我的帖子以顯示我當前的代碼狀態 –

-1

檢查FullFileName變量的值,方法看起來很完美。始終隔離用戶界面和功能事件。