Getting the caret position in a RichTextBox - vb.net

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()

Related

How can I get the caret's position in 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;

How to get the current token of java editor in a plugin

How do I programmatically get a current token on which there is a caret in a java editor in Eclipse plugin? For example, if the caret is placed on one of characters of a string (say, "text here") I would like to get 'text here' as a value in a eclipse plugin. Is there API for that? I have found an example to get a highlighted selection, but could not find to one that calculates the current token just by offset of the caret.
You can use
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
to get the current active editor.
Then do (after checking that the editorpart is a ITextEditor)
IDocumentProvider provider = ((ITextEditor) editor).getDocumentProvider();
IDocument document = provider.getDocument(((ITextEditor) editor).getEditorInput());
ITextSelection textSelection = (ITextSelection) ((ITextEditor) editor).getSite().getSelectionProvider().getSelection();
int offset = textSelection.getOffset();
int lineNumber = document.getLineOfOffset(offset);
to get the offset and line number, and you could use this information to parse out the relevant text/token from the document. This will work even if no text is selected (i.e. the caret position).

Remove last character from textbox on button click

I have a textbox, textbox1, whose ReadOnly property is set to true. It is somewhat like an on-screen keyboard. When the user presses a button, I can add characters by using:
Textbox1.Text = Textbox1.Text & A
But, when the user wants to remove the text, which should work exactly like the Backspace key, the last character should be removed from the textbox.
I am ready to give up normal textbox and use richtextbox if there is any way ahead with the rich one.
How to remove last character of a textbox with a button?
The previously suggested SubString method is what I would have posted too. However, there is an alternative. The Remove method works by removing every character after the given index. For instance:
TextBox1.Text = Textbox1.Text.Remove(TextBox1.Text.Length - 1)
You can use the SubString method to get all but the last character, for instance:
TextBox1.Text = TextBox1.Text.SubString(0, TextBox1.Text.Length - 1)
The above functions such as Substring and Remove are not executed in Visual Basic 6 (or earlier versions). I recommend to use Mid function.
For instance,in this example last character of Hello is deleted and rest of word is saved in Text1
Text1=Mid("Hello",1, Len("Hello") - 1)
and result is Text1=Hell

Word wrap position in UILabel

When working with a muti-line UILabel object, is there a way to know at which position in the text the line wrapping will occur?
For example if the text of my label is 20 words, the display will be different according to the size of the box provided. I would like to know when (character position) the display is going to the next line.

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.