Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C#: Avoiding Overlapping Labels using getChildAtPoint()

  • 26-04-2006 03:17PM
    #1
    Registered Users, Registered Users 2 Posts: 353 ✭✭


    Edit: After each label is created and added to the display the references to that label are recycled for the next label to tbe created. Could that be the source of my problem? The only Label I have been able to manipulate using getChildAtPoint was an instance variable so does the program need a reference to the label to exist somewhere even if that reference wasn't needed to identify it?

    I'm working on a project which involves drawing up a timeline. The problem I'm having is when two events on the timeline are too close and the Label connected to the first covers that of the second.
    What I'm trying to do to get around this is shift the second Label to the right when it is created useing exceptions to locate the first. However I can't get this working and my code doesn't seem to be having any effect at all; it simply runs through to the "return new point .." every time.
    I'm very new at C# and trying to learn it by myself so it might well be I'm making a simple error or making things difficult for myself. I'ld welcome any hints and tips.
    This is the method for generating the Point for the new Label location.

    All Labels are 100 x 80.
    xVal, yVal indicate centre-top of the Label.
    private Point clearSpace( int xVal, int yVal )
    	{
    		int xValue = xVal;
    		int yValue = yVal;
                              // Try to find a Label on the left
    		Label Left = (Label)this.GetChildAtPoint( new Point( xValue - 45, yValue - 40 ) );
                              // Try to find a Label on the right.
    		Label Right = (Label)this.GetChildAtPoint( new Point( xValue + 45, yValue -40 ) );
    		Point spot = new Point( 0, 0 );
    		try
    		{
    			Left.Text = Left.Text;
    			return clearSpace( xValue + 10, yValue );
    		}
    		catch( NullReferenceException )
    		{
    			try
    			{
    				Right.Text = Right.Text;
    				return clearSpace( xValue, yValue + 10 );
    			}
    			catch( NullReferenceException )
    			{
    				return new Point( xValue - 50, yValue );
    			}
    		}
    		return spot;
    	}
    


Advertisement