Windows Phone TextBox Control Foreground and Background Brushes not updating properly - c#

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.

Related

Selecting all text in TextBox when focusing

I have a small window with 2 textboxes in a grid databinded to some properties, it is called from context menu of another window. I made one of textboxes focused after appearing by
<Grid FocusManager.FocusedElement="{Binding ElementName=priceBox}">
I would like to have behavior that Text in TextBox would be selected (dark blue background) so if I start type new symbols old ones being immediately deleted. I don't want to delete old symbols first. Same behavior I would like to have after I press Tab to switch to next textbox.
Is there any textbox settings to achieve this?
I have very old winforms applications and It looks like it was behaving this way by default.
You will have to set Keyboard focus on the TextBox before selecting the text
e.g:
private void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
Keyboard.Focus(textBox);
textBox.SelectAll();
}
}

Textbox issue in windows phone 7?

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!

Prevent auto-selection in Windows Phone 8 textboxes

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.

update datagridview cell style

I am trying to allow the user to change the color of the datagrid displayed in my app.
for this I use user settings and colordialog boxes.
problem is, when I try to update the color, it isnt displayed, and I either have to close/reopen the app to see the changes, or load a completly different DGV (they are in a tabcontrol).
here is the code I use at the update:
AlternatingRowsDefaultCellStyle.BackColor = Properties.Settings.Default.CellBackColor1;
am I missing something?
I tried to refresh the DGV, but it doesnt change a thing.
I added a Button to a form that opens up a ColorDialog and waits for the users to select a color. If the user hits the OK button, it first saves the setting and then sets the dataGridView.CellStyle.BackColor to whatever color the user selects. This forces an immediate update of the form with the correct background color.
private void button1_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.CellBackColor1 = colorDialog1.Color;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = colorDialog1.Color;
}
}
I assume your Form.Load and Form.FormClosing events handle the saving of the settings.

Auto-edit row in a Silverlight DataGrid

Is it possible for the DataGrid to automatically enter edit mode when the user starts typing on the keyboard, without having to hit F2 or double-click with the mouse first?
I've tried those 2 solutions:
Define only a CellEditingTemplate (no CellTemplate) : doesn't work. the datagrid always displays a textbox but the user still has to double-click or press F2 to enter edit mode
Handle KeyDown event and call BeginEdit:
private void DataGrid_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
(sender as DataGrid).BeginEdit();
}
This works, but I lose the first key the user has pressed. I could work around this problem but I'm looking/hoping for a better solution.
Thank you!

Categories

Resources