Windows phone 7 Handling backspace KeyDown event - windows-phone-7

I have a textbox in XAML:
<TextBox Text="blah"
Name="blah"
LostFocus="blah_OnLostFocus"
KeyDown="blah_OnKeyDown"/>
.cs file has the following event declared:
private void blah_OnKeyDown(object sender, KeyEventArgs e)
{
TextBox t = sender as TextBox;
int i = 0;
if(e.Key == Key.Delete)
i = 1;
}
When I press the backspace key on the emulator's keyboard, the event is ignored. If I press any other button, it is triggered. Also, when I use the same event body for the KeyUp event, backspace key triggers it.
How do I track when the backspace key was pressed for KeyDown event? Why is pressing the backspace key does not trigger the KeyDown event?
Thank you.

The backspace button is handled internally by the textbox control. The control will take the backspace event and remove a letter and won't bubble up the event to your event. This is by design. You'll notice that if there are no letters in the textbox, then the event is bubbled up. In order to get around this, you can use AddHandler to handle the event. Try doing this:
//Handle the mainpage loaded event and add the handler to the textbox
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
textBox1.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(blah_KeyDown), true);
}
Then change your Key_Down event handler to this:
private void blah_OnKeyDown(object sender, KeyEventArgs e)
{
TextBox t = sender as TextBox;
int i = 0;
if(e.Key == Key.Back)
i = 1;
}
That should have the textbox internally handle the event, but also call your OnKeyDown event.

To detect backspace, you need to use Key.Back rather than Key.Delete
See Key enumeration at http://msdn.microsoft.com/en-us/library/system.windows.input.key(v=VS.95).aspx

Related

WindowsForms TopMost versus ComboBox

I have a WindowsForms application in which I open a Form as a Dialog (Form2.ShowDialog) and in this Form I have a Timer that sets the TopMost property of the Form to true.
But I also have a ComboBox in this Form and when I click on the ComboBox to select an Item, the list opens and closes immediately as the Timer sets the TopMost property back to true.
If you ask me This is wrong way and should be replace your "load data function" from timer to form_load event. so if you want current way you should disable timer in ComboBox Enter Event and enable timer in ComboBox Leave Event.
private void comboBox1_Enter(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
timer1.Enabled = true;
}

Xamarin DataPicker how to handle done button click event

I added to my DataPicker unfocused event. And in this event I would like to check if Done button was clicked.
private void DatePicker_Unfocused(object sender, FocusEventArgs e)
{
}
Or other option.
Is it possible to add listener to button Done, and if was clicked then fired some method?
How can I do it?
Thank you.
Every UI control which receives input always has some event handlers which you can listen to.
In your case the DatePicker has an Event Handler which fires when the Date property changes.
Here is the documentation:
DatePicker Event Handler - Xamarin Documentation
The implementation of this handler is simple:
DatePicker datePicker;
DateTime newDate;
DateTime oldDate;
public YourPage()
{
datePicker.DateSelected += Date_DateSelected;
}
void Date_DateSelected(object sender, DateChangedEventArgs e)
{
newDate = e.NewDate;
oldDate = e.OldDate;
}
Always read the documentation first, as that's the best way to learn how to use a specific control.

Xaml Button is "clicked" if it's pressed-and-hold on a touch screen

I am developing a Windows UWP App with C++. I want to add a button that can either be left-clicked or right-clicked and do corresponding task.
The button works perfectly on normal devices with mouse. Unfortunately on touch screen, if users press-and-hold the button, both left-click and right-click events are triggered.
I am using "Click" to handle the left click event and "RightTapped" to handle the right click event. Any suggestion on how to disable the left-click event when users press-and-hold the button? Thank you!
The XAML code is short:
<Button x:Name="m_NewPageButton"
Content="New Page"
Height="48"
Width="199"
Click="OnClick"
RightTapped="OnRightClick"/>
And here's the cpp code:
void MainPage::OnClick()
{
// Left click task
}
void MainPage::OnRightClick()
{
// Right click task
}
Edit:
Thanks guys, the solution is to change the left click handler to , which will not be triggered by press-and-hold.
Hacky but works...
private bool rightClicked;
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (rightClicked)
return;
await new MessageDialog("left").ShowAsync();
}
private async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
rightClicked = true;
await new MessageDialog("right").ShowAsync();
rightClicked = false;
}
I'm guessing this is a flaw in the design.. :(
Actually... If you use the button's Tapped and RightTapped events it works as it should. So in this case just replace Clicked with Tapped and all is well... Then you can ignore the hack I put above.
You can use the Holding event to detect a tap and hold. Use the Tapped event for a single tap (or click). The Tapped event should only fire for touch on a quick tap and release. The mouse specific click will raise Tapped but a touch tap-and-hold will not.

How to enable PhoneApplicationPage_BackKeyPress event when set textbox.Focus() on textbox_LostFocus event in windows phone 8

How to enable
PhoneApplicationPage_BackKeyPress
event when set textbox.Focus() on textbox_LostFocus event in windows phone 8.
I want to show keyboard when i add some item into listbox from textbox. Listbox gets update but my keyboard always gets hide on button click.
I want to make it open all the time till i press back key.
Thanks,
Nishant
you can get BackKeyPress Event by :
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
}
You can write textbox.Focus() at the end of button click when all work is done
try this :
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ListboxName.Items.Add(TextBoxName.Text);
TextBoxName.Text = "";
TextBoxName.Focus();
}
I have implemented. Its working...

Where Can I Find Key Events in LightSwitch?

I'm working on a project in Lightswitch and I'm trying to handle the keyup event on specific screen
I couldn't find it, where can I find those events (such us KeyUp KeyDown LostFocus ext. )
and if they are not supported what should I do to handle a situation like that?
I guess you are referring to controls and not the actual screen. So, for example, to handle keyboard events on a textbox, one way of achieving it is like in this example, where I have an "Address1" textbox and I want to change its text whenever the user types a certain letter:
1 - On the Activated event of the screen, you can get to the required textbox:
partial void CustomersListDetail_Activated()
{
this.FindControl("Address1").ControlAvailable += AddressTextBoxAvailable;
}
2 - On the Available event handler, you can connect with the required event (you can have KeyUp, KeyDown, LostFocus and others):
private void AddressTextBoxAvailable(object sender, ControlAvailableEventArgs e)
{
((System.Windows.Controls.TextBox) e.Control).KeyUp += AddressTextBoxKeyUp;
}
3 - On the KeyUp event handler, you can do your manipulation:
private void AddressTextBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
((System.Windows.Controls.TextBox) sender).Text = "You typed A";
}
}

Resources