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

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.