Select specific text from a TextBox on Tapped event - c#

This is a UWP app.
I'm trying to select text with a single tap while having buttons that allow a user to differentiate if they want to select a word, sentence, paragraph, or page. I can get the pointer position and the text inside the textbox but I can't find any reasonable way to associate the two.
Is there a property or method that I'm missing which will allow me to get perhaps an index position in the textbox where the pointer currently is?

If you do not have anything selected currently (SelectionLength = 0), then the SelectionStart Property will return the index of the item that matches your cursor position.
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.textbox.selectionstart?view=winrt-20348

Related

How to select right text with RichTextBox.Select in C#

I have a RichText in my RichTextBox.
If i print textBox.Text.Length it is shorter than if I print textBox.TextLength.
I want to index search inside the textBox and find a certain string to select, but since they missmatch in length, if i use a textBox.Text.IndexOf("bla"), i get wrong position.
How do I do to get the right position to select?
Try this:
RichTextBox1.Select();
RichTextBox1.Select(RichTextBox1.Text.IndexOf("bla"), "bla".Length);

select listViewItems in listView as text

In ListView there are 3 option of SelectionMode
1.Single - only one item can be selected.
2.Multiple - you can select muliple items, one after the other.
3.Extended - You can select multiple items and used Ctrl or Shift key.
I'm need to select some items in ListView as text in TextBox.
i.e. press with the left button of mouse, until the mouse is up.
and mark all between items as Selected.
How can I make it?
Thanks
1.Single: SelectionMode="Single"?
2 Mutiple : i think use binding
enter link description here
3
enter link description here
Or You try this line SelectionMode="Muti..."
Firstly, the porpose of this problem is to display listView of TextBlocks for allow use ItemsSource of ListView for display text for several reasone.
behind each textBlock that contains a word, there are in the ViewModel class named Word. that contains the text of the word and property of IsSelected.
I solve this problem, by adding 3 EventSetter event to the ListViewItem,
1.PreviewMouseLeftDown
2.MouseEnter
3.PreviewMouseLeftUp
and adding a flag of IsInSelection, and two object Word that present the control in the view,1.firstSelectionWord, 2.lastSelectionWord.
and, when the first event raise, i update the current control to be Selected.
and set a flag IsInSelection to true. and set firstSelectionWord = lastSelectionWord = current word pressed.
in the MouseEnter event i checked if IsInSelection is true, and them mark also the current control to Selected=true. set the lastSelectionWord = current word pressed.
and call a method that mark all the Word between them as selected.
in the PreviewMouseLeftUp function, i set the IsInSelection = false.

Get position of selected text in string in textbox

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

Using keystrokes to select DataGridView row

Hello I have a datagridview with many rows. I would like to be able to click on a cell and type some letters and have a matching row get selected (or scroll to it). I can sort out which column it is sorting to, but right now there is no 'search' capability.
When you open a Window's Explorer window and click on a file, you can start typing to have the selection move to the file/folder that matches your keystrokes.
Is this possible with the datagridview?
What I meant was the DataGridRow.IsSelected property. Since you said you know which rows contains your results, you can call DataGridView.ClearSelection() and then set the IsSelected property of the row that contains the data to true.

C# WinForms need to automatically align a set of labels inside a control in a simple topleft fashion

I want to make a gui for what i explained here C# visual control for editing statements / equations / conditions?
Basically its an expression editor. Each expression consists of a list of stuff, which can be a text string or a parameter. For example, an expression:
If x is greater than 0
consists of:
String "If"
Parameter "variable" (= "x")
String "is"
Parameter "comparator" (= "greater than")
Parameter "value" (= "0")
So, when user wants to edit such expression, i must create (dynamically) five labels, and place them inside the control (Panel) and add onclick events to those of them that arent just strings, so that user can open a window to change the comparator or the variable name etc. The labels must obviously arrange themselves inside the control they are bound to.
Thing is, i dont know if there is already a way to do it automatically. I'd like those labels to arrange themselves just like the words arrange on this page. While it fits, put it to the right of the previous label, when it doesnt, put it on the start of the next row.
Do i have to manually move them OnResize() of the control they are in, or is there an automated way to do it?
Thanks!
Try using a FlowLayoutPanel
//Sample:
//Assuming you are creating your labels from
//List<string>
List<string> labels=new List<string>();
labels.Add("If");
labels.Add("variable");
labels.Add("=");
labels.Add("5");
for (int i = 0; i < labels.Count; i++)
{
Label lbl = new Label();
lbl.Text = labels[i];
flowLayoutPanel1.Controls.Add(lbl);
}
Take a look a the FlowLayoutPanel.
From the docs:
The FlowLayoutPanel control arranges its contents in a horizontal or
vertical flow direction. Its contents can be wrapped from one row to
the next, or from one column to the next. Alternatively, its contents
can be clipped instead of wrapped.

Categories