Get position of control

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Hi All!

Is it possible to get the position of a control such as a Panel at runtime?
Or, is it possible to get the mouse position (X,Y) relative to the X,Y of a
control. For example the upper left is 0,0 instead of it's screen position.

Thanks,
Joe
 
Hi Joe,

My name is Allen Chen. It's my pleasure to work with you on this issue.

From your description you want to get the relative mouse position to an
element on the page. If so we can use JavaScript to get the position and
minus the position of the element to get the result we need. Below is the
demo that demonstrates how to do that:

Aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Javascript_Mouse_Position_lvl_200._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function ShowPosition(obj, e,id) {
var e = (typeof event != 'undefined') ? window.event : e; // IE
: Moz
var relativeto = document.getElementById(id);
var relativex = getX(relativeto);
var relativey = getY(relativeto);
var s;
if (e.clientX) {
s = (e.clientX - relativex) + "X" + (e.clientY - relativey);
}
else {
var p = e.currentTarget.clientLeft;
s = (p.clientLeft - relativex) + "X" + (p.clientTop -
relativey);
}
document.getElementById("Text").innerHTML = s;
}
function getY(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}
function getX(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div >

<table>
<tr>
<td colspan="2">
occupy some room..</td></tr>
<tr>
<td>occupy some room..</td>
<td>
<asp:Panel Width="200" Height="200" BackColor="Yellow" ID="Panel1"
runat="server">
</asp:Panel>
</td>
</tr>
</table>
<div id="Text"></div>
</div>
</form>
</body>
</html>


Aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Javascript_Mouse_Position_lvl_200
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

this.Panel1.Attributes.Add("onmousemove","ShowPosition(this,event,'Panel1')"
);
}
}
}


To test, please move your mouse over the yellow Panel. You'll see the
position shown on the page.

Please let me know if it works and feel free to ask if you have additional
questions.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Joe,

Please remove the following code. I forgot to clear it before posting.

else {
var p = e.currentTarget.clientLeft;
s = (p.clientLeft - relativex) + "X" + (p.clientTop -
relativey);
}

Regards,
Allen Chen
Microsoft Online Community Support
 
Hi Allen,

This doesn't completely work. The top left gives me X=2, Y=-21. I'm using
VS.NET 2008 and IE 6.

Thanks,
Joe
 
Hi Joe,

Thanks for your update. I tested the code in IE 6.0.3790.3959, IE 7 and
FireFox 3. The Javascript works for all of them (though there has slight
difference when the mouse moves cross the bouondary of the panel, which is
due to different ways in which IE and FireFox catch mouse ove event). Could
you double check if your code is the same as mine? If it is please tell me
your IE version. I'll test it on my side.

Regards,
Allen Chen
Microsoft Online Community Support
 
Hi Joe,

Have you solved this issue?

Regards,
Allen Chen
Microsoft Online Community Support
 
Hi Allen,

The problem I'm seeing shows up when the browser is no maximized. I'm using
IE 6.

-Joe
 
Hi Joe,

Thanks for your information. Now I see what the problem is. The scroll
position is not added in my code. So if the scroll bar moves the result is
incorrect. I've updated my code. See below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function ShowPosition(obj, e,id) {
var isie=typeof event != 'undefined';
var e = isie ? window.event : e; // IE : Moz
var relativeto = document.getElementById(id);
var relativex = getX(relativeto);
var relativey = getY(relativeto);
var s;

if (!isie) {
//Moz
s = (e.clientX - relativex + window.scrollX) + "X" +
(e.clientY - relativey + window.scrollY);
}
else {
//IE
s = (e.clientX - relativex +
document.documentElement.scrollLeft) + "X" + (e.clientY - relativey +
document.documentElement.scrollTop);
}
document.getElementById("Text").innerHTML ="_________" + s;

}
function getY(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}
function getX(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div >
<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />
<table>
<tr>
<td colspan="2">
occupy some room..</td></tr>
<tr>
<td>occupy some room..</td>
<td>
<asp:Panel Width="200" Height="200" BackColor="Yellow" ID="Panel1"
runat="server">
</asp:Panel>
</td>
</tr>
</table>

<div id="Text"></div>
<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />


</div>
</form>
</body>
</html>

Please try it to see if it works this time.

Regards,
Allen Chen
Microsoft Online Support
 
You're welcome Joe.

Have a nice day!

Regards,
Allen Chen
Microsoft Online Community Support



=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
=================================================
 
Back
Top