如何每隔3秒闪一次行

时间:2012-08-17 14:53:32

标签: c# winforms

我不知道我是否正确地做了这个,但我有一个网格,我在网格中循环查看项目是否匹配。如果他们这样做,我想让该行每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;
}

3 个答案:

答案 0 :(得分:1)

您是否需要再次更改颜色(到原始颜色)以获得闪烁效果?

答案 1 :(得分: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();
        }
    }

答案 2 :(得分:-3)

也许这个,不是吗?

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