2013-01-02 22 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以下; –

相关问题