Solved

Access UserControl.left and UserControl.top c#

Posted on 2009-05-12
8
591 Views
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
0
Comment
Question by:tonysebsatian
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:tculler
Comment Utility
Can you post the error message, or summarize it?
0
 

Author Comment

by:tonysebsatian
Comment Utility
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....
0
 

Author Comment

by:tonysebsatian
Comment Utility
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

0
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
Comment Utility
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:tonysebsatian
Comment Utility
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...
0
 

Author Comment

by:tonysebsatian
Comment Utility
Thanks for your help....
0
 

Author Closing Comment

by:tonysebsatian
Comment Utility
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
0
 
LVL 9

Expert Comment

by:tculler
Comment Utility
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 :)

Nate
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now