Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Reader,

Hi
please help me with this.

i wrote code for button click

C#
Thumb addthumb = new Thumb();
                 addthumb.Height = 20;
                 addthumb.Width = lenght_[i]+25;
                 addthumb.Name = "L" + i.ToString() + "P" + i.ToString();
                 addthumb.Background = System.Windows.Media.Brushes.Orange;
                 addthumb.DragDelta += new DragDeltaEventHandler(myThumb_DragDelta);
                 Canvas.SetLeft(addthumb, position_array[balls]+25);
                 Canvas.SetTop(addthumb, 20);
                 canvas_container.Children.Add(addthumb);

canvas_container is name of my canvas.
and event
C#
private void myThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
       {
           FrameworkElement element = sender as FrameworkElement;
           Canvas.SetLeft(element, Canvas.GetLeft(element) + e.HorizontalChange);
           lbl_Heightshow.Content = element.Height;
           lbl_widthshow.Content = element.Width;
           lbl_Slub_name.Content = element.Name;
           lbl_leftposition.Content = Canvas.GetLeft(element).ToString();
           if (Canvas.GetLeft(element) == 0)
           {
               lbl_leftposition.Content = "0";
           }
           else if (Canvas.GetLeft(element) < 0)
           {
               lbl_leftposition.Content = "0";
               Canvas.SetLeft(element, 0);
           }
           else if (Canvas.GetLeft(element) + element.Width > canvas_container.ActualWidth)
           {
               lbl_leftposition.Content = (canvas_container.ActualWidth - element.Width).ToString();
               Canvas.SetLeft(element, canvas_container.ActualWidth - element.Width);
           }



now everything is fine till here.
what i want is the generated controls should not over lap on each other like they are not moving out of canvas.

please assist.
Posted

1 solution

I presume that you are allowing the user to move the items. What you need to do is monitor the move of an item, and not allow the item to move to a location that would cause an overlapping by forcing the position (left, top) of the item to a value that would be valid. I have done this in the past by manually handling the move by monitoring the mouse position, and moving the item manually.
 
Share this answer
 
Comments
ajit_machhe 7-Mar-12 17:34pm    
thank you for looking into it... and i will do that the same..

but is there anyother way to do that ? i mean easy way..?
Clifford Nelson 7-Mar-12 17:42pm    
I do not think so. There might be something that somebody has created that will do the same. You can create a control that will do what you are looking for. This is what I probably would do. Then it is easy to reuse. I did something similar, except mine was a drawing surface. I kept controls from being dragged off the drawing surface. If the solution is helpful, vote.

Thanks

PS If I get some time in the next couple of days, I will look into creating a control
ajit_machhe 9-Apr-12 1:53am    
Hi Clifford,

(sorry for late reply)
if possible please help me with this.
i still cant stop overlapping...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900