Text Is Selected After File Read Into A Textbox - vb.net

So after I run the following code, all my text is highlighted in the textbox.
Dim ioFile As New System.IO.StreamReader("FilePath")
TextBox1.Text = ioFile.ReadToEnd()
Is there a way to not select all the text? BTW, its a multiline textbox.

Use the Select Method to remove the selection.
'Cursor at beginning of text.
TextBox1.Select(0, 0)
'Cursor at end of text.
TextBox1.Select(TextBox1.Text.Length, 0)

Related

How to get the Selected Text from Active Control in VB.NET?

Is there a way to get the selected text or highlighted text only from the Active Control? Active Control doesn't have .SelectedText option, so I used .Text
Example in the image.
I only highlighted "Rus" from the EnhacedTextBox.
ActiveControl.Text contains "Russia".
How do I get the SelectedText "Rus" to be set in Clipboard.SetDataObject() for copying?
Thanks a lot for your opinions and suggestions.
Do you mean you want to get selected text of a textbox ? If so,you can use TextBox.SelectedText property.
I am not sure if you are looking for this but if no, then i assume you are generating multiple textboxes from code behind/during design time ? If so, then try the following code to get the active textbox :
Private Sub GetTheText()
If Me.ActiveControl.[GetType]() = GetType(TextBox) Then
Dim textBox As TextBox = CType(Me.ActiveControl, TextBox)
Dim mytext = textbox.SelectedText
End If
End Sub
Hope this helps you
m_strGetText = Me.m_udtNavigationController.TemplateKeyAss.PrimaryTask.ActiveControl.Text.ToString()
Dim trial As EnhancedTextBox = TryCast(Me.m_udtNavigationController.TemplateKeyAss.PrimaryTask.ActiveControl, EnhancedTextBox)
Dim trial2 As String = trial.SelectedText().ToString()
Solution from #jmcilhinney.
trial2 now contains the Rus selected text. Thanks.

Dynamically assign text to text box in the loop

I want to assign Text to the textBox in the look, I tried
Dim textBoxHB As TextBox = FindName("txt_HB_" + iRecCnt.ToString())
Me.Controls(String.Format("txt_HB_" + iRecCnt.ToString()).Text = .HouseBill
My Text box name change form txt_HB_1 ,txt_HB_2 and so on, and i want to where iRecCnt has 1,2.. values and Text is coming form .HouseBill
Is there any other way i can try?
Replace the loop with this:
Dim boxes = Me.Controls.OfType(Of TextBox).Where(Function(b) b.Name.StartsWith("txt_HB_"))
For Each box As TextBox in boxes
box.Text = .HouseBill
Next

Force a MultiLine TextBox Horizontal ScrollBar to the left

I have a MultiLine TextBox that is updated over a period of time as an app runs, and I've managed to make it so that the TextBox scrolls to the bottom, ensuring that the latest entry is always shown.
However, sometimes the text is quite long and goes off of the side of the TextBox, so the Horizontal ScrollBar scrolls to the right.
How can I amend the code below so that the ScrollBar is always to the left, meaning that the beginning of lines is always visible? Please note that I do not wish to wrap text, as I can't have one entry on multiple lines. Thanks.
Private Sub UpdateCurrentProgress(ByVal Text As String)
If Text = "" Then Exit Sub
Dim Textbox As TextBox = Me.txtCurrentProgress
If Textbox.Text <> "" Then Text = vbCrLf & Text
Textbox.AppendText(Text)
Textbox.Select(Textbox.TextLength, 0)
Textbox.ScrollToCaret()
End Sub
You can select the first char at the current line like this:
Me.TextBox1.Select(Me.TextBox1.GetFirstCharIndexOfCurrentLine(), 0)
If I understand your problem correctly, then you need to get first the last line index and then select the first char of that line.
Dim lineNumber = textBox1.Lines.Count()-1
textBox1.Select(textBox1.GetFirstCharIndexFromLine(lineNumber), 0)

Get RichTextBox Format and use it for a replace function

I just started to learn VB and try to make a little WYSIWYG-HTML Editor. For that i already made a RichTextBox in which the user is able to change colour, fontsize etc.. Now I want to add for example a <b> -Tag before and a </b> -Tag after a word which is written in bold style, save it in a string and give back the new string in a second read-only-Textbox.
What's the best way to do this?
So you want to set read-only textbox's text to the richtextbox's text and then replace certain string with other strings?
If so, here's some code I wrote up. It's probably not the best but it works.
TextBox1.Text = RichTextBox1.Text 'Copies the text form the richtextbox to the normal textbox
If TextBox1.Text.Contains("<b>") Then 'Checks to see if Textbox1 contains the string "<b>"
TextBox1.Text = TextBox1.Text.Replace("<b>", "[b]") 'Replaces <b> with [b]
End If
If RichTextBox1.Text.Contains("</b>") Then 'Same thing as above but this checks to see if it contains "</b>"
TextBox1.Text = TextBox1.Text.Replace("</b>", "[/b]")
End If

"Caret Position" in VB.NET for syntax highlighting

I'm trying to make a TextBox with syntax highlighting (for (HTML/CSS) in VB.NET 2008.
I figured that if I use RichTextBox.Find(), I can color specific text, but then I need to call RichTextBox.DeselectAll().
The problem is that the the cursor jumps to the beginning of the RTB.
I'm using WinForms.
Any ideas?
You can get and set the cursor position using the SelectionStart property.
Therefore, you can write,
Dim selStart As Integer = rtb.SelectionStart
'Do things
rtb.SelectionStart = selStart
Imports System.Text.RegularExpressions
Public Class Form1
'Create a Html Keyword Regex
Dim htmlkeywords As New System.Text.RegularExpressions.Regex("<html>|</html>|<head>|</head>|<meta|<p>|</p>|<div>|</div>") <----add as many terms as you like between the () don't forget the pipe symbol between each term in the Regex.
'Then in your Richtextbox textchanged event add this
Private Sub rtText_TextChanged(sender As Object, e As EventArgs) Handles rtText.TextChanged
Dim selStart As Integer = rtText.SelectionStart
Do Until False
For Each keyWordMatch As Match In htmlkeywords.Matches(rtText.Text)
rtText.Select(keyWordMatch.Index, keyWordMatch.Length)
rtText.SelectionColor = Color.Purple
rtText.SelectionStart = rtText.Text.Length 'this puts the caret at the end
rtText.SelectionLength = 0 ' of the word
Next keyWordMatch
Exit Do
Loop
rtText.SelectionColor = Color.Black
rtText.SelectionStart = selStart ' this makes sure that if your caret is behind a word and you press enter to move a text down a line; the caret will stay in position on the next line that you start typing on. You can remove this code to see what I'm talking about
End Sub
rtText is my RichTextBox name. This will change the word you want to whatever color then change it back to black, which you can change which colors do what. Hope this helps!