Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Access UserControl.left and UserControl.top c#

Posted on 2009-05-12
Medium Priority
Last Modified: 2012-08-13
How can I access the user controls left and top properties from within the user control?
I have a WPF user control. When i create this control dynamically through code i cannot access top and left properties. Any help regarding this will be much appreciated.. thanks very much
Question by:tonysebsatian
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3

Expert Comment

ID: 24367155
Can you post the error message, or summarize it?

Author Comment

ID: 24373111
Thanks for your response...

I have a requirement where I need to create a rectangle shape when clicked a button. I created a WPF usercontrol as WinForm application dont have a rectangle or a shape control.

// When I create an instance of a rectangle object (usercontrol)
Rectangle rectangle = new Rectangle();
//and pass it to the below constructor of a class, it gives me all the properties and methods except Top and Left
 public RectangleElement(Rectangle rectangle)
            height= rectangle.Height;
            width = rectangle.Width;
            top = 0;//   cannot find rectangle.Top
            left = 0;//   cannot find rectangle.Left

Hope this give a better view of my problem...  Thanks very much once again....

Author Comment

ID: 24373314
Hello, I think I have found a way to get Top and Left  values. and it seems working...

I added the below code snippet to the Rectangle class...
And i am able to move the rectangle using top and Left values..

Thaniks very much for your time and effort....

       public double Top
            get { return Convert.ToDouble(Canvas.TopProperty); }
            set { this.SetValue(Canvas.TopProperty, value); }
        public double Left
            get { return Convert.ToDouble(Canvas.LeftProperty); }
            set { this.SetValue(Canvas.LeftProperty, value); }

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

tculler earned 1500 total points
ID: 24380719
That should work, but why doubles? Last I checked, pixels were measured with whole numbers ;). An int (Int32) is what is "usually" used, but a short (Int16) would more than likely suffice.

Anyways, as for the Top and Left stuff, I'm really not sure why that didn't work (was the Rectangle a System.Drawing.Rectangle?), but the X property should be the same as the Left, and the Y property the same as the Top.

Author Comment

ID: 24383387
Thanks for your comment..
The WPF Rectangle control has both Top and left properties.
But when I add rectangle to a WPF user control and create an instance of it  --
WPFUserControl myusercontrol = new WPFUserControl();
I cant find both Top and Left properties.  But I have resolved this by adding two peoperties, Top and Left to the UserControl as i have listed in my previous message

Yes, I am taking your advice regarding Double. I have changed it to Int(int32) as its usually used. Thanks for the advice again...

Author Comment

ID: 24383399
Thanks for your help....

Author Closing Comment

ID: 31580597
Hello, thanks very much for your time. Thought you didnt had to spend much time on this Issue,  I would like to give you all the credits as an appreciation for your time...  Thanks

Expert Comment

ID: 24387048
I apologize for not being moer help, I've been a bit busy as of late, and have been slower on getting back to questions because of it. I'm glad you've found a solution though :)


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question