【问题标题】:Buttons blank when Windows Forms application is startedWindows 窗体应用程序启动时按钮为空白
【发布时间】:2017-06-23 09:15:28
【问题描述】:

当我第一次打开我的 Windows 窗体应用程序时,一些按钮无法正确呈现,当窗体稍微移动时它们会出现。

见下文。

拖动它们出现的表单后。

这是我的启动代码。

[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;
                }
            }
        }
    }
}

这不是一个大问题,只是烦人,因为我不知道为什么会这样。

这是表单代码。

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);
    }
}

【问题讨论】:

  • 我认为问题不在于该代码...您是否在表单的构造函数或Load 事件中做任何事情?
  • 我同意@Pikoh。但是,您的 Main 方法看起来很可疑。你确定,带有 SetForegroundWindow 的分支是正确的吗?
  • 我不认为是@TcKs,应该是if (Process.GetProcessesByName(current.ProcessName).Any()) {SetForegroundWindow(process.MainWindowHandle); 。但这与问题无关:)

标签: c# .net winforms


【解决方案1】:

似乎在dataGridView1_CellFormatting中做的太多会导致问题。

我将代码缩小到:

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;
        }
    }
}

并将默认行颜色设置为绿色,现在它可以正常工作了。

【讨论】:

  • 您不必在CellFormatting 事件中进行foreach。这大概就是问题所在。在DataGridViewCellFormattingEventArgs 中,您会看到正在处理的行,这就是您需要更改的行。这显然会减慢电网速度
猜你喜欢
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多