How to highlight entire TextBox on triple click? - c#

When there are spaces between the text in the Input TextBox, my triple click only highlights up to the next space instead of the whole TextBox. Help?

Subscribe to the DoubleClick event:
private void textBox1_DoubleClick(object sender, EventArgs e)
{
textBox1.SelectionStart = 0; // set the selection start index to the beginning
textBox1.SelectionLength = textBox1.Text.Length; // set the selection length to the length of the text
}
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.doubleclick?view=windowsdesktop-7.0

Related

(C#) Getting the index from a remove char of a TextBox WinForms

I have a TextBox where the user can add and remove text, however each char relates to a row on a DataGridView, with a variety of user-selectable options. So knowing what char is being removed is very important because the DataGridView needs to know what row must be removed.
At first, I had a simple string-compare method, but with duplicate char sequences (e.i. "aaaa") it couldn't figure out which letter was removed and defaulted to returning the index of last char in the sequence. So I went online to see if there was a way to track the text caret's position, and there is...but not for WinForms. The only aspects I found for the Caret was SelectionStart, SelectionLength and SelectionText; which will be usefully for batch remove, but not when the user hits the backbutton/deletebutton.
I'm pretty stumped right now. The "easiest" solution is switching to XAML because it tracks the Caret Position... but that feels like quiter talk. Though with that said, I still have no idea how to tackle this problem.
You can try to define the variable originalText to save origianl textbox text and textlength to save text length.
Subscribe to textBox1_Enter to give them the initial value.
private void textBox1_Enter(object sender, EventArgs e)
{
textlength = textBox1.Text.Length;
originalText = textBox1.Text;
}
Then subscribe to textBox1_TextChanged to get the deleted chars.
int textlength;
string originalText;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textlength > textBox1.Text.Length)
{
Console.WriteLine($"You deleted the char from {textBox1.SelectionStart} to {textBox1.SelectionStart + textlength - textBox1.Text.Length - 1}");
Console.WriteLine($"deleted substring {originalText.Substring(textBox1.SelectionStart, textlength - textBox1.Text.Length)}");
}
// reset
textlength = textBox1.Text.Length;
originalText = textBox1.Text;
}

How to move caret to the beginning of text inside a Textbox?

I tried using this code but It doesn't work
private void textBox1_Enter(object sender, EventArgs e)
{
this.textBox1.Select(0, 0);
}
I want whenever the user click on the textbox, the caret position will be at the beginning of text instead of being in the position when user clicked ?
How to move caret to the beginning of text inside a Textbox ?
Use the MouseClick Event :
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Select(0, 0);
}
Note that this will not work if you enter the TextBox through Tab.
You can use SelectionStart and SelectionLenght property . for example ,
SelectionStart = 0;
Selectionlenght = 0;
You can use these code in Enter event .

WPF (with C#) TextBox Cursor Position Problem

I have a WPF C# program where I attempt to delete certain characters from a text box at TextChanged event. Say, for instance, the dollar sign. Here is the code I use.
private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
string data = txtData.Text;
foreach( char c in txtData.Text.ToCharArray() )
{
if( c.ToString() == "$" )
{
data = data.Replace( c.ToString(), "" );
}
}
txtData.Text = data;
}
The problem I have is that whenever the user enters $ sign (Shift + 4), at the TextChanged event it removes the $ character from the textbox text alright, but it also moves the cursor to the BEGINNING of the text box which is not my desired functionality.
As a workaround I thought of moving the cursor the the end of the text in the text box, but the problem there is that if the cursor was positioned at some middle position then it would not be very user friendly. Say, for instance the text in the textbox was 123ABC and if I had the cursor after 3, then moving the cursor to the end of the text would mean that at the next key stroke user would enter data after C, not after 3 which is the normal functionality.
Does anybody have an idea why this cursor shift happens?
Its not an answer to your question, but probably a solution for your problem:
How to define TextBox input restrictions?
If it is overkill for you, set e.Handled = true for all characters you want to avoid in PreviewKeyDown (use Keyboard.Modifiers for SHIFT key) or PreviewTextInput.
Try TextBox.CaretIndex for restoring cursor position in TextChanged event.
Hope it helps.
You can use the Select function of TextBox to change the cursor position.
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox1.Text.Replace("$", "");
textBox1.Select(textBox1.Text.Length, 0);
}
You can see more about Position the Cursor on the MSDN
You can use the SelectionStart property of the textbox. Probably something along these lines should work:
private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
var pos = txtData.SelectionStart;
string data = txtData.Text.Replace("$", "");
txtData.Text = data;
txtData.SelectionStart = pos;
}
You can try Regular Expression
Sample
1) Use PreviewTextInput="CursorIssueHandler" in .xaml file
2) In your .cs file ,write the below code:
private void CursorIssueHandler(object sender, TextCompositionEventArgs e)
{
var TB = (sender as TextBox);
Regex regex = new Regex("[^0-9a-zA-Z-]+");
bool Valid = regex.IsMatch(e.Text);
//System.Diagnostics.Debug.WriteLine(Valid); // check value for valid n assign e.Handled accordingly your requirement from regex
e.Handled = Valid;
}

Control the position of the vertical scrollbars

I have a multi-line textbox (let's call it textBox1) that has plenty of text inside.
After doing a search, I highlight the string I was looking for with:
textBox1.SelectionStart = textBox1.Text.ToLower().IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
Now when I call the form that contains the textbox it highlights the selected text, but what I would like to do is that the scrollbars would scroll automatically to the highlighted text.
I tried
textBox1.ScrollToCaret();
But didn't work.
Any ideas?
Thank you.
What event are you firing this from? The Form probably isn't in a state where it can process this. If you call from Form.Load it will be too soon. If you call from Form.Shown, it should work properly.
private void Form1_Shown(object sender, EventArgs e) {
var STRING = "Suspendisse mi risus";
textBox1.SelectionStart = textBox1.Text.IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
textBox1.ScrollToCaret();
}

Keep selection when clicking into textbox

I need to have the text in a TextBox become selected when a user clicks into the box. If the text is already selected, it needs to be a regular cursor. So on the click event of all the textboxes I have this code:
TextBox t = (TextBox)sender;
bool alreadyselected = t.SelectedText == t.Text;
if (!alreadyselected) t.SelectAll();
the problem is, by the time the click event is reached, t.SelectedText is empty
so the full text always becomes selected even when clicking multiple times
I would appreciate a solution that can be for all the textboxes at once if possible
You're correct, the default Click for the TextBox is changing the position of the caret and thus clearing any selected text. But you can restore it.
First add 2 int vars to store the selection Start and Length and initialize Start as -1 to signal not set:
private int SelectedStart = -1;
private int SelectedLength = 0;
then make a handler for the TextBox's Leave event and save the Start and Length for the currently selected text when we lose focus.
private void textBox1_Leave (object sender, EventArgs e)
{
SelectedStart = textBox1.SelectionStart;
SelectedLength = textBox1.SelectionLength;
}
Finally, make a handler for the TextBox's Click event and, if we previously saved the Start and Length, restore them to the TextBox and then set Start to -1 to signal not set again (this allows for normal click behavior within textbox when it is focused).
private void textBox1_Click (object sender, EventArgs e)
{
if (SelectedStart != -1) {
textBox1.SelectionStart = SelectedStart;
textBox1.SelectionLength = SelectedLength;
SelectedStart = -1;
}
}
Use the Control.Tag property to set a bool flag to select or deselect the TextBox text:
private void TextBox_Click(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.SelectionStart = 0;
// First click will select the text
if (txtBox.Tag == null)
{
txtBox.Tag = true;
txtBox.SelectionLength = txtBox.Text.Length;
}
// Second click will deselect the text
else
{
txtBox.Tag = null;
txtBox.SelectionLength = 0;
}
}

Categories