2016-05-27 139 views
0

我有一個列表視圖中的繪製複選框。點擊繪製複選框

當前當我點擊列表視圖上繪製的複選框時,它會單擊該列中的第一個複選框,或者完全沒有。

我知道它與temppoint有關,所以在我發佈這個希望之後,我可以在有人回答之前自己找到解決方案:P。

這是該複選框被繪製

private void lstSourceToUser_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) 
{ 
    if (e.ColumnIndex > 1) 
    { 
     int usrIndex = userProfile.GetUserIndexByID(e.Header.Name); 
     int srcIndex = userProfile.GetUsersSourceIndex(e.Header.Name, e.Item.SubItems[0].Text); 
     Graphics g = e.Graphics; 
     CheckBoxRenderer.DrawCheckBox(g,new Point((e.Bounds.X + e.Bounds.Width/2 -10),(e.Bounds.Y)), userProfile.SystemUserList[usrIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal); 
    } 
    else 
     // Draw the other subitems with default drawing. 
     e.DrawDefault = true; 
} 

而這正是點擊事件發生

private void lstSourceToUser_MouseClick(object sender, MouseEventArgs e) 
{ 
    ListViewItem.ListViewSubItem subItem = lstSourceToUser.HitTest(e.X, e.Y).SubItem; 
    if (subItem.Name != "") 
    { 
     Point tempPoint = new Point(e.X, e.Y); 
     int userIndex = userProfile.GetUserIndexByName(subItem.Name); 
     Rectangle rect = new Rectangle(subItem.Bounds.X, subItem.Bounds.Y, 100, subItem.Bounds.Height); 
     if (rect.Contains(tempPoint)) 
     { 
      int srcIndex = userProfile.GetUserSourceIndexByUserName(subItem.Name, lstSourceToUser.SelectedItems[0].SubItems[0].Text); 
      userProfile.SystemUserList[userIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState = !userProfile.SystemUserList[userIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState; 
      this.lstSourceToUser.Invalidate(); 
     } 
    } 
} 

任何幫助,將其解決方案或鏈接/導遊:) 謝謝理解

+0

檢查'subItem.Bounds.Y'是否報告所有項目的正確y位置! – TaW

回答

0

問題不是tempPoint它是srcIndex。

if (rect.Contains(tempPoint)) 
    { 
     **int srcIndex = userProfile.GetUserSourceIndexByUserName(subItem.Name, lstSourceToUser.SelectedItems[0].SubItems[0].Text);** 
     userProfile.SystemUserList[userIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState = !userProfile.SystemUserList[userIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState; 
     this.lstSourceToUser.Invalidate(); 
    } 

public int GetUserSourceIndexByUserName(string userName, string sourceId) 
    { 
     foreach (FCMUser user in SystemUserList) 
     { 
      if (**user.id** != userName) continue; 
      var index = 0; 
      foreach (FCMSource src in user.fusionUserSources) 
      { 
       if (src.id == sourceId) 
       { 
        return index; 
       } 
       index++; 
      } 
     } 
     return 0; 
    } 

if語句尋找匹​​配的userName是不正確的。它正在檢查用戶ID而不是用戶名。

來自我的愚蠢錯誤。生活和學習我猜。