Solved

Change the location of control at runtime in WPF application.

Posted on 2008-06-17
4
5,070 Views
1 Endorsement
Last Modified: 2013-12-16
How to change the location (left and top) of the image control in wpf windows application at runtime?
1
Comment
Question by:Mr_Javed
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
BitRunner303 earned 50 total points
Comment Utility
Not very intuitively...  here I've got a form with a button and a textbox both sitting inside a Canvas layout object, I'll use the Click event of the button to change the location of the textbox.  Basically this works by getting the existing X/Y of the textbox, then I'll offset it by a +/- amouhnt in transX/Y.  The values I'm setting are the "attached properties" for it that come from it being inside a Canvas container.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int transX = 50;
            int transY = 20;

            GeneralTransform myTrans = this.textBox1.TransformToAncestor(this);
            Point p1 = myTrans.Transform(new Point(0, 0));

            this.textBox1.SetValue(Canvas.LeftProperty, p1.X + transX);
            this.textBox1.SetValue(Canvas.TopProperty, p1.Y + transY);

        }
1
 

Author Comment

by:Mr_Javed
Comment Utility
Thanks for the reply.

When I created the sample like yours. in VB

==============================================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        TextBox1.SetValue(Canvas.LeftProperty, 10)
    End Sub
==============================================================

This give me the error : '10' is not a valid value for property 'Left'.
When I changed the value 10 to any other, the same error I'm getting.

What can be the problem?




0
 

Author Comment

by:Mr_Javed
Comment Utility
That error is solved now. that was because I pass 10, that is integer value and it requires double value.

your solution works. Thanks BitRunner303

0
 

Author Closing Comment

by:Mr_Javed
Comment Utility
Thanks a lot
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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

9 Experts available now in Live!

Get 1:1 Help Now