在用戶控制代碼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; }
}
}
看不到您正在調用'redcounts'來更新計數器,但... – bonCodigo
你確定'計數器'沒有在兩個地方定義? 'lb1'和'listBox1'之間的關係是什麼? –
我只是在這裏猜測,但'ListBoxControl'是一個UserControl,它包含一個ListBox,'listBox1'。 ...對? –