Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:24:51+00:00 2026-06-16T21:24:51+00:00

In the User Control code im doing in the top: int counter; In constructor:

  • 0

In the User Control code im doing in the top:

int counter;

In constructor:

counter = 0;

In the MouseDown event im doing:

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;
            }

I used a breakpoint on the counter += 1; and saw that it’s growing by one each time i click the right mouse button.

Then i add in the bottom a property for the counter:

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

Then in Form1 in the top i did:

ListBoxControl lb1;

In constructor:

lb1 = new ListBoxControl();

Then in the bottom of Form1 i added:

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
                {

                }
            }

But the result of RedCounts is all the time 0 i don’t know why.

EDIT:

I found that if im doing instead of counter = 0; doing counter = 1; and instead of counter += 1; doing RedCounts += 1; Then using a breakpoint on the RedCounts i see that counter is growing by 1 starting from 1. 1,2,3,4….

The problem for some reason is in Form1 when i click the deleteSelectedLightningsToolStripMenuItem_Click using a breakpoint then lb1.RedCounts is 1 for some reason it’s getting the value of 1 maybe from the property or from the line counter = 1; im not sure why. So if i set counter = 121; then lb1.RedCounts will show me 121. Strange.

This is the the full User Control with the counter and 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:24:52+00:00Added an answer on June 16, 2026 at 9:24 pm

    There are no miracles, right click on counter somewhere in code -> “Find all references”. If you can’t see in results immediately where you initialize it with 0, set a break point on every occurrence and you will find it soon

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a DatePicker user control (ASP code below, no code behind) which is
the following code is for user control(it display banner), the page get stuck in
Below is my entire code from a User control that contains the YUI Uploader.
I have a user control which uses objects as inner properties (some code is
I have extended a server control (not a user control) and put the code
I'm utilizing the code posted by Jesper Palm here: Make user control display outside
I have a user control which contains the following code: <form id=CurrencyForm method=post runat=server>
I have the following code in my user control: <asp:LinqDataSource ID=myLinqDataSource runat=server AutoSort=true ContextTypeName=MyDBContext
I have a user control which has several radiobuttons and buttons... I have code
I have a custom user control DatePicker.cs. Inside of another piece of code I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.