I'm using a read-only TextBox in my Windows Phone app to display copyable text. Now I would like taps on the TextBox a single character, the one on which the user tapped.
The following code properly selects the first character, but a split second later the selection is reset (that is, Windows Phone automatically selects the entire word at the tap location):
private void TextBox_Tap(object sender, System.Windows.Input.GestureEventArgs e) {
TextBox box = sender as TextBox;
Dispatcher.BeginInvoke(delegate { box.Select(0, 1); });
//simplified example: select only the first character.
}
This works fine in WP7, though.
Any ideas on how to revert to the old behaviour?
I'm not sure how this would interact with the rest of the code you've got, but you should be able to use a combination of SelectionChanged and GotFocus events on the TextBox to replace the previous use of the Tap handler to stop the Selection being reset by the user.
Update
That's not appropriate for this scenario.
From a quick play on my device it looks like some other event like MouseLeftButtonUp is causing this and not Tap. Try moving your code to that event.
Related
I have a SearchBox that works fine on desktop. When clicking on icon the QuerySubmitted event is fired.
However on phone this is not the case. With the KeyUp event i can get the enter from the keyboard UI but that doesn't help me with the issue of the icon not working.
<SearchBox PlaceholderText="" QuerySubmitted="SearchBox_QuerySubmitted" QueryChanged="SearchBox_QueryChanged">
</SearchBox>
Code behind
private void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
{
Debug.WriteLine("SearchBox_QuerySubmitted");
}
private void SearchBox_QueryChanged(SearchBox sender, SearchBoxQueryChangedEventArgs args)
{
Debug.WriteLine("SearchBox_QueryChanged");
}
Is this a bug or is this intended?
Is there a work around?
For UWP you should use AutoSuggestBox:
To use an AutoSuggestBox, you need to respond to 3 user actions.
•Text changed - When the user enters text, update the suggestion list.
•Suggestion chosen - When the user chooses a suggestion in the suggestion list, update the text box.
•Query submitted - When the user submits a query, show the query results.
Query submitted will be raised properly on phone. I just tested it.
More info here : https://msdn.microsoft.com/en-gb/library/windows/apps/windows.ui.xaml.controls.autosuggestbox.aspx
User have already selected some word by tap in WebBrowser (windows phone),
1) user needs to tap once to remove selecting
2) user taps again to select some text
Can we do 2 without doing 1?
I can get tap coordinates, if it can help
_webBrowser.MouseLeftButtonUp += _webBrowser_MouseLeftButtonUp;
void _webBrowser_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var tapPosition = e.GetPosition((UIElement)sender);
}
tapPosition here - it is Point with X and Y properties, where user have taped. I need to select text under this position.
May be there is some javascript function, that I can call.
You need to write your own java script functions.
See this answer it will help you:
Retrieving Selected Text from Webbrowser control in Windows phone 7
About delete selection in javascript: How to remove text selection from selected text which is coming by default on page load?
In my app,I just have a page with four text boxes, so when i click a text box soft keyboard appears, now when i want to move to next textbox then i have to tap outside the textbox to make the keyboard disappear and then click on another text box. I don't think it is user friendly, so i have two options,
1)To change the functionality of return button(to make it work as tab).
2)To reduce the frame size and so scrolling will be enabled.
How can I do the foretold two options in windows phone 7??
for the first option
Make return key of the input panel work like tab key.
make key down event of 1st textbox like this
private void txt1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
txt2.Focus();
}
}
similarly make this event for txt2 and txt3 and give focus accordingly and on on txt4 keydown event focus the main grid.
and about the 2nd way. Its a big problem in wp according to my knowledge.
For moving to next textbox #Amu 's answer will work perfect, and to dismiss the keyboard,
if (e.Key == System.Windows.Input.Key.Enter)
{
this.Focus();
}
That will take the focus away from your text box and will bring it to your screen.
And So keyboard will disappear!
TLDR: my textboxes will update their Foreground and Background colors properly when manually selected by touch input, but will fail to update if I use the Textbox.Focus() method after they are created.
The question is similar to this - windows phone - textbox background
My Application creates a textbox with an associated GotFocus event, that changes the Foreground and Background colors of the textbox to the system default whenever the textbox receives focus. Upon the user pressing the enter key on the keypad, the app then generates another identical textbox below the first.
The issue is that for any of these textboxes, if the textbox is pressed manually, everything works fine, and the textbox is displayed how it's meant to be. However, if I use TextBox.Focus() after the enter key is pressed, although focus is passed to the textbox, and the GotFocus event has been processed, the Background and Foreground colors are not updated, leaving white text on a white background.
I have tried passing focus between the textbox for a number of times (up to 10), and even though I can confirm that the focus is being passed as it should, the colors are only updated if the user gives focus to the textbox (if I give focus to the textbox via code, I must then manually deselect and then reselect it for the color change to apply. If I don't give focus via code I can simply select it).
The code for this is:
public void txtInputKeyUp(object sender, KeyEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if (e.Key == Key.Enter)
{
Evaluate(txtBox);
InitializeDivider();
InitializeTextInput();
InitializeTextOutput();
txtInput[txtInput.Length - 1].Focus();
}
}
public void txtInputGotFocus(object sender, RoutedEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if(txtBox.Text == "Input Here")
{
txtBox.Text = "";
}
txtBox.Foreground = (SolidColorBrush)App.Current.Resources["PhoneForegroundBrush"];
txtBox.Background = (SolidColorBrush)App.Current.Resources["PhoneBackgroundBrush"];
}
The Initializeblabla stuff basically just creates the relevant textboxes and all associated data. I've tried switching focus between textboxes, disabling and enabling the specified textboxes and a few other options, and consistently the only thing that works is not giving focus to the textbox via code, but rather waiting for the user to select the textbox, something I'm not really satisfied with. Attempting to manually edit the style didn't help either.
My eventual solution was to add a timer to the app with a very short interval (short enough to make the change not visible) and have that change the color of the textbox after the textbox had received focus.
I am writing a metro app where it makes sense for focus to jump to a single text box anytime the user starts typing. But I can't figure out how to implement this functionality locally, without modifying other user controls to detect keydown and forward focus to the text box.
In WinForms I would have used the form's "KeyPreview" property, which caused any key presses within the form's controls to fire form KeyDown/KeyPress/KeyUp events. I can't find any equivalent in metro.
I tried the naive solution of just forcing focus to the text box anytime it left, but that has obvious problems (e.g. buttons flicker instead of staying highlighted when you click and hold on them).
How can I ensure any keyboard typing goes to a specific text box?
The event needs to be placed on the current core window, which is the root element all controls are nested on.
Windows.UI.Xaml.Window.Current.CoreWindow.KeyDown += (sender, arg) => {
// invoked anytime a key is pressed down, independent of focus
}
Here you go ...
Responding to keyboard input (Metro style apps using C#/VB/C++ and XAML)
&&
Implementing keyboard accessibility (Metro style apps using C#/VB/C++ and XAML)
Pay special attention to routed events. There are examples there too.
Hope this helps.
In your xaml code u bind these to events to page::
Loaded="pageRoot_Loaded_1"
Unloaded="pageRoot_Unloaded_1"
and inside these methods u have to bind and unbind ur events for keydown or keypress
private void pageRoot_Loaded_1(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void pageRoot_Unloaded_1(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
}