I have a selection problem with RichTextBox control. If the control contains hidden text, the selection behaves strangely.
If I do a selection with the mouse, sometimes the bug is present, sometimes not. Especially if I select more lines the bug seems to disappear.
But the bug is very annoying if the user tries to select the text with the keyboard.
The issue is the following: Let's say my control has this text:
There is the little upgraded control that hopefully will make a
differnce when it is hidden text the reason
Then let's say we hide words upgraded, hopefully, hidden by applying proper RTF tags:
#"{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 There is the little \v upgraded \v0 control that \v hopefully \v0 will make a differnce when it is \v hidden \v0 text the reason\par}";
It all looks good, but when the user tries to select the text using the keyboard, selection seems to reset every time a hidden word is reached.
It is crucial for my control to contain that hidden text (some important id's from my objects that are forming the content inside the control are stored as hidden text at special positions, and I can't/don't want to change that).
I am using the following Form, where richTextBox is the RichTextBox in question and RichTextBox_SelectionChanged is the SelectionChanged event handler that we will try to use to fix our issue.
public MainForm()
{
InitializeComponent();
this.richTextBox.Rtf =
#"{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 My \v upgraded \v0 control that \v hopefully \v0 will make it\par}";
this.richTextBox.SelectionChanged += RichTextBox_SelectionChanged;
}
Basically, the idea is simple - use SelectionChanged handler to properly Select hidden data alongside the previous selection.
For that we will have to store the previous selection data:
private class SelectionData
{
public static SelectionData FromStartAndEnd(
Int32 start,
Int32 end)
{
return new SelectionData(
start: start,
length: end - start);
}
public SelectionData(TextBoxBase tb)
: this(
start: tb.SelectionStart,
length: tb.SelectionLength)
{ }
public SelectionData(Int32 start, Int32 length)
{
this.Start = start;
this.Length = length;
}
public readonly Int32 Start, Length;
public Int32 End
{
get
{
return this.Start + this.Length;
}
}
}
in some field:
private SelectionData _previousSelection;
And update/fix selection inside the SelectionChanged hanlder
private void RichTextBox_SelectionChanged(object sender, EventArgs e)
{
var newSelection = new SelectionData(this.richTextBox);
this.SelfUpdateSelection(newSelection);
}
SelfUpdateSelection method would be something like:
private Boolean _isSelectionSelfUpdating = false;
private void SelfUpdateSelection(SelectionData newSelection)
{
if (!this.IsKeyBoardSelection())
{
// Or it will use previous selection when we don't need it.
this._previousSelection = null;
return;
}
if (this._isSelectionSelfUpdating)
return;
this._isSelectionSelfUpdating = true;
try
{
var fixedSelection = this.FixSelection(newSelection);
this.richTextBox.Select(
start: fixedSelection.Start,
length: fixedSelection.Length);
this._previousSelection = fixedSelection;
}
finally
{
this._isSelectionSelfUpdating = false;
}
}
IsKeyBoardSelection for simplicity can be something like the following, though properly detecting selection change source will be more difficult:
private bool IsKeyBoardSelection()
{
// It may not be true, but usually close enough.
return Control.ModifierKeys.HasFlag(Keys.Shift);
}
FixSelection method should compare whether newSelection can be a this._previousSelection and create a new SelectionData that will contain both newSelection, this._previousSelection and the hidden data between them.
You can use something like this:
private SelectionData FixSelection(SelectionData newSelection)
{
if (this._previousSelection == null)
return newSelection;
var start = Math.Min(
newSelection.Start,
this._previousSelection.Start);
var end = Math.Max(
newSelection.End,
this._previousSelection.End);
return SelectionData.FromStartAndEnd(
start: start,
end: end);
}
but it:
Will work only with forward(right arrow) selection - can be fixed by adding some additional logic to FixSelection.
Will also require a bit of additional this._previousSelection handling (like resetting it on FocusLost event) - there are some edge cases, but still nothing impossible.
public MainForm()
{
...
this.richTextBox.LostFocus += RichTextBox_LostFocus;
}
private void RichTextBox_LostFocus(object sender, EventArgs e)
{
this._previousSelection = null;
}
P.S.: For simplicity I have implemented everything inside the form with fields and form-level handlers, but with some effort it could be made into something reusable (at worst derived RichTextBox, at best some external component that will provide such handling for RichTextBox).
Related
I have design 1 winform to look like the picture. But I want the highlighted yellow part to be dockable with dockpanel suite reference. Is that do-able or any other suggestion of better design?
Right now the treeview is on the dockpanel and the red box part is a usercontrol placed in the same dockpanel. I tried to put the redbox as another form but I can't place it as it is in the picture. Also, this winform is need to be responsive so I put in the redbox part in a table layout panel.winform design and not familiar actually with the dockpanel suite reference. If there is a beginner tutorial that I can refer to, it would be much appreciated.
Current design:
There are two approach to your problem. First is dirty one and second elegant one. By dirty and elegant i mean way they display. Method they work are both same.
I will explain to you how to do it on empty form and you just implement that in your populated one.
First create new form.
Add 2 or more GroupBoxes to it
Add some items inside them (just to see if it works)
At the top of the each boxes add Button which will toggle visibility
Our form now looks like this and let's look of code behind it.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class TestForm : Form
{
// This is property
bool ShowFirstGroupBox
{
get
{
// We let user get our property from private variable
return _ShowFirstGroupBox;
}
set
{
// When user change this property we do something based on that
switch(value)
{
case true:
groupBox1.Size = new Size(groupBox1.Width, FirstGroupBoxDefaultHeight);
break;
case false:
groupBox1.Size = new Size(groupBox1.Width, 55);
break;
}
_ShowFirstGroupBox = value;
}
}
bool ShowSecondGroupBox
{
get
{
return _ShowSecondGroupBox;
}
set
{
switch (value)
{
case true:
groupBox2.Size = new Size(groupBox1.Width, FirstGroupBoxDefaultHeight);
break;
case false:
groupBox2.Size = new Size(groupBox1.Width, 55);
break;
}
_ShowSecondGroupBox = value;
}
}
// We store our boxes current state ( TRUE = shown, FALSE = HIDDEN )
bool _ShowFirstGroupBox = true;
bool _ShowSecondGroupBox = true;
// We store our default height for groupboxes
int FirstGroupBoxDefaultHeight;
int SecondGroupBoxDefaultHeight;
public TestForm()
{
InitializeComponent();
// Assigning default height of our groupboxes
FirstGroupBoxDefaultHeight = groupBox1.Height;
SecondGroupBoxDefaultHeight = groupBox2.Height;
}
private void button1_Click_1(object sender, EventArgs e)
{
ShowFirstGroupBox = !(_ShowFirstGroupBox); // This sets our property value to opposite of this boolean
}
private void button1_Click_1(object sender, EventArgs e)
{
ShowSecondGroupBox = !(_ShowSecondGroupBox); // This sets our property value to opposite of this boolean
}
}
}
Now when we have code like this and press button it will collapse groupbox.
NOTE: Controls under groupbox are still on place but just hidden since they are child of groupbox and everything outside of bounds is not visible to user.
This is dirty way since i would like to display it much prettier with MINUS sign on the right side of the groupbox title so i do not have button inside it. To do this you would need to create custom control which inherits groupbox, add button to it and position it in title bar and create event for it. It is easy if you have ever tried creating custom controls but if you haven't and you think dirty approach is okay with you then do not try it.
In my VS 2012 WPF project, I have a customized textbox class called TextBoxDX that adds an AutoSelect feature. No problem. I have another class based on TextBoxDX called IntBox which only allows integers. That's where our story begins. Both classes are used in binding situations like so:
<local:TextBoxDX Grid.Row="0" Grid.Column="1" x:Name="txtBox_singlesName" Width="320" HorizontalAlignment="left" Text="{Binding SelectedItem.name, ElementName=listBoxSingles, Mode=OneWay}"/>
<local:IntBox x:Name="intBox_heightin" Width="60" AllowZeroValue="True" MaxLength="2" Text="{Binding SelectedItem.heightin, ElementName=listBoxSingles, Mode=OneWay}" MinVal="0" MaxVal="11"/>
Pretty sure most of that is irrelevant except for the binding. Both work fine in that they change their text corresponding to ListBox selections. But in the IntBox class, I had to add code for allowing only integers. In order to do that, I tapped into the TextChanged event in my IntBox class. The end result is this:
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Herculese
{
public class IntBox : TextBoxDX
{
//INIT PROPERTIES
private int _MaxVal = 0;
private int _MinVal = 0;
private bool _AllowZeroValue = false;
//INIT STRING TO KEEP TRACK OF TEXT BEFORE CHANGES
private string originalText;
public IntBox()
{
//ADD TO TEXTCHANGED HANDLER
TextChanged += new TextChangedEventHandler(My_OnTextChanged);
//STORE ORIGINAL TEXT
originalText = this.Text;
}
//EVENT HANDLER WHEN TEXT IS CHANGED
private void My_OnTextChanged(object sender, EventArgs e)
{
//IF THERE IS TEXT IN THE BOX,
MessageBox.Show("yee");
if (this.Text.Length > 0)
{
//REMOVE SPACES AND LEADING ZEROS FROM STRING
if (!_AllowZeroValue)
this.Text = this.Text.TrimStart('0');
this.Text = Regex.Replace(this.Text, #"\s+", "");
//IF VALUE ISN'T NUMERICAL OR NOTHING IS LEFT AFTER REMOVING ZEROS AND SPACES, CHANGE TEXT BACK TO ORIGINAL
Regex regex = new Regex("[^0-9]+");
if (regex.IsMatch(this.Text) || this.Text.Length < 1)
{
this.Text = originalText;
System.Media.SystemSounds.Beep.Play();
}
//IF VALUE IS NUMERICAL,
else
{
//MAKE SURE VALUE IS WITHIN ACCEPTED RANGE. IF NOT, CHANGE IT TO HIGHEST/LOWEST AVAILABLE RESPECTIVELY
int intText;
intText = Convert.ToInt32(this.Text);
if (intText > _MaxVal)
{
this.Text = _MaxVal.ToString();
System.Media.SystemSounds.Beep.Play();
}
else if (intText < _MinVal)
{
this.Text = _MinVal.ToString();
System.Media.SystemSounds.Beep.Play();
}
//SUCCESS! UPDATE ORIGINAL TEXT WITH NEW VALID VALUE.
else originalText = this.Text;
}
}
}
//PROVIDE GET/SET PROPERTIES
public int MaxVal
{
get { return _MaxVal; }
set { _MaxVal = value; }
}
public int MinVal
{
get { return _MinVal; }
set { _MinVal = value; }
}
public bool AllowZeroValue
{
get { return _AllowZeroValue; }
set { _AllowZeroValue = value; }
}
}
}
As you can see, I had a great time in the TextChanged of my IntBox. Wild parties, you name it. Suddenly I realized that binding for IntBox wasn't working anymore. I could manually change the text just fine. It only accepted integers and worked like a charm. But changing the ListBox selection no longer updated the text. If I removed the code in My_OnTextChanged, the binding worked again. I figured my code was causing the problem. So I came back today with a fresh head and realized something weird.
If I remove the code in My_OnTextChanged and replace it with a MessageBox, the binding works and the message box appears. Makes perfect sense and furthers the idea that my code is causing the issue. Now for the weirdness: if I put the code back into the event after the MessageBox code, binding is broken again and the MessageBox never shows meaning the event never fires. All I can say to that is... HUUUUH?! I've recreated this several times just to be sure I'm not crazy. The only other thing I could think of is a conflict with the TextBoxDX that it inherits from so I made it inherit directly from TextBox and got the same results... Anybody got a clue on this one?
You are assigning a new value to Text which is removing the original binding as you have replaced it with a new string.
Instead of using this.Text = "Somthing" try using base.SetCurrentValue(TextProperty, value);
Example:
//IF VALUE ISN'T NUMERICAL OR NOTHING IS LEFT AFTER REMOVING ZEROS AND SPACES, CHANGE TEXT BACK TO ORIGINAL
Regex regex = new Regex("[^0-9]+");
if (regex.IsMatch(this.Text) || this.Text.Length < 1)
{
// this.Text = originalText;
base.SetCurrentValue(TextProperty, originalText);
System.Media.SystemSounds.Beep.Play();
}
SetCurrentValue Sets the value of a dependency property without changing its value source.
I have a tab control in my WPF application with multiple tabs. Each tab gives access to several buttons, text boxes, drop downs. Now before moving to the next tab valid entries in each of the controls in the tab is to be checked or jumping to the next tab should not be allowed. How can this be done?
I was able to use IsEnable property to do this. But I want it like, when I click on the next tab it should, without entering the next tab, display a warning that such and such entry in the present tab is not valid.
If you adhere to the Selected event you can do something like this:
// Keep a global variable for the previous index
int prevIndex = 0;
private void tabControl_Selected(object sender, TabControlEventArgs e)
{
TabControl tc = sender as TabControl;
if (tc != null)
{
bool letSwitchHappen = validateTabControls(tc.SelectedIndex);
if (!letSwitchHappen)
{
tc.SelectedIndex = prevIndex;
}
prevIndex = tc.SelectedIndex;
}
}
Where validateTabControls is something like:
private bool validateTabControls(int tabIndex)
{
bool validEntries = false;
// Some code here to set validEntries according to the control at tabIndex
return validEntries;
}
Take a look at this example from Josh Smith.
It shows explicitly how to do this, and Josh is well-known (and respected) in the WPF world.
I am programatically adding text in a custom RichTextBox using a KeyPress event:
SelectedText = e.KeyChar.ToString();
The problem is that inserting text in such a way doesn't trigger the CanUndo flag.
As such, when I try to Undo / Redo text (by calling the Undo() and Redo() methods of the textbox), nothing happens.
I tried programatically evoking the KeyUp() event from within a TextChanged() event, but that still didn't flag CanUndo to true.
How can I undo text that I insert without having to create lists for Undo and Redo operations ?
Thanks
I finally decided to create my own undo-redo system using stacks.
Here's a quick overview of how I did it :
private const int InitialStackSize = 500;
private Stack<String> undoStack = new Stack<String>(InitialStackSize);
private Stack<String> redoStack = new Stack<String>(InitialStackSize);
private void YourKeyPressEventHandler(...)
{
// The user pressed on CTRL - Z, execute an "Undo"
if (e.KeyChar == 26)
{
// Save the cursor's position
int selectionStartBackup = SelectionStart;
redoStack.Push(Text);
Text = undoStack.Pop();
// Restore the cursor's position
SelectionStart = selectionStartBackup;
}
// The user pressed on CTRL - Y, execute a "Redo"
if (e.KeyChar == 25)
{
if (redoStack.Count <= 0)
return;
// Save the cursor's position
int selectionStartBackup = SelectionStart + redoStack.ElementAt(redoStack.Count - 1).Length;
undoStack.Push(Text);
Text = redoStack.Pop();
// Restore the cursor's position
SelectionStart = selectionStartBackup;
return;
}
undoStack.Push(Text);
SelectedText = e.KeyChar.ToString();
}
It's just an idea but what if you set the caret position to where you would insert your text and instead of modifying the Text property, just send the keys?
SendKeys.Send("The keys I want to send");
There are bound to be quirks but as I said, it's just an idea.
You can use TestBox.Paste. The documentation in the class overview, saying "Sets the selected text to the specified text without clearing the undo buffer.", seems confusing. I have just tried it and it sets the Undo as expected.
Is spite of its name it has no relation to Clipboard at all, it just replaces the currently selected text with the text you provide as an argument, and therefore seems just to do what the question asks for, in very simple manner.
I have a form containing a TextBox in C# which I set to a string as follows:
textBox.Text = str;
When the form is displayed, why does the text in the texbox appear highlighted/selected?
The text box has a TabIndex of 0 and TabStop set to true. This means that the control will be given focus when the form is displayed.
You can either give another control the 0 TabIndex (if there is one) and give the text box a different tab index (>0), or set TabStop to false for the text box to stop this from happening.
The default behavior of a TextBox in Windows Forms is to highlight all of the text if it gets focused for the first time by tabbing into it, but not if it is clicked into. We can see this in Reflector by looking at the TextBox's OnGotFocus() override:
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) && (Control.MouseButtons == MouseButtons.None))
{
base.SelectAll();
}
}
}
It's that if statement that is causing the behavior that we don't like. Furthermore, to add insult to injury, the Text property's setter blindly resets that selectionSet variable whenever the text is re-assigned:
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.selectionSet = false;
}
}
So if you have a TextBox and tab into it, all the text will be selected. If you click into it, the highlight is removed, and if you re-tab into it, your caret position (and selection length of zero) is preserved. But if we programmatically set new Text, and tab into the TextBox again, then all of the text will be selected again.
If you are like me and find this behavior annoying and inconsistent, then there are two ways around this problem.
The first, and probably the easiest, is to simply trigger the setting of selectionSet by calling DeselectAll() on form Load() and whenever the Text changes:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox2.SelectionStart = this.textBox2.Text.Length;
this.textBox2.DeselectAll();
}
(DeselectAll() just sets SelectionLength to zero. It's actually SelectionStart that flips the TextBox's selectionSet variable. In the above case, the call to DeselectAll() is not necessary since we are setting the start to the end of the text. But if we set it to any other position, like the start of the text, then calling it is a good idea.)
The more permanent way is to create our own TextBox with the desired behavior through inheritance:
public class NonSelectingTextBox : TextBox
{
// Base class has a selectionSet property, but its private.
// We need to shadow with our own variable. If true, this means
// "don't mess with the selection, the user did it."
private bool selectionSet;
protected override void OnGotFocus(EventArgs e)
{
bool needToDeselect = false;
// We don't want to avoid calling the base implementation
// completely. We mirror the logic that we are trying to avoid;
// if the base implementation will select all of the text, we
// set a boolean.
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) &&
(Control.MouseButtons == MouseButtons.None))
{
needToDeselect = true;
}
}
// Call the base implementation
base.OnGotFocus(e);
// Did we notice that the text was selected automatically? Let's
// de-select it and put the caret at the end.
if (needToDeselect)
{
this.SelectionStart = this.Text.Length;
this.DeselectAll();
}
}
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
// Update our copy of the variable since the
// base implementation will have flipped its back.
this.selectionSet = false;
}
}
}
You maybe tempted to just not call base.OnGotFocus(), but then we would lose useful functionality in the base Control class. And you might be tempted to not mess with the selectionSet nonsense at all and simply deselect the text every time in OnGotFocus(), but then we would lose the user's highlight if they tabbed out of the field and back.
Ugly? You betcha. But it is what it is.
The answers to this question helped me a lot with a similar problem, but the simple answer is only hinted at with a lot of other complex suggestions. Just set SelectionStart to 0 after setting your Text. Problem solved!
Example:
yourtextbox.Text = "asdf";
yourtextbox.SelectionStart = 0;
You can also choose the tab order for your form's controls by opening:
View->Tab Order
Note that this option is only available in "View" if you have the Form design view open.
Selecting "Tab Order" opens a view of the Form which allows you to choose the desired tab order by clicking on the controls.
To unhighlight a text field, with VS 2013, try init with:
myTextBox.GotFocus += new System.EventHandler(this.myTextBox_GotFocus);
And add the method:
public void myTextBox_GotFocus(object sender, EventArgs e)
{
myTextBox.SelectionLength=0;
}
I haven't tested this on C# but I ran into the same issue using a C++ WIN32 dialog box. Is seems like you can change the behavior by returning FALSE from OnInitDialog() or WM_INITDIALOG. Hope this helps.
Here is what worked for me
public void SetNotes(string notes)
{
notesTextBox.Text = notes;
notesTextBox.Select();
notesTextBox.SelectionLength = 0;
notesTextBox.SelectionStart = notes.Length;//place cursor at end of text
}