Quick fix, Read text in a text box? - vb.net

I have a simple text reading code for Visual Basic:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(fileReader)
I have used this in the past, but I usually make the text display in a text box. I know this is sort of a "newb" question but I can't remember how to display the text in a textbox. If you guys could help me out that would be great!

You'd just do:
textBox1.Text = fileReader
This puts the contents of the string "fileReader" into the TextBox's Text, provided you're doing this from within your Form, and you have a text box on the form named "textBox1".
Also, make sure that your text box is set to be a multi line textbox, if your file has more than a single line within it.

myTextBox.text = fileReader

Related

How to search only the first line of a multiline textbox in VB.NET

Is there any way to search only the first line of a Multiline Textbox without knowing exactly at what position the text is you're looking for?
If I knew the position of the text I was looking for I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Substring(0,4) = myText Then
' Do Something
End If
Or if I wanted to search the entire textbox I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Contains(myText) Then
' Do Something
End If
But I want to search only the first line of the textbox and I'm not sure at what position the text may be. Is there anyway to do a search like that?
This is another example of why you should ALWAYS read the relevant documentation. If you had read the documentation for the TextBox class then you'd know that it has a Lines property. To get the first line of text, you simply get the first element of that array:
Dim firstLine = myTextBox.Lines(0)
If Not filrstLine.Contains(myText) Then
'Do something
End If
Note that this only applies where the user has explicitly added a line break to the text. I assume that that is what you want, given that you have accepted another answer that does the same thing. If you mean the first line based on automatic word-wrap then that requires a bit more effort.
You could take the text and extract the first line.
int pos = text.IndexOfAny('\r', '\n');
if (pos >= 0)
text = text.SubString(0, pos);
// text now contains only the first line
Then you can search the resulting string.

How to display image in text box in vb.net?

I have this code
Dim sb As New StringBuilder
For Each c As String In TextBox2.Text
sb.AppendFormat("<img src='{0}.jpg'/>", c)
sb.AppendLine(Line)
Next
Textbox3.Text = sb.ToString()
Suppose i get image inside my textbox3 but it didn't execute. It just come out the url of the image. how I'm going to get it. I had used literal.text but my image doesn't execute line by line i want my image execute like image below
Try to use a WebBrowser control to display your images:
Dim sb As New System.Text.StringBuilder
For Each c As String In Me.TextBox1.Text
sb.AppendFormat("<img src='{0}.jpg'/>", c)
Next
Me.WebBrowser1.DocumentText = sb.ToString
This is a simple code to start your project.
To obtain the same result in your image I think you also had to:
declare a better HTML and pass it to WebBrowser.DocumentText
use a multiline textbox and handle the carriage return
if you want the same effect, you could take a vertical slice of your composite image, set it as the css background-image of the textbox, with background-repeat:repeat

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

vb.net scan installation folder for rtf documents

Does someone know a code so that my program looks in the map where it is installed for rtf documents and show them in a combobox. I am making an agenda.
It can search for "VPA event -" in the title too (that is what all the event names start with). If i have the code to d that then i can use this one to read the events
Dim objreader2 As New System.IO.StreamReader(ComboBox1.Text & ".rtf")
RichTextBox2.Text = objreader2.ReadToEnd
objreader2.Close()
thanks
Here's the code you can use to load the comboBox. After that, you can use the SelectedIndexChanged event (or other events) to read the file into the rich text box. I didn't test this, but it should be pretty close.
dim ss() as string
fPath = System.Windows.Forms.Application.UserAppDataPath
' or fPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments, or other directory
ss = Directory.GetFiles(fPath, "*.rtf")
ComboBox1.items.clear()
for each string s in ss
ComboBox1.Items.add(s)
next s

Can I stop my textbox from crashing when loading in too much data into it?

Not sure if this is something really simple that I haven't noticed. Or just a simple try/catch (I guess most likely).
I have a textbox on a form. It takes in data from another text file which is very large (lets assume 1Mb) and in a lot of cases it can take it but in some cases the program crashes when loading the file.
How can I handle this?
You could try to read the file size of the text file before loading it into your text box. If the length of the file is greater than the capacity of your text box, you can prevent the text box from being filled.
You can use a function like File.ReadAllLines to determine the size of the text file.
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
For Each fileSystemInfo As System.IO.FileSystemInfo In _
SourceDirectoryInfo.GetFileSystemInfos
Dim FileText As String = System.IO.File.ReadAllText _
(AppDataLocation & fileSystemInfo.Name)
Next
Alternately, you can use FileInfo.Length as seen in this example here.
Dim file As New FileInfo("file.txt")
Dim sizeInBytes As Long = file.Length