WPF TextBox's IsInactiveSelectionHighlightEnabled property is not work? - c#

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.

Related

Hide keyboard Windows Phone 8.1

When I try to add a text in the TextBox from a canvas using handwriting, the cursor go to the TextBox and the keyboard shows, and I try to add some code like make the TextBox isReadonly or trying to hide the keyboard and doesn't work.
I want every time select an item from the ListBox the item add to the TextBox without showing the keyboard. the action on RecognizedListBox_SelectionChanged a ListBox
private void RecognizedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (RecognizedListBox.SelectedItem == null)
return;
//gte the selected item from listbox
string inputTextWritePad = RecognizedListBox.SelectedItem.ToString();
//add the item to RichEditBox
MyTextNote.Text += inputTextWritePad + " ";
//clear the canvas return the listbox to vide
ClearAllClick(sender, e);
}
If I add isReadonly for TextBox, it will permanent disable to edit it, and I can't add any text using keyboard. I don't know where I will put my code, or verify when I need the keyboard to use it.
I see if I need to hide the keyboard, I must have an event for the keyboard button or something like this
private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if(e.Key==Windows.System.VirtualKey.Enter)
{
Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide();
}
}
but nothing to figure out!!
update 1:
I add this code
private void MyTextNote_GotFocus(object sender, RoutedEventArgs e)
{
InputPane.GetForCurrentView().TryHide();
}
and help me to not show the keyboard, but I need to show it when I clicked the textbox, I try whit tapped but nothing help.
Here's a property to avoid displaying Keyboard if your TextBox receives focus programmatically :
<TextBox PreventKeyboardDisplayOnProgrammaticFocus="true"/>
Set this property to true to prevent the onscreen touch keyboard from
showing when focus is programmatically set on a text box. By default,
the onscreen touch keyboard is displayed whenever focus moves to an
editable text box and the most recent input was generated by touch.
Official Doc

How to select all textbox text and textbox opacity in WP application

I use a textbox as an address bar for a WP browser application. I want to select all text when a user selects the textbox and also to modify the opacity.
I tried using GotFocus method to do that. I see that the whole text is selected for 1 second or so and then it is deselected. I also need to modify the opacity once the focus is on textbox and when the textbox loses focus. Using GotFocus method I can modify the opacity but when the focus is lost, when I set again the opacity percent nothing happens.
Can you give me a hint regarding the events that determine the text to be selected for a short period of time and for the opacity problem?
private void URLTextBox_GotFocus(object sender, RoutedEventArgs e)
{
URLTextBox.Opacity = 50;
URLTextbOX.SelectAll();
}
private void URLTextBox_LostFocus(object sender, RoutedEventArgs e)
{
URLTextBox.Opacity = 10;
}
You can try subscribing to one of the tunnelling events (PreviewGotKeyboardFocus and PreviewLostKeyboardFocus) instead of the GotFocus event.

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.

C# autocomplete combobox trigger SelectionChangeCommited

I'm having problems with the autocomplete property of a combobox. I want to trigger the SelectionChangeCommited event every time I choose an item using the autocomplete but it's not working. The only way the event is triggered is when I use the mouse click and select an option or when the combobox is focused and I use arrow keys on the keyboard. How do I achieve this behaviour using the autocomplete property?
My combo has these properties set:
AutoCompleteMode = SuggestAppend
AutoCompleteSource = ListItems
FormattingEnabled = True
The items in my combo are set with a datasource.
Any ideas?
Thanks
If you mean that you want it to register a change when you start typing:
Call the SelectionChangeCommited event from the TextChanged event.
If you've never done this, the most basic example I could find was on the .net forums here. Granted, the methods shown there are generalities, but is very simple to understand and apply to your code.
EDIT FIXED (as of most recent comment):
Still tie the events together, but instead of using TextChanged, which would occur ever time you type, use the SelectedIndexChanged, which occurs when you use the mouse to select an auto suggested item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}
you canuse a trick and call comboBox1_SelectionChangeCommitted
in Validated event
when ever text in combobox changes and user leave the combo box it will be fired
private void comboBox1_Validated(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}

Winforms: how to open combobox properly?

I have a combobox on the winforms. On Enter even I open it:
cbo.DroppedDown = true;
But if I do that the combo opens and closes immediately. What am I doing wrong?
The reason why I use this event is I need to open combo on Tab, when user click tab on the previous control, this combo opens properly. But if user clicks the combo it opens and closes. How to do that properly?
Thanks
I tried it just like this:
private void comboBox1_enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
no changes to mouseup or timers. it behaved just as expected. Whether I selected the comboBox with a mouse click or tabbed into it the drop down list appeared and stayed open until I selected something.
I would look to see if there is something else pulling focus off the box.
The reason you are having this problem is because the mouseup event occurs after the enter event and the default window procedure is closing the combobox.
In the enter you could check the mouse button status and if the button is down, do not open the combo. Then have another event handler for the mouseup event to open the combo.
Another option is to set a timer for a few milliseconds, and open the combo when it goes off.
Knowing this is a bit old, but I found that this works well. You can TAB into the combo box and it opens and if you click the arrow it doesn't close back up.
private void ComboBox_Enter(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.None)
((System.Windows.Forms.ComboBox)sender).DroppedDown = true;
}
Set DroppedDown = true in GotFocus event of the combobox. Otherwise, the dropdown list will show at wrong location.
void cbo_GotFocus(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
cbo.DroppedDown = true;
}
I think you just need to focus it first before open the comboBox.
cbo.Focus();
cbo.DroppedDown = True
Hope it works for you.

Categories

Resources