Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i am adding user controls dynamically to my canvas. i want to avoid overlapping of user controls while draging. i am checking position of every control. is there any easy solution or proper way to implement this.


What I have tried:

i didnt get any solution plz help me
Posted
Updated 28-Apr-16 0:50am
v3
Comments
TABiSH777 28-Apr-16 3:47am    
Instead of Canvas,Try Grid. as in Grid You can check empty Row & Column & thereby avoid overlapping
Mahesh Alappuzha 28-Apr-16 5:40am    
i have more than 32 controls in that canvas/Grid .is this good way ?? if there is column span or row span, this logic is possible?
TABiSH777 28-Apr-16 5:58am    
Depends on the content of Each User Control You are using. if there are single textblocks or any such control in each User Control then no problem. but if there are heavy controls then your system should be capable up to it.

And Yes! the logic is possible... if there is column span & row span.. all you have to apply is the logic we apply in a matrix program to get the availaible free cells.
Mahesh Alappuzha 28-Apr-16 6:03am    
there are 3 label and 3 text box in the user control. and these user controls are created dynamically may 10 user control or 40 .

Any sample code ??
TABiSH777 28-Apr-16 6:25am    
If you have to show only strings via label then better use textblock, as Labels are considered heavier than textblocks.
Also i cant provide you complete code for it as it contains numerous line of codes for drag & drop but i can give you logic for it & availaible cells if you want.

1 solution

First Of all please create a grid of fixed no. of rows & columns so that it would be easier to get free cells.. here is the code..
C#
void TotalUsed()
        {
            TotalUsedCells = new ObservableCollection<usedcells>();

            foreach (var item in HomeItems)
            {
                for (byte i = item.RowNumber; i < (item.RowNumber + item.RowSpan); i++)
                {
                    for (byte j = item.ColumnNumber; j < (item.ColumnNumber + item.ColumnSpan); j++)
                    {
                        TotalUsedCells.Add(new UsedCells() { RowNo = i, ColumnNo = j });
                    }
                }
            }
        }
</usedcells>


Here I have Created a collection of my UsedCells which contains only 2 properties (RowNo & ColumnNo). and each time i add a new item or expand it or reduce it add or remove from this collection. HomeItems here are the list of items, in Your case you may have your user control Later to get Free Cells...

C#
for (byte aRow = 0; aRow <= TotalRows - 1; aRow++)
                    {
                        for (byte aCol = 0; aCol <= TotalColumns - 1; aCol++)
                        {
                            var aCells = _gdHelper.TotalUsedCells.Where(x => x.RowNo == aRow && x.ColumnNo == aCol).FirstOrDefault();
                            if (aCells == null)
                                freeCells.Add(new UsedCells() { RowNo = aRow, ColumnNo = aCol });
                        }
                    }


As I told We should have TotalRows & TotalColumns To calculate, here i have done it. & freeCells are just my another Collection of UsedCells.
Hence While placing any item i just...

C#
if (freeCells == null || freeCells.Count == 0)
                        throw new Exception("THERE IS NO FREE SPACE AVAILAIBLE TO ADD ITEMS");

                    if (homeList.Count > freeCells.Count)
                        throw new Exception(string.Format("THERE IS NO FREE SPACE AVAILAIBLE, PLEASE REDUCE THE SELECTIONS TO {0}", freeCells.Count));

                    foreach (var h in homeList)
                    {
                        var hItem = RootHomeScreenItems.FirstOrDefault(x => x.ID == h);
                        var fCells = freeCells.FirstOrDefault();
                        hItem.RowNumber = fCells.RowNo;
                        hItem.ColumnNumber = fCells.ColumnNo;
                        freeCells.Remove(fCells);
                        _gdHelper.AddUsed(hItem);
                        hCollection.Add(hItem);
                    }


The AddUsed Method here Just Adds in the TotalUsedCells.

Hope You get help From This...
 
Share this answer
 
Comments
Mahesh Alappuzha 28-Apr-16 7:30am    
thank you TABiSH77 for helping me
TABiSH777 28-Apr-16 8:24am    
Most Welcome :)

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