tab multiple selected lines of wpf textbox c# - wpf

I am facing a problem with the text box control in WPF application.
The problem is that when the user selects multilines of the text and then clicks on tab, the selected lines are deleted instead of being indented to the right.
Is there a way to solve this issue?
Appreciate any help.
Thanks
Ahmad

You will need to handle it in the code behind as it is not the default action of a textbox. Many ways you can handle it. You will need to override the PreviewKeyDown and you can set the e.handled to true in order for text to not be overridden.
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox tbx = sender as TextBox;
if (e.Key == Key.Tab)
{
tbx.Text = tbx.Text.Insert(tbx.SelectionStart, "\t" + tbx.Text.Substring(tbx.SelectionStart));
e.Handled = true;
}
}

Sadly have to say that you have to implement that functionality, as the tab key was not made for that in the TextBox.

Related

Winforms- Stop SelectedItem being highlighted without using the style DropDownList

This is about ComboBox used in Winforms. I need to stop the selected item being heighlighted. I know I can get it done if I set the style of the combobox to ComboBoxStyle.DropDownList.
But I'm looking for a solution where I don't have to use that. Instead, at the moment what I have done is using ComboBoxStyle.DropDown.
I don't have any other option, because if I set it to DropDown, I have to deal with some other issue in my code. It's due to something else which I cannot avoid.
Can someone suggest an alternative pls ?
use the following code in your form's Paint event.
private void myForm_Paint(object sender, PaintEventArgs e)
{
comboBox1.SelectionLength = 0;
}
or pass focus to another control in your combo box selected index changed event:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Focus();
}

WPF popup listbox use keyboard for navigation

Can anyone suggest the solution how can I make navigation using down and up keys in listbox which come on popup.
Solutions like set selected items on keyup and keydown event are not working for me.
Should I make something more special then just set selected item in this case?
ListBox already implements selection navigation using keyboard when it is focused.
All you have to do is give it focus when you want, for example in the window that contains it:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
listbox.SelectedIndex = 0;
listbox.Focus();
}
}
Because listbox.Focus(); will only give it focus but won't yet change the selection item (which will make the user hit's the "Down" button twice in order to do so) set the ListBox's SelectedIndex first.
Hope this helps

Remove select all from combobox WPF

I have combobox in WPF application that when the user clicks on it, it selects all the text. How can I change the behavior of this to when the user clicks it just set the typing cursor like a normal textbox?
Try
<ComboBox IsEditable="True" />
According to Reflector, the ComboBox code contains this:
private static void OnGotFocus(object sender, RoutedEventArgs e)
{
ComboBox box = (ComboBox) sender;
if ((!e.Handled && box.IsEditable) && (box.EditableTextBoxSite != null))
{
if (e.OriginalSource == box)
{
box.EditableTextBoxSite.Focus();
e.Handled = true;
}
else if (e.OriginalSource == box.EditableTextBoxSite)
{
box.EditableTextBoxSite.SelectAll(); // <==
}
}
}
This method is registered for the GotFocus event in the static constructor using the EventManager:
EventManager.RegisterClassHandler(typeof(ComboBox), UIElement.GotFocusEvent, new RoutedEventHandler(ComboBox.OnGotFocus));
So, I think you can only change that behavior by deriving a custom control from ComboBox and override this event registration by your own method which replaces the call to SelectAll() with another method which sets the caret to the correct position. However, I do not know how to set the caret to the click position. You might have to use Reflector on the TextBox to find that...
Seems that I had to solve similar issue.
It's quite tricky, but the way I solved is to set IsEditable to false/true from code, at the same time I set the focus on TextBox.
Not the pretties way but does the job.

WPF ComboBox, force input to UpperCase

I have an editable WPF ComboBox with TextSearchEnabled. I need to force the user's text input to uppercase when they type to filter the ComboBox.
I was thinking of modifying the textbox that is part of the control (named 'PART_EditableTextBox') to set CharacterCasing="Upper", however I can't quite figure out how to do this.
Do I need to use a trigger, or modify the template in some way?
This works and seems like a reasonable solution:
protected void winSurveyScreen_Loaded(object sender, RoutedEventArgs e)
{
(comboBox.Template.FindName("PART_EditableTextBox", cbObservation) as TextBox).CharacterCasing = CharacterCasing.Upper;
}
Ensure that the combobox is not collapsed on loaded otherwise the template will not be found.
IMO, the quicker way is to set the UpdateTrigger to PropertyChanged and, in the data object, uppercase the value when it is updated.
I found that post where the attached property is used. That permit to use that for all of your ComboBox without rewriting the code.
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Textbox editableTextbox = sender as Textbox;
foreach (char ch in e.Text)
{
if (Char.IsLower(ch))
{
editableTextbox.Text += Char.ToUpper(ch);
e.Handled = true;
}
}
}
or try creating an attached behaviour for the textbox

highlight a win forms checkbox when cursor is inside it

I have a checkbox with no text specified. Now whenever I tab down to this checkbox, it doesnot get highlighted.I even tried setting focus in checkbox_Enter() event. I checked for focus in this event and focus is there in this checkbox. How to get it highlighted so that user can know that the cursor is there in checkbox.
Try putting just a space into the Checkbox
Or
setting AutoSize to false
Setting the size of the Checkbox
Then there may be somewhere for WinForms to draw the focus ret.
Otherwise you have to to custom draw the Checkbox, or draw the focus rec round the Checkbox yourself.
Whatever you do it will not look that good, as users expect the focus rec to be round the label of the checkbox, and you wish to have a checkbox with no label.
I managed to do it by the below mentioned way
Use a panel.Push the checkbox inside that panel.Set the dimensions of a panel as such that it looks like a rectangle around the checkbox.In checkbox_enter() event set the border
BorderStyle.FixedSingle;
And in checkbox_Leave() event set the border again to
BorderStyle.None;
So this way it will tell user that focus in inside the checkbox.
To make the checkbox appear highlighted I had it change color on got focus and change back on lost focus.
this part is in the Form1.Designer.cs:
private void InitializeComponent()
{...
ckBox1.GotFocus += new System.EventHandler(checkBox_Highlight);
ckBox1.LostFocus += new System.EventHandler(checkBox_EndHighlight);
ckBox1.MouseHover += new System.EventHandler(checkBox_Highlight);
ckBox1.MouseLeave += new System.EventHandler(checkBox_EndHighlight);
}
This part is in the Form1:
private void checkBox_Highlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
control.FlatStyle = FlatStyle.Flat;
control.ForeColor = Color.Blue;
}
private void checkBox_EndHighlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
if (!control.Focused)
{
control.ForeColor = DefaultForeColor;
}
}
While tabbing, this puts a light blue shadow beneath the CheckBox on Enter and on Leave - at least on my Windows7:
...
checkBox1.Enter += new System.EventHandler(check_Enter);
checkBox1.Leave += new System.EventHandler(check_Leave);
...
private void check_Enter(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = SystemColors.Highlight;
}
private void check_Leave(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = Color.Transparent;
}
Actually the highlights comes on the text of the checkbox not the checkbox itself
So, if I was you, I would put any control in the background of my check box and give it the focus when my checkbox is focused, to have the same look of default controls highlights.
which will be shown to end users as checkbox highlights.

Resources