473,387 Members | 1,876 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,387 software developers and data experts.

RE: wpf: Trap keyup event in canvas

Hi George,

Keyboard input introduces the idea of focus. In Windows, a particular
element is designated as having the focus, meaning that it acts as the
target for keyboard input. The user sets the focus by clicking the mouse or
tapping the stylus on the control in question, or by using navigation keys
such as the Tab and cursor keys.

In principle, any user interface element can receive the focus. The
IsFocused property is defined on UIElement, the base class of
FrameworkElement. However, the Focusable property determines whether this
feature is enabled on any particular element. By default, this is true for
controls and false for other elements.

In your practice, neither the Rectangle nor the Polyline is control, so
they cannot receive focus and the KeyDown or KeyUp event.

To trap the KeyUp event, you can do it in the window level. You can also
trap the MouseDown event in the window and set the current "selected"
element. If the Delete key is pressed, remove the current selected element
from the Canvas. For example:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

this.MouseDown += new
MouseButtonEventHandler(Window1_MouseDown);
this.KeyUp += new KeyEventHandler(Window1_KeyUp);
}

object currentElement = null;
void Window1_MouseDown(object sender, MouseButtonEventArgs e)
{
currentElement = e.OriginalSource;
}

void Window1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (currentElement != null && currentElement is UIElement)
{
if (myCanvas.Children.Contains(currentElement as
UIElement))
{
myCanvas.Children.Remove(currentElement as
UIElement);
currentElement = null;
}
}
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 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 or complex
project analysis and dump analysis issues. 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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #1
2 6491
Thanks Linda,

Even though Chris's suggestion worked very good, I ended up using a
variation of your suggestion below because I also had to create a collection
of selected UIElements (for when the user wants to select more than one item
at a time). Then based on the window's key up event, I could delete
everything in the collection.
"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:uC**************@TK2MSFTNGHUB02.phx.gbl...
Hi George,

Keyboard input introduces the idea of focus. In Windows, a particular
element is designated as having the focus, meaning that it acts as the
target for keyboard input. The user sets the focus by clicking the mouse
or
tapping the stylus on the control in question, or by using navigation keys
such as the Tab and cursor keys.

In principle, any user interface element can receive the focus. The
IsFocused property is defined on UIElement, the base class of
FrameworkElement. However, the Focusable property determines whether this
feature is enabled on any particular element. By default, this is true for
controls and false for other elements.

In your practice, neither the Rectangle nor the Polyline is control, so
they cannot receive focus and the KeyDown or KeyUp event.

To trap the KeyUp event, you can do it in the window level. You can also
trap the MouseDown event in the window and set the current "selected"
element. If the Delete key is pressed, remove the current selected element
from the Canvas. For example:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

this.MouseDown += new
MouseButtonEventHandler(Window1_MouseDown);
this.KeyUp += new KeyEventHandler(Window1_KeyUp);
}

object currentElement = null;
void Window1_MouseDown(object sender, MouseButtonEventArgs e)
{
currentElement = e.OriginalSource;
}

void Window1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (currentElement != null && currentElement is UIElement)
{
if (myCanvas.Children.Contains(currentElement as
UIElement))
{
myCanvas.Children.Remove(currentElement as
UIElement);
currentElement = null;
}
}
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 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 or complex
project analysis and dump analysis issues. 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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


Jun 27 '08 #2
Hi George,

Thank you for your feedback! I'm glad to hear the problem is solved now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice day!

Sincerely,
Linda Liu
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:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #3

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

Similar topics

2
by: Rich | last post by:
Hello, I need to trap/detect when a textbox is entered via the tabkey. If the textbox is not empty when entered via the tabkey then set focus to next textbox. To enter that textbox would then...
3
by: RobinS | last post by:
I'm trying to learn WPF and do it in VB instead of C#. (God forbid I should do *anything* the easy way. ;-) Here's something weird. On p162-3 of this book by Petzold (in C# of course) in an...
3
by: moondaddy | last post by:
I'm working in a WPF windows application and am wondering if it's possible to rotate a Canvas or user control derived from Canvas. I created a user control which derives from Canvas and I need to...
8
by: =?Utf-8?B?RyBIdXN0aXM=?= | last post by:
This is the 2nd time posting so sorry for duplications. I am using VB.NT 2005 & a standard Combobox. I've been wracking my brain over this problem for a over a month & cannot seem to find a way to...
2
by: moondaddy | last post by:
Below is some xaml that is a mockup of a control I'm building. It's a shape that will be used in a diagramming tool. The red, blue and green rectangles simulate connectors on the side of the...
4
by: moondaddy | last post by:
I have a wpf project where I use a canvas to drag shapes around. when I drag a shape beyond the right or bottom side I get scrollbars which is good. I also get scrollbars when I zoom in and a...
1
by: Chris Jobson | last post by:
>I have a canvas which I use for dragging shapes around such as rectangles Add Focusable="True" to the XAML for the PolyLine and Rectangle elements and then when you set the focus to them they...
1
by: asharda | last post by:
Hi, I have a listbox on my WPF window and a cnavas. I have to be able to drag the items from the list box and drop them on the canvas and draw the item accorgingly. If anyone knows how to...
23
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.