473,811 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PointToC lient(Windows.F orms.Cursor.Pos ition) 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 6671
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 getPanelMouseCo Ords 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="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="Panel MousePosition.W ebForm1" %>
<!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="So lid">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="JavaS cript">
function getPanelMouseCo Ords()
{
var mouseX = event.clientX + document.body.s crollLeft;
var mouseY = event.clientY + document.body.s crollTop;

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

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

function handlePanelClic k()
{
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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace PanelMousePosit ion
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Pane l Panel1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (! Page.IsPostBack )
{
Panel1.Attribut es["onmousemov e"]="getPanelMouse CoOrds()";
Panel1.Attribut es["onclick"]="handlePanelCl ick()";
}
else
{
int panelX = Int32.Parse(Req uest.Form["hidX"]);
int panelY = Int32.Parse(Req uest.Form["hidY"]);

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

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

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

}
#endregion
}
}

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

"Jason Hales" <ja*********@ya hoo.com> wrote in message
news:11******** ************@j3 3g2000cwa.googl egroups.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 getPanelMouseCo Ords 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="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="Panel MousePosition.W ebForm1" %>
<!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="So lid">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="JavaS cript">
function getPanelMouseCo Ords()
{
var mouseX = event.clientX + document.body.s crollLeft;
var mouseY = event.clientY + document.body.s crollTop;

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

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

function handlePanelClic k()
{
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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace PanelMousePosit ion
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Pane l Panel1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (! Page.IsPostBack )
{
Panel1.Attribut es["onmousemov e"]="getPanelMouse CoOrds()";
Panel1.Attribut es["onclick"]="handlePanelCl ick()";
}
else
{
int panelX = Int32.Parse(Req uest.Form["hidX"]);
int panelY = Int32.Parse(Req uest.Form["hidY"]);

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

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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(Req uest.Form("hidX "))"?

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

"Jason Hales" <ja*********@ya hoo.com> wrote in message
news:11******** ************@j3 3g2000cwa.googl egroups.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 getPanelMouseCo Ords 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="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="Panel MousePosition.W ebForm1" %>
<!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="So lid">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="JavaS cript">
function getPanelMouseCo Ords()
{
var mouseX = event.clientX + document.body.s crollLeft;
var mouseY = event.clientY + document.body.s crollTop;

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

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

function handlePanelClic k()
{
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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace PanelMousePosit ion
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Pane l Panel1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (! Page.IsPostBack )
{
Panel1.Attribut es["onmousemov e"]="getPanelMouse CoOrds()";
Panel1.Attribut es["onclick"]="handlePanelCl ick()";
}
else
{
int panelX = Int32.Parse(Req uest.Form["hidX"]);
int panelY = Int32.Parse(Req uest.Form["hidY"]);

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

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

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

}
#endregion
}
}


Mar 30 '06 #4
Shawn, just to repeat my emailed reply, it seems as though the
getPanelMouseCo Ords 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
2698
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 selected its position was adjusted in accordance with the mouse move events. After migrating it to VB.NET I tried setting the PictureBox class's BorderStyle to Fixed3D or FixedSingle during the Mouse Down event. However, doing so seems to prevent...
2
24739
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
2079
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 forecolor, backcolor and font. The labels also are rotatable and the text can also be flipped 180 degrees (the flipped text part is still being worked on). I have one context menu for all 30 labels that allows for the property changes to the labels....
13
3159
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 getting to know if the mouse is over that area since I have the coordinates to paint the Pie but I don't know where to start or what to look for really. Best Regard
2
5024
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 is contained by the panel. I've done this : Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs) If Cursor.Position.X > Me.Location.X + Me.Width Or Cursor.Position.Y > Me.Location.Y + Me.Height _
1
1804
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 to another position on the control, and releases the mouse. It isn't like drag and drop, when two objects are interacting. Here I have only one control. Also, I need to when the user also pressed the ctrl key while dragging.
1
4501
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 them to click on a custom control of mine, drag a line to another custom control, and when they let go draw a connection. With Drag and Drop I am able to get the start and end controls, and draw the final connection line, but I cannot for the life...
5
4853
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 users mouse leaves the button, or the buttons in the list I want the list to disappear. I originally tried to place all of the drop down buttons into a single user control, and to then have them appear when users enter some button and disappear when...
2
3147
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 same column then mouseup anywhere in a cell. The mouseup event sometimres fires before the selection of table cells by dragging is complete. It's important that I stop these "false" mouseup's from firing or distinguish them from when I let go of...
4
6979
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 just gotta fix it. I need to make some calculations on the measurements and VB6 is my language. Yes, the system mouse will corrupt the measurement, but it's an auditing function and that's acceptable.
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.