2012-08-17 43 views
1

我不知道如果我正確地這樣做,但我有一個網格,我遍歷網格來看看項目相匹配。如果他們這樣做,我想讓行每3秒閃一次。現在我在代碼中所擁有的只是突出顯示行但不閃爍。任何人都可以幫忙看一下嗎?如何讓行閃光每3秒

public static void CheckRow(int item, DataGridViewRow row) 
{ 
    List<int> col = new List<int>(); 
    //call to db and add to col 

    foreach (var item in col) 
    { 
     if (item == col.Item) 
     { 
      currentRow = row; 
      Timer t = new Timer(); 
      t.Interval = 3000; 
      t.Tick += new System.EventHandler(Highlight); 
      t.Start(); 
     } 
    } 
} 

private static void Highlight(object sender, EventArgs e) 
{ 
    currentRow.DefaultCellStyle.BackColor = Color.Brown; 
} 
+0

你是怎麼調用'CheckRow()'? – banging 2012-08-17 14:58:01

+0

爲什麼區間'5000'? – banging 2012-08-17 14:58:38

+0

'currentRow'在哪裏? – banging 2012-08-17 14:59:15

回答

1

難道你不需要再次改變顏色(原來的)有一個閃爍的效果?

+0

我會在那裏放? – Calvin 2012-08-17 14:57:45

+0

同意了,但我想有必須是「折戟」,否則,可能會改變這麼快發生,人眼就無法把它撿起來之前,一些短暫的延遲。 – 2012-08-17 14:58:29

+0

首先,你看起來就像你用「currentRow」作爲您的高亮方法獲得的方式要改變... ...行是你真正想要的?如果不止一行需要閃光怎麼辦?我可能會看看使用後臺工作線程,調用UI線程設置顏色所需行(通過行作爲參數傳遞給後臺工作線程),然後睡(在後臺線程),然後再調用行的顏色變回原來的顏色。至少這似乎是一種做法。 – 2012-08-17 15:01:50

-3

或許這,不是嗎?

private static void Highlight(object sender, EventArgs e) 
{ 
    currentRow.DefaultCellStyle.BackColor = Color.Brown; 
    System.Threading.Thread.Sleep(2000); 
    currentRow.DefaultCellStyle.BackColor = Color.White; 
} 
+0

這會在UI線程上添加一個兩秒鐘的停頓 – Tergiver 2012-08-17 15:22:40

0

您應該使用線程。看代碼:)

bool go = false; //for changing cell color 
    int count = 10; //to stop timer (blinking) 
    public blinkForm() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     timer1.Start(); 
     Thread a = new Thread(blink); 
     a.Start(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     dataGridView1.AutoGenerateColumns = false; 
     if (dataGridView1.Columns.Count == 0) 
     { 
      //generate new columns for DataGridView 
      dataGridView1.Columns.Add("user", "User"); 
      dataGridView1.Columns.Add("pcStatus", "PC Status"); 
      dataGridView1.Columns.Add("service", "Servis"); 

      //generate new rows for DataGridView 
      dataGridView1.Rows.Add("Ali", "PC007", "chrome.exe"); 
      dataGridView1.Rows.Add("Vusal", "PC010", "photoshop.exe"); 
      dataGridView1.Rows.Add("Rahim", "PC015", "chrome.exe"); 
     } 

    } 

    private void blink(object o) 
    { 
     while (count > 0) 
     { 
      while (!go) 
      { 
       //change color for binking 
       dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.Tomato; 
       go = true; 
       //stop for 0.5 second 
       Thread.Sleep(500); 
      } 

      while (go) 
      { 
       //change color for binking 
       dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.LimeGreen; 
       go = false; 
       //stop for 0.5 second 
       Thread.Sleep(500); 
      } 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     count--; 
     if (count == 0) 
     { 
      //stop blinking after 10 second 
      timer1.Stop(); 
     } 
    }