Jason Wragg
Jason Wragg

Reputation: 589

Buttons blank when Windows Forms application is started

When I open my Windows Forms application for the first time some of the buttons do not render correctly, when the form is move slightly they appear.

See below.

before

after dragging the form they appear.

after

This is my startup code.

[STAThread]
static void Main()
{
    bool createdNew = true;

    using (Mutex mutex = new Mutex(true, "Support_Desk_System", out createdNew))
    {
        if (createdNew)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            GC.KeepAlive(mutex);
        }
        else
        {
            Process current = Process.GetCurrentProcess();

            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    SetForegroundWindow(process.MainWindowHandle);
                    break;
                }
            }
        }
    }
}

It's not a major issue just annoying because i have no idea why it's happening.

This is the form code.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.status_CodesTableAdapter.Fill(this.supportDeskDataSet.Status_Codes);
        this.systemsTableAdapter.Fill(this.supportDeskDataSet.Systems);
        this.cases_Quick_ViewTableAdapter.Fill(this.supportDeskDataSet.Cases_Quick_View);
        this.dataGridView1.DoubleBuffered(true);
    }

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        foreach (DataGridViewRow Myrow in dataGridView1.Rows)
        {    
            if (Myrow.Cells[7].Value.ToString() == "High")
            {
                Myrow.DefaultCellStyle.BackColor = Color.Red;
            }
            else
            {
                Myrow.DefaultCellStyle.BackColor = Color.Green;
            }
            if(Myrow.Cells[2].Value.ToString() == "Back Burner")
            {
                Myrow.DefaultCellStyle.BackColor = Color.Gray;
                Myrow.DefaultCellStyle.ForeColor = Color.White;
            }
        }
    }
}
public static class DataGridViewExtensioncs
{
    public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        var dgvType = dgv.GetType();
        var pi = dgvType.GetProperty("DoubleBuffered",
        BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, null);
    }
}

Upvotes: 0

Views: 158

Answers (1)

Jason Wragg
Jason Wragg

Reputation: 589

It seems that doing too much in dataGridView1_CellFormatting causes the problem.

I scaled the code down to :

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    foreach (DataGridViewRow Myrow in dataGridView1.Rows)
    {    
        if (Myrow.Cells[7].Value.ToString() == "High")
        {
            Myrow.DefaultCellStyle.BackColor = Color.Red;
        }
        //else
        //{
        //  Myrow.DefaultCellStyle.BackColor = Color.Green;
        //}
        if(Myrow.Cells[2].Value.ToString() == "Back Burner")
        {
            Myrow.DefaultCellStyle.BackColor = Color.Gray;
            Myrow.DefaultCellStyle.ForeColor = Color.White;
        }
    }
}

and set the default row colour to Green instead and now it works without issue.

Upvotes: 1

Related Questions