How can I get the caret's position in xaml? - xaml

I have a few textboxs and I want to achieve this funcion: when tap into each textbox, that specific textbox will move to the top and when user type in some data it can auto scroll and keep the cursor|caret always on top of the screen. I know that I can useScrollViewer.ChangeView(null,offset,null); to set the view, however, how can I get the cursor's position (y or vertical offset) though. This is WP 8.1 app.

According to the documentation for the WPF TextBox class here, there is a property called CaretIndex which "Gets or sets the insertion position index of the caret."
You might also be able to use the value of the SelectionStart and SelectionLength properties to find where the caret is, but if the SelectionLength is larger than zero, that might not work. Try the CaretIndex property.

I did not fully understand your requirement. Currently there is no property/method to get the current caret position in the text. But if you want to move your caret at the start of the textbox than use below code.
txtbox.SelectionStart = 0;
txtbox.SelectionLength = 0;
Edit
By using txtbox SelectionStart and textbox selection length you can get caret position.
int caretIndex = txtbox.SelectionStart + txtbox.SelectionLength;

Related

Textbox scroll to bottom in Avalonia

I have a textbox that I write info to that automatically creates a scroll bar when the screen is filled, and I'd like to scroll to the bottom of it. It seems like what I need to do is set the scrollbar Offset to some vector, but my problem is finding the ScrollViewer.
I can't use FindControl because it's not named anywhere in the xaml, and I can only change a few values using textbox.SetValue
The only way to scroll a TextBox currently is to set the CaretPosition. For example to scroll to the end of the text you could use:
textbox.CaretIndex = int.MaxValue;
For me it works only when I set textbox.CaretIndex to the length of the text. Assuming that property Logs (string type) is binded to textbox I used this
textbox.CaretIndex = Logs.Length

Moving label to fit according to length vb.net

Hi I have a button which will add a string in to a label. (this can be done multiple times.) As the user can add multiple strings with each one been a different length, is there anyway I can get it to find the length of the label and those around it so that it spaces out correctly?
Thanks
Label.Width will return the current width of the label control, but it sounds like you already know this. Since the label can be narrower than the text it is trying to display, you would need to measure the full text using the graphics object. This method will return the width of the text in a label:
Private Function getFullTextWidth(lbl As Label)
Using g As Graphics = Label1.CreateGraphics()
Return g.MeasureText(Label1.Text, Label1.Font).Width
End Using
End Function
Alternatively, you could just set the label's AutoSize property to true, and then just check the label's Width property after you set the Text.

Getting the caret position in a RichTextBox

In a .NET 3.5 application, i want to get caret position in a RichTextBox control. The RTB is not XAML. Also, the RTB doesn't have CaretPosition property as described here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition.aspx
What is the simplest way using which i can get the caret position?
EDIT:
To be more specific i want to find out what is the position of the caret from the start of the line on which it is positioned. I can get the line number by using GetLineFromCharIndex(rtb.SelectionStart) but not the offset from the start of the line.
To get the position of the caret from the start of the current line I will try this:
Dim posInLine as Integer = rtb.SelectionStart - rtb.GetFirstCharIndexOfCurrentLine()

is it possible to anchor a control to another control?

Is it possible to anchor a control to another control?
Lets say i want my Button1 to keep its 4px distance from Textbox1 control without
using FlowLayoutTable control?
"Why would be this advantageous?" - one could ask
Well let's say you have a SplitContainer with a vertical splitter and you have Textboxes
in SplitContainer.Panel1 which are anchored to the left and right but their maximum size's
width is smaller than you allow to the SplitContainer.Panel1's width to have (maybe
because you want text to show up there or because additional padding or whatever,you name it)
Now let's say you also have a button next to Textbox1 and you dont want Textbox1 to be
overlapped by the Button1 because its extends to far.
If i want to have my textbox fill the SplitContainer.Panel1 in a fashion that it leaves space for
Button1 control while still both of them are anchored to the right how would i do it?
I extensively use TableLayoutPanels and FlowLayoutPanels to accomplish this. For you specific circumstance I would use a TableLayoutPanel with three columns and a row for each TextBox.
Column 1: Auto-width, contains Labels all with AutoSize = True.
Column 2: 100% width, contains TextBoxes all with Anchor = Left, Right.
Column 3: Auto-width, contains the Button in the appropriate row.
Next, I set all text boxes, except for the one next to the button, ColumnSpan = 2. Then just put the TableLayoutPanel in the SplitPanel and set Dock = Fill.
it will be a sequence in live which should be flow out from left and keep working lets the right side should be layout.
List item safty cares should be provided.
List item all things that use in this method should be provided and be check;

changing the position of textbox using code

In my form I have an AXVS Flex Grid and a Textbox. Firstly the textbox will not be visible.
When the focus is on a particular column of the flexgrid, the textbox will be visible and the Flex Grid will be disabled. The position of the text box will be at the bottom of the particular row at the first time.
But when another row is inserted, then I need to change the position of the textbox to the bottom of the new row and so on.
How can I do that? Thanks in advance.
Position of a control can be retrieved and set using the Top & Left properties of the control.
So you can get the Top,Left property of the inserted row and set the Top,Left property of TextBox accrodingly.
e.g.
TextBox1.Top = InsertedRow.Top +=10
TextBox1.Left = InsertedRow.Left
This will bring the textbox below inserted row.