SelectOnEntry for wpf textbox? - c#

I want to select all text into a textbox just when it got focus , there is a property named SelectOnEntry in wpf texbox or an equivalent?
If Not how to implment it ?

Attach handler for GotFocus event:
<TextBox GotFocus="TextBox_GotFocus"/>
and in handler:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).SelectAll();
}
If you need to do it in pure XAML way, you can create an attached property to select all text on focus. Refer to the solution here.

Related

WPF TextBox's IsInactiveSelectionHighlightEnabled property is not work?

WPF's TextBox has a property named IsInactiveSelectionHighlightEnabled. I set this property to true in order to make a TextBox always show selection. However, it doesn't work in this case:
private void button_Click(object sender, RoutedEventArgs e) {
textBox.Select(0, 10);
}
I just want to see the selection after clicking the button. But selection will not appear until I right click the TextBox. Why? Am I miss something?
You should have the Keyboard focus on your textbox to select the text in it.
Add this code to your button click event before the selection.
Keyboard.Focus(textBox);
Hope it helps.

Onclick event for textbox in csharp form application

I'm creating form application on c# . I have dragged a textbox with some text in it.
private void textBox1_Click(object sender, EventArgs e)
{
}
Now what is the event for the onlick on textBox1 ?
I need to add this on that function textBox1.Clear();
P.S I searched everywhere. But all i can find is jquery and javascripts... No c#.
EDIT
I tried onfocus like below..but its not working
private void textBox1_OnFocus(object sender, EventArgs e)
{
MessageBox.Show("dsd");
}
If you want to do something when the control is clicked the handle the Click event, not the TextChanged event. Presumably you just double-clicked the control in the designer. That will only handle the default event. To handle other events, open the Properties window, click the Events button at the top and then double-click the appropriate event.
That said, is Click really appropriate? What if the user enters the TextBox using the Tab key? If what you actually want to do is act when the control gets focus then you should handle the Enter event.
You can handle OnFocus/GotGocus event in the TextBox, and clear the text in the textbox.
Hope this helps.

WPF SizeChanged event doesn't fire

This is a bit of a noob question.
Basically, I have a grid MainGridin my WPF window
I'd simply like to be notified when the MainGridis resized ( when user maximizes/resizes the window).
This does nothing at the moment. Am I supposed to declare and then add this SizeChanged event earlier before it will work?
private void MainGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
MyTextBox.Text = "MainGrid resized";
}
Thank you
If you have your event in code-behind and this in XAML:
<Grid SizeChanged="MainGrid_SizeChanged">
it will work.

Silverlight MVVM - Twoway binding not fired on Datepicker

I have Silverlight MVVMLight 4.0 application in which I have datepicker.
The datepicker is two way bound to the viewmodel. There is no code behind.
This works fine when tabbing of the datepickers textbox and will change the underlying property of the object.
But when I change the text box and don't tab off and click save the change is not registered.
I have looked at the various events that are fired and they don't fire unless you tab off
private void startDateDatePicker_TextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_TextInputStart(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_TextInputUpdate(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
}
Does anybody have a workaround for this?
kind regards,
Pat
I'm fairly sure that the TextBox needs to lose focus before the property is updated.
You could try handling the KeyDown event of the TextBox and update the property from there, but I'm not sure how well that would behave.
Change the UpdateSourceTrigger value in your binding.
If is not set, uses the default (which is the Lost Focus for TextBox.Text)
Depending the inner-workings and needs of your screen, you can set it to PropertyChanged or Explicit.
http://msdn.microsoft.com/en-us/library/ms752347.aspx

Equivalent of WinForms TextBox.Validating event in WPF

In WinForms I could handle the Validated event to do something after the user changed text in a TextBox. Unlike TextChanged, Validated didn't fire for every character change; it only fired when the user was done.
Is there anything in WPF I can use to get the same result, an event raised only after the user is done changing the text?
LostFocus will fire when the user moves from your textbox onto any other control.
It seems that there is no native solution.
The LostFocus event is a good idea. But when the user click on Enter, he wants the TextBox to validate the change.
So here is my suggestion : use the LostFocus event and the KeyDown event when the key is Enter.
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// code to lauch after validation
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// call the LostFocus event to validate the TextBox
((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
}
}
LostFocus is not equivalent to Validate. It creates lots of problem when you have multiple text boxes on one screen and every text box has some logic written in Validate. In validate event you can control focus easily but not in LostFocus so easily.
You can also try Binding.ValidationRules
Documented at : http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx
Here is the article to get started:
How to implement binding validations :
http://msdn.microsoft.com/en-us/library/ms753962.aspx

Categories

Resources