2017-06-23 91 views
0

當我第一次打開我的Windows窗體應用程序時,某些按鈕無法正確呈現,當窗體稍微移動時,它們就會出現。Windows窗體應用程序啓動時空白按鈕

見下文。

before

拖動它們出現的形式後

after

這是我的啓動代碼。

[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); 
    } 
} 
+2

我不認爲問題出現在那個代碼中......你在窗體的構造函數或Load事件中做什麼? – Pikoh

+0

我同意@Pikoh。但是你的Main方法看起來很可疑。你確定,具有SetForegroundWindow的分支是正確的嗎? – TcKs

+0

我不認爲它是@TcKs,它應該像'if(Process.GetProcessesByName(current.ProcessName).Any()){SetForegroundWindow(process.MainWindowHandle); '。但它與問題無關:) – Pikoh

回答

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

和默認的行顏色爲綠色,而不是和現在的作品沒有問題。

+3

您不必在'CellFormatting'事件中執行foreach。這可能是問題所在。在'DataGridViewCellFormattingEventArgs'中,你得到了正在處理的行,這就是你需要改變的行。這顯然會減緩電網 – Pikoh

相關問題