2013-01-02 21 views
0

在用戶控制代碼IM在頂部做:爲什麼變量計數器0一直都是?

int counter; 

在構造:

counter = 0; 

在MouseDown事件即時通訊做:

private void listBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      int index = listBox1.IndexFromPoint(e.X, e.Y); 

      if (e.Button == System.Windows.Forms.MouseButtons.Right) 
      { 
       if (m_itemIndexes.Contains(index)) 
        return; 

       m_itemIndexes.Add(index); 
       DrawItem(index); 
       counter += 1; 
      } 

我用在斷點計數器+ = 1;並看到每次點擊鼠標右鍵時它正在增長一次。

然後我在底部添加屬性的計數器:

[Browsable(true)] 
     public int RedCounts 
     { 
      get { return counter; } 
      set 
      { 
       counter = value; 
       Refresh(); 
      } 
     } 

然後在在Form1上我所做的:

ListBoxControl lb1; 

在構造函數中:

lb1 = new ListBoxControl(); 

然後在Form1的底部添加:

private void deleteSelectedLightningsToolStripMenuItem_Click(object sender, EventArgs e) 
      { 
       if (MessageBox.Show("Are you Sure you want to delete " + lb1.RedCounts + " the selected files ? Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) 
       { 

       } 
       else 
       { 

       } 
      } 

但是RedCounts的結果始終是0我不知道爲什麼。

編輯:

我發現,如果我做而不是counter = 0;做counter = 1;而不是counter + = 1;做RedCounts + = 1;然後使用RedCounts上的斷點,我看到計數器從1開始增長。1,2,3,4 ....

由於某些原因,問題出在Form1上,當我單擊使用斷點的deleteSelectedLightningsToolStripMenuItem_Click那麼lb1.RedCounts是1,因爲某些原因,它可能從屬性或行計數器= 1獲得值1;我不知道爲什麼。所以如果我設置counter = 121;那麼lb1.RedCounts會告訴我121.奇怪。

這是完整的用戶控制與計數器和RedCounts:

/*---------------------------------------------------------------- 
* Module Name : ListBoxControl 
* Description : Change listBox items color 
* Author  : Danny 
* Date   : 30/12/2012 
* Revision  : 1.00 
* --------------------------------------------------------------*/ 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

/* 
* Introduction : 
* 
* By default the color is red. 
* Added a property to change the color. 
* Right mouse click on item to change item color. 
* Left mouse click on item to change the item color back. 
* If the listBox is empty the control will be filled with 10 "Test" items. 
* */ 

namespace Lightnings_Extractor // to check how and change the namespace to listBoxControl 
{ 
    public partial class ListBoxControl : UserControl 
    { 
     private Color m_MyListColor; 
     private List<int> m_itemIndexes = new List<int>(); 
     private List<int> m_coloringItemIndexes = new List<int>(); 
     private int counter; 
     public event EventHandler<ItemEventArgs> ItemRemoved; 

     public List<int> Indices 
     { 
      get { return m_itemIndexes; } 
     } 

     public ListBoxControl() 
     { 
      InitializeComponent(); 

      counter = 1; 
      if (listBox1.Items.Count == 0) 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        listBox1.Items.Add("Test " + i); 
       } 
      } 
     } 

     private void listBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      int index = listBox1.IndexFromPoint(e.X, e.Y); 

      if (e.Button == System.Windows.Forms.MouseButtons.Right) 
      { 
       if (m_itemIndexes.Contains(index)) 
        return; 

       m_itemIndexes.Add(index); 
       DrawItem(index); 
       RedCounts += 1; 
      } 
      else if (e.Button == MouseButtons.Left) 
      { 
       if (!m_itemIndexes.Contains(index)) 
        return; 

       m_itemIndexes.Remove(index); 
       DrawItem(index); 
       OnItemRemoved(index, listBox1.Items[index].ToString()); 
      } 
      listBox1.SelectedIndex = index; 
     } 

     protected virtual void OnItemRemoved(int indx, string name) 
     { 
      EventHandler<ItemEventArgs> handler = ItemRemoved; 

      if(handler != null) 
       ItemRemoved(this, new ItemEventArgs() { Index = indx, Name = name}); 
     } 

     private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
     { 
      m_MyListColor = MyListColor; 
      if (m_MyListColor.IsEmpty == true) 
      { 
       m_MyListColor = Color.Red; 
      } 

      bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; 

      if (m_itemIndexes.Contains(e.Index)) 
      { 
       using (var brush = new SolidBrush(m_MyListColor)) 
       { 
        e.Graphics.FillRectangle(brush, e.Bounds); 
       } 
      } 
      else 
      { 
       e.DrawBackground(); 
      } 

      string item = listBox1.Items[e.Index].ToString(); 
      e.Graphics.DrawString(item, e.Font, selected || m_itemIndexes.Contains(e.Index) ? Brushes.White : Brushes.Black, e.Bounds, StringFormat.GenericDefault); 

      if (selected) 
       e.DrawFocusRectangle(); 
     } 

     private void DrawItem(int index) 
     { 
      Rectangle rectItem = listBox1.GetItemRectangle(index); 
      listBox1.Invalidate(rectItem); 
     } 

     [Browsable(true)] 
     public Color MyListColor 
     { 
      get { return m_MyListColor; } 
      set 
      { 
       m_MyListColor = value; 
       Refresh(); 
      } 
     } 

     [Browsable(true)] 
     public ListBox MyListBox 
     { 
      get { return listBox1; } 
      set 
      { 
       listBox1 = value; 
       Refresh(); 
      } 
     } 

     [Browsable(true)] 
     public int RedCounts 
     { 
      get { return counter; } 
      set 
      { 
       counter = value; 
       Refresh(); 
      } 
     } 

     private void ListBoxControl_Load(object sender, EventArgs e) 
     { 
      this.listBox1.SelectedIndex = 0; 
     } 
    } 

    public class ItemEventArgs : EventArgs 
    { 
     public int Index { get; set; } 
     public string Name { get; set; } 
    } 
} 
+1

看不到您正在調用'redcounts'來更新計數器,但... – bonCodigo

+0

你確定'計數器'沒有在兩個地方定義? 'lb1'和'listBox1'之間的關係是什麼? –

+0

我只是在這裏猜測,但'ListBoxControl'是一個UserControl,它包含一個ListBox,'listBox1'。 ...對? –

回答

2

有沒有奇蹟,用鼠標右鍵單擊counter在代碼的某個地方 - > 「查找所有引用」。如果在用0初始化結果時無法立即看到結果,請在每次出現時設置一箇中斷點,然後您會很快找到它

-4

你的計數器變量始終是零,因爲每一個類被調用時,所有非靜態對象將被再次實例化。您需要將您的計數器變量聲明爲static,例如`static int counter = 0;

`

+4

nonononono ...做**不**嘗試「修復」我的工作,使一個領域靜態。這與問題無關。 –

+0

我現在發現,如果我將變量計數器設置爲公共靜態,並且在form1中沒有像以前那樣聲明lb1並且沒有像之前那樣使用屬性,那麼它就可以工作。這不是一個解決方案,因爲我不知道爲什麼它不適用於該屬性。但如果即時通訊使用它作爲公共靜態它的工作。問題仍然存在,爲什麼它是0,如果即時通訊使用它。 – user1196715

+0

嘗試添加RedCounts = counter;在計數器+ = 1以下; –

相關問題