2013-10-03 196 views
1

我編寫的程序是一個註冊表,當條形碼被掃描時,將進入數據庫並提取人名和I.D.號,也列出了「時間:」在此期間,他們掃描,然後當掃描他們第二次將增加一個組成部分。「超時:」像這樣的:將項目從一個列表框移動到另一個列表框 - C#

Peters, Alastair (242242) Time In : 14:50 Time Out : 14:50 Time In : 14:50

我在找它做的是當我第一次在你掃描將看到:

Peters, Alastair (242242) Time In : 14:50

,然後當你掃描第二次將在Time Out :添加,然後刪除整條生產線,並將其存儲在一個第二個列表框,所以不顯示該人何時不在。

然後當在(第3次)的人掃描將重新列表中的上線,但隨着新Time In

任何建議或想法將是非常有益的!

這裏是我的代碼:

private string CreateNewEntry(string current) 
    { 
     var indexIn = current.LastIndexOf("Time In : "); // Get the last index of the word "in" 
     var indexOut = current.LastIndexOf("Time Out : "); // Get the last index of the word out 

     if (indexOut > indexIn) 
     { 
      return current + "  "+"Time In : "; // if the last "out" comes after the last "in" 
     } 
     else 
     { 
      // If the last "in" comes after the last "out" 
      return current + "  " +"Time Out : "; 
     } 
    } 

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand(); 

     Object returnValue; 

     string txtend = textBox1.Text; 

     if (e.KeyChar == 'L') 
     { 
      DBConnection.Open(); 
     } 
     if (DBConnection.State == ConnectionState.Open) 
     { 
      if (textBox1.Text.Length != 6) return; 
      { 
       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; 

       returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")"; 

       DBConnection.Close(); 

       bool found = false; 

       foreach (var item in listBox1.Items) 
       { 
        var entry = item.ToString(); 
        if (entry.Contains(returnValue.ToString())) 
        { 
         listBox1.Items.Remove(item); 
         listBox1.Items.Add(CreateNewEntry(entry) + DateTime.Now.ToString("HH:mm")); 
         found = true; 
         break; 
        } 
       } 

       if (!found) 
       { 
        listBox1.Items.Add(returnValue + "  " + "Time In : " + DateTime.Now.ToString("HH:mm")); 
       } 

       textBox1.Clear(); 

       System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName); 
       foreach (object item in listBox1.Items) 
       SaveFile.WriteLine(item.ToString()); 
       SaveFile.Flush(); 
       SaveFile.Close(); 

       if (listBox1.Items.Count != 0) { DisableCloseButton(); } 
       else 
       { 
        EnableCloseButton(); 
       } 
       Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance."; 
       e.Handled = true; 
      } 
     } 
+0

我很樂意提供幫助。你能解釋一下哪些不適合你嗎? – uSeRnAmEhAhAhAhAhA

+0

謝謝@Giovani。 Theres沒有什麼「不是」工作,我只需要建議或幫助試圖找出上述。 當我掃描自己「出」,在偶數的掃描,我希望它整個行移動到另一個列表框來隱藏它,並添加「超時:」,然後當我掃描回來,在一個奇數次的掃描,我想讓它重新進入第一個列表框,添加了「Time in」 –

回答

0

據我所知,起始位(例如, 「彼得斯,阿拉斯泰爾(242242)」)是唯一的,保持不變;在這種情況下,您可以使用FindString。示例代碼:

int curIndex = listBox1.FindString("Peters, Alastair (242242)"); 
string wholePetersRecord = ""; 
if (curIndex >= 0) 
{ 
    wholePetersRecord = listBox1.Items[curIndex].ToString(); 
    listBox1.Items.RemoveAt(curIndex); 
} 

該代碼就會發現,店wholePetersRecord並刪除第一個項目開始,「彼得斯,阿拉斯泰爾(242242)」(「彼得斯,阿拉斯泰爾(242242)時間:14:50超時: 14:50時間:14:50「)。

相關問題