hello frds
I want to drag and drop all label created crossponding to checklist box's checked item.
But problem is only move last created label. other give no responce.
help me..

my code is.

 public CheckedListBox chk;

        private void Form1_Load(object sender, EventArgs e)
        {
            chk = new CheckedListBox();
            chk.CheckOnClick = true;
            chk.Items.Add("A");
            chk.Items.Add("B");
            chk.Items.Add("C");
            chk.Items.Add("D");
            chk.Items.Add("E");
            chk.Items.Add("F");
       }




        static string stri;
        public Label image;
        int zx = 20;
        int zy = 20;
        Array arr;
        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            foreach (string str in chk.CheckedItems)
            {
                stri = str;
                label2.Text +=  stri+",";
            }
             arr = label2.Text.Split(',');
            for (int i = 0; i < arr.Length-1; i++)
            {
                image = new Label();
                image.Text = arr.GetValue(i).ToString();
                image.BackColor = Color.Aqua;
                image.Location = new System.Drawing.Point(zx, zy);
                this.Controls.Add(image);
                zx = zx + 20;
                zy = zy + 30;

               if (arr.GetValue(i).ToString() == image.Text)
               {
                    this.image.MouseUp += new MouseEventHandler(image_MouseUp);
                    this.image.MouseMove += new MouseEventHandler(image_MouseMove);
                    this.image.MouseDown += new MouseEventHandler(image_MouseDown);
                }
                else
                {
                  MessageBox.Show("Selected Item is Not valid");
                }                

            }



        }
        private bool isDragging1 = false;
        private int clickOffsetX1, clickOffsetY1;
        void image_MouseDown(object sender, MouseEventArgs e)
        {
            isDragging1 = true;
            clickOffsetX1 = e.X;
            clickOffsetY1 = e.Y;
        }

        void image_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging1 == true)
            {
                image.Left = e.X + image.Left - clickOffsetX1;
                image.Top = e.Y + image.Top - clickOffsetY1;

            }
        }

        void image_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging1 = false;
        }

I hope you will give me good solution

Dani AI

Generated

The problem was not the mouse math — it was using the same form-level image variable for every label. Each loop iteration overwrote that field, so every event handler ended up operating against the last-created Label. 's advice to cast the sender to Label is exactly right: event handlers should act on the control that raised the event, not on a shared field.

A more robust pattern:

  • Create a local Label for each checked item and attach handlers to it.
  • In the handlers cast sender to Label (or keep a single dragging reference set in MouseDown).
  • Avoid comparing label.Text to some external value inside the loop; attach handlers unconditionally and use label.Tag to store the checklist item or index if you need to map back later.
  • Don’t show MessageBox inside the creation loop — it interrupts flow.

Example dragging pattern (uses a single dragging reference and parent coordinate conversion for smoother behavior):

private Label dragging;
private Point offset;

void lbl_MouseDown(object sender, MouseEventArgs e)
{
    dragging = (Label)sender;
    offset = e.Location;
    dragging.Capture = true;
    dragging.BringToFront();
}

void lbl_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging == null) return;
    var p = dragging.Parent.PointToClient(Cursor.Position);
    dragging.Left = p.X - offset.X;
    dragging.Top = p.Y - offset.Y;
}

void lbl_MouseUp(object sender, MouseEventArgs e)
{
    if (dragging == null) return;
    dragging.Capture = false;
    dragging = null;
}

Extra tips: set Label.Cursor, set a fixed size or AutoSize appropriately, and store the checklist item in label.Tag. For cross-control dragging or richer UX consider Control.DoDragDrop (see Microsoft docs). Thanks to for the sender-casting suggestion — that change fixes the symptom; the above notes fix robustness and mapping back to the checklist.

Recommended Answers

All 4 Replies

Hi,

Please replace sender instead of image in MouseMove() EventHandler

void image_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging1 == true)
{
image.Left = e.X + image.Left - clickOffsetX1;
image.Top = e.Y + image.Top - clickOffsetY1;

}
}

Instead

void image_MouseMove(object sender, MouseEventArgs e)
        {
            Label image = (Label)sender ;
            if (isDragging1 == true)
            {
                image.Left = e.X + image.Left - clickOffsetX1;
                image.Top = e.Y + image.Top - clickOffsetY1;

            }
        }

hi frd,
that code did not give diffrent result from my code.
i think problem is in label not in move.

Hi,

I have just modified your code, please look at the code. U can see the small different. i used image as local variable. It was initialized by the sender object explicitly. Ok i give another way of coding

void image_MouseMove(object sender, MouseEventArgs e)
    {
        Label control;
        if (isDragging1 == true)
        {
            control = (Label)sender ;
            control.Left = e.X + control.Left - clickOffsetX1;
            control.Top = e.Y + control.Top - clickOffsetY1;
        }
    }

thanks frd
your code is working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.