为什么变量计数器总是0

本文关键字:计数器 变量 为什么 | 更新日期: 2023-09-27 17:50:18

在用户控制代码中,我在顶部做:

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。1、2、3、4…

问题出于某种原因是在Form1当我点击deleteeselectedlightningstoolstripmenuitem_click使用断点然后lb1。RedCounts = 1出于某种原因它从属性或行counter = 1中获取值1;我不知道为什么。所以如果我set counter = 121;然后两派。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; }
    }
}

为什么变量计数器总是0

没有奇迹,右键单击代码中的counter ->"查找所有引用"。如果您不能立即看到用0初始化的结果,那么在每次出现时设置一个断点,您很快就会发现

计数器变量始终为零,因为每次调用类时,所有非静态对象都会再次实例化。您需要将计数器变量声明为静态的,例如"static int counter=0;

"

"