This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Window. Location Changed Event

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Occurs when the window's location changes.

A window's location changes when:

A user moves a window by dragging it with the window's title bar.

A window is moved after DragMove is called.

Either the Left or Top property is set programmatically.

The Move menu item of a window's System menu is chosen.

The WindowState property is changed.

Additional resources

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Which event captures position change of a Window?

I need some event like the SizeChanged on MainWindow that is activated when the user has changed the position of a window?

Many Thanks,

H.B.'s user avatar

5 Answers 5

I think it is the LocationChanged event .

Ben Robinson's user avatar

LocationChanged?

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.locationchanged.aspx

GrandMasterFlush's user avatar

I believe that .LocationChanged is the event you are looking for.

Stewbob's user avatar

How to Find an Event - A Visual Studio Adventure

Step1

You can even try RegionChanged event which is similar to LocationChanged

Panduranga Rao Sadhu's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c# .net wpf events window or ask your own question .

Hot Network Questions

window location change event wpf

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

window location change event wpf

window location change event wpf

window location change event wpf

Window Location in WPF

window location change event wpf

window location change event wpf

Introduction

This is a simple application that shows how we can change window location in WPF. This robot follows your cursor forever.

Using the Code

In a few steps, we can create this application. First I created a window in WPF - I've done it with Expression Blend 2 SP1.

Then we can access the window location with these properties:

I've added some methods to use cursor position for changing the window location:

That's all.

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Comments and Discussions

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

window location change event wpf

WPF | Help Topics

Handle window state changing events.

There are two events associated when the dialog window’s state changes. These events have the suffix “ing” or “ed” appended to them, reflecting the order in which they occur. The window state changing events allow you to perform some custom business logic before or after the window’s state is changed.

WindowStateChanging - The moment right before the dialog window control’s state changes, the WindowStateChanging event fires. You can cancel this event if certain conditions are not met.

WindowStateChanged -This event fires after the dialog window control’s state is changed.

The following code demonstrates how to handle the WindowStateChanging and WindowStateChanged events.

In Visual Basic:

Related Topics

Custom Cursors

Customize Modal Dialog Window Background

Customize the Window Header Icon

Handle Moving Events

Keyboard Settings

Modal and Modeless Dialog Windows

Position Minimized Dialog Window

Set the Dialog Window’s Height and Width When Minimized

Start Up Position of xamDialogWindow

The xamDialogWindow Control’s Behavior

Window Header Controls

# window.location Cheatsheet

Looking for a site's URL information, then the window.location object is for you! Use its properties to get information on the current page address or use its methods to do some page redirect or refresh 💫

https://www.samanthaming.com/tidbits/?filter=JS#2

# window.location Properties

# difference between host vs hostname.

In my above example, you will notice that host and hostname returns the value. So why do these properties. Well, it has do with the port number. Let's take a look.

URL without Port

https://www.samanthaming.com

URL with Port

https://www.samanthaming.com:8080

So host will include the port number, whereas hostname will only return the host name.

# How to change URL properties

Not only can you call these location properties to retrieve the URL information. You can use it to set new properties and change the URL. Let's see what I mean.

Here's the complete list of properties that you can change:

The only property you can't set is window.location.origin . This property is read-only.

# Location Object

The window.location returns a Location object. Which gives you information about the current location of the page. But you can also access the Location object in several ways.

The reason we can do this is because these are global variables in our browser.

window location change event wpf

# window.location vs location

All 4 of these properties point at the same Location object. I personally prefer window.location and would actually avoid using location . Mainly because location reads more like a generic term and someone might accidentally name their variable that, which would override the global variable. Take for example:

I think that most developer is aware that window is a global variable. So you're less likely to cause confusion. To be honest, I had no idea location was a global variable until I wrote this post 😅. So my recommendation is to be more explicit and use window.location instead 👍

Here's my personal order of preference:

Of course, this is just my preference. You're the expert of your codebase, there is no best way, the best way is always the one that works best for you and your team 🤓

# window.location Methods

# window.location.tostring.

This method returns the USVString of the URL. It is a read-only version of Location.href

In other words, you can use it to get the href value from the

on this 😊. But I did find a performance test on the difference.

JSPerf: Location toString vs Location href

One thing I want to note about these speed tests is that it is browser specific. Different browser and versions will render different outcome. I'm using Chrome, so the href came out faster then the rest. So that's one I'll use. Also I think it reads more explicit then toString() . It is very obvious that href will provide the URL whereas toString seems like something it being converted to a string 😅

# assign vs replace

Both of these methods will help you redirect or navigate to another URL. The difference is assign will save your current page in history, so your user can use the "back" button to navigate to it. Whereas with replace method, it doesn't save it. Confused? No problem, I was too. Let's walk through an example.

Current Page

I just need to emphasize the "current page" in the definition. It is the page right before you call assign or replace .

# How to Do a Page Redirect

By now, you know we can change the properties of the window.location by assigning a value using = . Similarly, there are methods we can access to do some actions. So in regards to "how to redirect to another page", well there are 3 ways.

# replace vs assign vs href

All three does redirect, the difference has to do with browser history. href and assign are the same here. It will save your current page in history, whereas replace won't. So if you prefer creating an experience where the navigation can't press back to the originating page, then use replace 👍

So the question now is href vs assign . I guess this will come to personal preference. I like the assign better because it's a method so it feels like I'm performing some action. Also there's an added bonus of it being easier to test. I've been writing a lot of Jest tests, so by using a method, it makes it way easier to mock.

But for that that are rooting for href to do a page redirect. I found a performance test and running in my version of Chrome, it was faster. Again performance test ranges with browser and different versions, it may be faster now, but perhaps in future browsers, the places might be swapped.

JSPerf: href vs assign

# Scratch your own itch 👍

Okay, a bit of a tangent and give you a glimpse of how this cheatsheet came to be. I was googling how to redirect to another page and encountered the window.location object. Sometimes I feel a developer is a journalist or detective - there's a lot of digging and combing through multiple sources for you to gather all the information available. Honestly, I was overwhelmed with the materials out there, they all covered different pieces, but I just wanted a single source. I couldn't find much, so I thought, I'll cover this in a tidbit cheatsheet! Scratch your own itch I always say 👍

# Community Input

: This is awesome, I’ve used window.location.href in the past, but didn’t realise how simple it is to access sections of the URL!

If you want to see a live-action of what James is talking about, check out the table of content at the top of this article. Click on it and it will scroll down to the specific section of the page.

# Resources

Related Tidbits

Console.table to display data, colorful console message, window.location cheatsheet, fresh tidbits.

Code snippet on HTML abbr Tag

HTML abbr Tag

Code snippet on How to Pad a String with padStart and padEnd in JavaScript

How to Pad a String with padStart and padEnd in JavaScript

Code snippet on Avoid Empty Class in Vue with Null

Avoid Empty Class in Vue with Null

Code snippet on Fix Text Overlap with CSS white-space

Fix Text Overlap with CSS white-space

Code snippet on How to Check if Object is Empty in JavaScript

How to Check if Object is Empty in JavaScript

JS Reference

Html events, html objects, other references, window parent.

Change the background-color of the parent:

Definition and Usage

The parent property returns the parent window (of the current window).

The parent property is read-only.

The parent property is not the same as the top property.

window.parent returns the immediate parent of a window.

window.top returns the topmost window in the hierarchy of windows.

The top Property

Return Value

More examples.

The location of the parent window:

Browser Support

window.parent is supported in all browsers:

Get started with your own server with Dynamic Spaces

COLOR PICKER

colorpicker

Get your certification today!

window location change event wpf

Get certified by completing a course today!

Subscribe

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Your Suggestion:

Thank you for helping us.

Your message has been sent to W3Schools.

Top Tutorials

Top references, top examples, web certificates, get certified.

window location change event wpf

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window.Left and Window.Top are broken when usign PerMonitorV2 #4127

@todor-dk

todor-dk commented Feb 3, 2021

@todor-dk

daveorourke commented Mar 4, 2021 • edited

Sorry, something went wrong.

@FoxPride

Perpete commented Nov 21, 2021 • edited

@Perpete

markoweb2 commented Oct 4, 2022

@MichaeIDietrich

MichaeIDietrich commented Oct 5, 2022

@Sandler

Sandler commented Jan 9, 2023

@dt200r

dt200r commented Feb 22, 2023 • edited

@lindexi

lindexi commented Feb 23, 2023

Dt200r commented feb 23, 2023, daveorourke commented feb 24, 2023 • edited.

dt200r commented Feb 24, 2023 • edited

No branches or pull requests

@MichaeIDietrich

We have closed this ticket because another page addresses its subject:

Answers approved by DevExpress Support

0 'other ' : '') + 'answers '">, recently viewed tickets.

problem with window.location after using hyperlink from template

Hello,  I am very new at html and javascript so basic reponses would be better. Using this template location in a .html file   <ul data-role="listview" data-source="app.data" data-template="LED-template"></ul>    <script id="LED-template" type="text/x-kendo-template">     <a href="page/LEDlist/details.html?##id=#: LEDPartNumber #">                <div class="Manufacturer">#: LEDManufacturer #</div>             <div class="Partnumber">#: LEDPartNumber #</div>         </a>     </script>  gives me a list of all the data in app.data. Once any of the links are clicked this is executed   var location = window.location.toString();  var partNumber = location.substring(location.lastIndexOf('?') + 4); it is able to get the partnumber which I can use to filter the total data to find the right data and create a page based on the information. The problem I am having is that after going back once and choosing a second partnumber the above code fails to return a valid reponse. I've attached the console that is outputing  location and the partNumber 

Thank you for any help!

7 Answers , 1 is accepted

window location change event wpf

Upon further investigation, it seems like window.location does not return the right value the second time the page is accessed. However, it works fine the first time.

The second (or third, etc) time the page is accessed, window.location returns the previous page, not the current one.

See attached picture.

Would you know what could cause this?

Hello again,here isthe base of my project. what I am sending you is able to run in the appbuilder simulator in the CLI with the error. to get the error, choose any LED and you will see the page with data, then press back and choose another the info on the next page will be wrong showing the first page again since the location is returning the wrong info. 

[Attachment removed, in accordance with customer request.]

Syncfusion Blogs

How do you detect navigation events in Blazor WebAssembly?

LocationChangedis an event handler that will fire when the navigation location has been changed. The following example uses a JavaScript interop function to alert the user when the navigation location changes.

Refer to this documentation for more details.

Related FAQs

Blazor Ad Image

Couldn't find the FAQs you're looking for?

Platform Blazor ASP.NET WinForms WPF Angular React

Answer (Optional)

Email (Optional)

Email address is only for further clarification on your FAQ request. It will not be used for any other purpose.

Please leave this field empty.

Up arrow icon

Copyright © 2001 - 2023 Syncfusion Inc. All Rights Reserved

Fax: +1 919.573.0306, us: +1 919.481.1974, uk: +44 20 7084 6215, toll free: 1-888-9dotnet.

This web site uses cookies. By using the site you accept the cookie policy . This message is for compliance with the UK ICO law .

WPF Window Start-Up Locations

WPF windows can initially be displayed using three positioning options. The location of a window can be set manually, or the new window can be centred on the screen or in relation to its owner.

WindowStartupLocation

In the Windows Presentation Foundation Fundamentals tutorial , I described many properties of the Window class, which controls the display of windows and dialog boxes. The articles described how you can position a window manually using the Left and Top properties , which set the window's location relative to the top-left corner of the screen.

In many cases, you will want to centralise a window, either on the screen or relative to its owner. You could do this by obtaining the resolution of the screen, or the location and size of the parent window, calculating the correct location and setting the Left and Top properties. However, you can perform this type of positioning automatically by setting the WindowStartupLocation property to a constant from the WindowStartupLocation enumeration .

To demonstrate, create a new WPF application project in Visual Studio . Name the project, "WindowStartupLocationDemo". Once the solution is prepared, replace the XAML in the main window with the code below:

The above XAML creates a window with two buttons , each with its Click event registered. Switch to the code behind the window and ensure that the empty Click methods appear as follows:

Centring Windows

A common option for a newly loaded window is to centralise it on the screen. This can help to guide the user's attention to important items. Centralising on the screen is generally used when the window has no obvious owner or parent.

To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen . You can do this using the XAML or in code before the window is displayed.

Let's configure the "Centre Screen" button to load a new instance of the main window in the middle of the screen. To do so, update the button's Click method, as follows:

Run the program and click the button to see the result.

Centring in an Owning Window

When you display a child window from an existing window, it can be useful to centralise the child relative to its parent. As the user should be looking at the parent, their attention should be quickly drawn to the child.

To centre a window in this way, the new window's Owner property should be set to the parent window's object. You also set the WindowStartupLocation property to CenterOwner .

To demonstrate, update the second click method, as shown below. Note that after the MainWindow object is created, the Owner is set and the start-up location option is selected. The child window is slightly smaller than its parent, as if it was the same size, it would obscure the original window.

Run the program again to try the new code.

IMAGES

  1. Dialog Window Component

    window location change event wpf

  2. c#

    window location change event wpf

  3. c#

    window location change event wpf

  4. Open a new WPF Window on button click

    window location change event wpf

  5. Difference between window.location.href, window.location.replace and window.location.assign in

    window location change event wpf

  6. rofi: Change Window Location

    window location change event wpf

VIDEO

  1. Piège De La Danse

  2. Qualifying 2022 1/5 Scale Worlds

  3. Kakashi Speech || Meets his Father

  4. 🤯‼️என்னது இந்த கடையில Saree Free

  5. Keti ko -Unnchai || Kirti and Anjali || Dance cover || #dgirlsvlogger

  6. 3d printer color change CRAZYYYYY #3dprinting #viral #subscribe #youtubeshorts #3dprintingservice

COMMENTS

  1. Window.LocationChanged Event (System.Windows)

    Remarks. A window's location changes when: A user moves a window by dragging it with the window's title bar. A window is moved after DragMove is called.

  2. Which event captures position change of a Window?

    Any ideas? Many Thanks,. c# .net · wpf · events · window.

  3. Window Location in WPF

    This is a simple application that shows how we can change window location in WPF. This robot follows your cursor forever.

  4. Handle Window State Changing Events

    There are two events associated when the dialog window's state changes. These events have the suffix “ing” or “ed” appended to them, reflecting the order in

  5. window.location Cheatsheet

    The only property you can't set is window.location.origin . This property is read-only. # Location Object. The window.location returns a Location object

  6. Window parent Property

    window.parent returns the immediate parent of a window. ... The location of the parent window:.

  7. Window.Left and Window.Top are broken when usign PerMonitorV2

    A window can be freely moved between the available monitors and WPF automatically changes scaling and other parameters.

  8. Calling the window.Location.reload() method triggers a button click

    Issue is , When we click on button 'Push' it is triggering the OK button click event of the popup. TestApp.zip.

  9. problem with window.location after using hyperlink from template

    The Router instance has a change event that provides access to the view parameters. The event can be attached via bind():

  10. How do you detect navigation events in Blazor WebAssembly?

    LocationChangedis an event handler that will fire when the navigation location has been changed. The following example uses a JavaScript interop

  11. WPF Window Start-Up Locations

    WPF windows can initially be displayed using three positioning options. The location of a window can be set manually, or the new window can