473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Get mouse position in control.

I have an application that has a vb.Net client and an ASP.Net page. In the
vb.Net form I use Panel1.PointToClient(Windows.Forms.Cursor.Position ) to get
the position on a panel where the mouse was clicked. I would like to do the
same thing in my ASP.Net page, but can't find a way to. Any ideas???
Mar 30 '06 #1
4 6645
You need to handle the onmousemove and onclick event of your panel in
JavaScript.

I've attached an example in C# (should be easy to convert to VB.net).
the getPanelMouseCoOrds JavasScript sets two text boxes (so you can see
it in action) and two hidden fields with the mouse postion.

The onclick handler just submits the form.

When the form is posted back to server the PageLoad extracts the values
from the two hidden form fields and sets the label for info

It works in IE but may ntyo work in other browsers:
Webform1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PanelMousePosition.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="hidX" name="hidX" type="hidden"> <INPUT id="hidY"
name="hidY" type="hidden">
<asp:Panel id="Panel1" runat="server" Width="423px" Height="94px"
BorderStyle="Solid">Panel</asp:Panel>
<p>X: <input name="xPos" type="text" size="5"></p>
<p>Y: <input name="yPos" type="text" size="5"></p>
<asp:Label id="Label1" runat="server"></asp:Label>
<script language="JavaScript">
function getPanelMouseCoOrds()
{
var mouseX = event.clientX + document.body.scrollLeft;
var mouseY = event.clientY + document.body.scrollTop;

document.Form1.xPos.value = mouseX;
document.Form1.yPos.value = mouseY;

document.Form1.hidX.value = mouseX;
document.Form1.hidY.value = mouseY;
return true;
}

function handlePanelClick()
{
document.Form1.submit();
}
</script>
</form>
</body>
</HTML>

and there's the code behind to register and handle events
(WebForm1.aspx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PanelMousePosition
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Panel1.Attributes["onmousemove"]="getPanelMouseCoOrds()";
Panel1.Attributes["onclick"]="handlePanelClick()";
}
else
{
int panelX = Int32.Parse(Request.Form["hidX"]);
int panelY = Int32.Parse(Request.Form["hidY"]);

Label1.Text = "You clicked @ " + panelX + ", " + panelY;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Mar 30 '06 #2
Thanks, I will look it over.

"Jason Hales" <ja*********@yahoo.com> wrote in message
news:11********************@j33g2000cwa.googlegrou ps.com...
You need to handle the onmousemove and onclick event of your panel in
JavaScript.

I've attached an example in C# (should be easy to convert to VB.net).
the getPanelMouseCoOrds JavasScript sets two text boxes (so you can see
it in action) and two hidden fields with the mouse postion.

The onclick handler just submits the form.

When the form is posted back to server the PageLoad extracts the values
from the two hidden form fields and sets the label for info

It works in IE but may ntyo work in other browsers:
Webform1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PanelMousePosition.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="hidX" name="hidX" type="hidden"> <INPUT id="hidY"
name="hidY" type="hidden">
<asp:Panel id="Panel1" runat="server" Width="423px" Height="94px"
BorderStyle="Solid">Panel</asp:Panel>
<p>X: <input name="xPos" type="text" size="5"></p>
<p>Y: <input name="yPos" type="text" size="5"></p>
<asp:Label id="Label1" runat="server"></asp:Label>
<script language="JavaScript">
function getPanelMouseCoOrds()
{
var mouseX = event.clientX + document.body.scrollLeft;
var mouseY = event.clientY + document.body.scrollTop;

document.Form1.xPos.value = mouseX;
document.Form1.yPos.value = mouseY;

document.Form1.hidX.value = mouseX;
document.Form1.hidY.value = mouseY;
return true;
}

function handlePanelClick()
{
document.Form1.submit();
}
</script>
</form>
</body>
</HTML>

and there's the code behind to register and handle events
(WebForm1.aspx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PanelMousePosition
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Panel1.Attributes["onmousemove"]="getPanelMouseCoOrds()";
Panel1.Attributes["onclick"]="handlePanelClick()";
}
else
{
int panelX = Int32.Parse(Request.Form["hidX"]);
int panelY = Int32.Parse(Request.Form["hidY"]);

Label1.Text = "You clicked @ " + panelX + ", " + panelY;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Mar 30 '06 #3
Any idea why I am getting "Input string was not in a correct format." When
it gets to "panelX As Integer = Int32.Parse(Request.Form("hidX"))"?

"Shawn" <ss******@bellsouth.net> wrote in message
news:us****************@TK2MSFTNGP14.phx.gbl...
Thanks, I will look it over.

"Jason Hales" <ja*********@yahoo.com> wrote in message
news:11********************@j33g2000cwa.googlegrou ps.com...
You need to handle the onmousemove and onclick event of your panel in
JavaScript.

I've attached an example in C# (should be easy to convert to VB.net).
the getPanelMouseCoOrds JavasScript sets two text boxes (so you can see
it in action) and two hidden fields with the mouse postion.

The onclick handler just submits the form.

When the form is posted back to server the PageLoad extracts the values
from the two hidden form fields and sets the label for info

It works in IE but may ntyo work in other browsers:
Webform1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PanelMousePosition.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="hidX" name="hidX" type="hidden"> <INPUT id="hidY"
name="hidY" type="hidden">
<asp:Panel id="Panel1" runat="server" Width="423px" Height="94px"
BorderStyle="Solid">Panel</asp:Panel>
<p>X: <input name="xPos" type="text" size="5"></p>
<p>Y: <input name="yPos" type="text" size="5"></p>
<asp:Label id="Label1" runat="server"></asp:Label>
<script language="JavaScript">
function getPanelMouseCoOrds()
{
var mouseX = event.clientX + document.body.scrollLeft;
var mouseY = event.clientY + document.body.scrollTop;

document.Form1.xPos.value = mouseX;
document.Form1.yPos.value = mouseY;

document.Form1.hidX.value = mouseX;
document.Form1.hidY.value = mouseY;
return true;
}

function handlePanelClick()
{
document.Form1.submit();
}
</script>
</form>
</body>
</HTML>

and there's the code behind to register and handle events
(WebForm1.aspx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PanelMousePosition
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Panel1.Attributes["onmousemove"]="getPanelMouseCoOrds()";
Panel1.Attributes["onclick"]="handlePanelClick()";
}
else
{
int panelX = Int32.Parse(Request.Form["hidX"]);
int panelY = Int32.Parse(Request.Form["hidY"]);

Label1.Text = "You clicked @ " + panelX + ", " + panelY;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


Mar 30 '06 #4
Shawn, just to repeat my emailed reply, it seems as though the
getPanelMouseCoOrds may not be setting the hidden fields hidX and hidY.
The parse error is because they are null or empty

Can you confirm that the text fields are being set when you move over
the panel?

Are you running IE and if so which version?

Mar 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen Williams | last post by:
I am migrating a VB 6 PictureBox control to VB.NET. In VB 6, this control modified its border style during the mouse down and up events to provide user feedback that it was selected. Once...
2
by: Mojtaba Faridzad | last post by:
Hi, by Control.MousePosition we can find the mouse position. but how we can set the mouse position? thanks
3
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
13
by: Lars Netzel | last post by:
Hi! I have a round area which I want to be able to move the mouse over and fire off events... how do I do that? I have drawn a FillPie Graphics and I feel that there has to be a way of...
2
by: Sam | last post by:
Hi, I can't figure out how to detect when my mouse cursor leaves a panel control. It should not trigger the event (or do anything) when the mouse leave the panel but still is over a control that...
1
by: ohadasor | last post by:
Hello, I have a control which I need to act in a specific way when the user drags the mouse on it. Means, that the user should click on the control's one position, hold the mouse button, move it...
1
by: Emanuel | last post by:
I am trying to make a little diagramming app in C# with the 1.1 Framework (currently our standard here at work, will be upgrading to 2005 sometime in the next couple of months). I want to allow...
5
by: Zaxxon21 | last post by:
I'm basically trying to implement a simple drop down menu list for a button that I have. When the user hovers over the button, I want a list of button options to appear below the button. If the...
2
by: markszlazak | last post by:
In the following script, a control displays (black box) in each table cell once you mouse over the cell. Mouse down on the control to change the mode of the table. Drag the mouse over cells in the...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.