1

我不知道我是否正确执行此操作,但我有一个网格,我遍历网格以查看项目是否匹配。如果他们这样做,我想让该行每 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;
}
4

3 回答 3

1

难道你不需要再次改变颜色(到原来的)来产生闪烁的效果吗?

于 2012-08-17T14:57:12.393 回答
1

你应该使用线程。看代码:)

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();
        }
    }
于 2016-06-27T18:12:45.583 回答
-3

也许这个,不是吗?

private static void Highlight(object sender, EventArgs e)
{
    currentRow.DefaultCellStyle.BackColor = Color.Brown;
    System.Threading.Thread.Sleep(2000);
    currentRow.DefaultCellStyle.BackColor = Color.White;
}
于 2012-08-17T15:00:28.947 回答