I want to Split the the text in the RichTextBox per line into array in vb.net
if i show array(0) it will show the first line of richtextbox, array(1) will show the second line and go on.
Im new in VB and I cant find any decent link or site. Thanks in advance for help.
Your Richtextbox has a property called Lines (link). Which returns an String() where each line is a new entry in the Array. More info can be found in the link above.
Code example
Dim myArr As String() = MyRichTextBox.Lines
Note: Searching "Richtextbox to array" (in google) does supply you with plenty of helpful webpages
Related
Hello I want some help with a code to search through the text file and to separate the text from a separator value followed by a rogue(stopping) value.
Each line of text in the text file is: for example with sample data:
DAC11010|This is a Desk and a chair|$100;.
In the above example has the | sign as a seperator value and ;sign as a rogue(stopping) value. The separator value separate the text DAC11010 , This is a Desk and a chair , $100 the rogue(stopping) value means that it it is the EOLN(end of line) and ready to check the next line.
The separated text goes to 3 ListBoxes and the data added one after the other. The DAC11010 goes to the lstItemCode , This is a desk and chair goes to the lstDescription , $100 goes to the lstPrice.
I would like if someone would volunteer to help me program the code in vb.net. I hope I made myself clear.
Thank you [shannon].
If you haven't already done so, please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime.
I will not write the code for you. That is not the purpose of Stack Overflow. We help with specific problems you are having with your code. You don't appear to have any code.
I will explain one way your goal might be accomplished.
Declare 3 New List(Of String) variables to hold the data for the three list boxes. We will be looping through the text files lines but we don't want to update the user interface until after the loop so we will collect the data in memory.
You will need to import System.IO to use methods of the File class. You can use File.ReadAllLines to get an array of strings; each member of the array will contain one line from the text file.
Next, you will want to loop through the lines splitting each line into 3 sections. Use the For Each...Next type of loop. You can use the String.Split method passing in your "|"c parameter. The c tells the compiler that this is a Char not a string because .Split is expecting a Char Now you can use the .Add method to add the sections to the appropriate list.
After the loop, you can assign the 3 list to the .DataSource property of the appropriate ListBox.
Now write the code and if you have any problems ask a new question showing the specific code where you are having the problem.
Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.
Okay so I fell into a loop and got stumped. I have been trying many ways to try and get a VB.net (Label.text) to = each independent line entered. Is there an easier alternative that will not come up with warnings? Just Curious. I could use a Listbox to Manage the Items and use those lines instead of a Textbox in Multi-lined, Just wondering how to get multi line function that reads text from a textbox to a label.text if many lines are entered.
For Example.
If ProgressBar1.Value = 0% Then Label10.Text = (TextBox1.Text + vbNewLine)
Problem with this is that it Addes it all into the Label. Id like a way to have line by line be shown by the Label.text. Sorry If this is coming off confusing to any of you haha.
Thanks
~Tom
First of all you need to make sure that your Label has property 'AutoSize', which must be set to 'False'
Create a string array by spliting the textbox.text using Environment.NewLine.
Then you can select the proper line to display on the label from that array depending on your business logic.
There's one thing that I want to ask. How can I get the whole line of text that contain a string in Visual Basic 2010?
Let's say:
MyText.txt file contains:
Configurations:
Name: Fariz Luqman
Age: 78
My Favourite Fruit: Lemon, Apple, Banana
My IPv4 Address: 10.6.0.5
My Car: Ferrari
In Visual Basic, I want to get the whole line of text that contain the string "Banana" and print it in the textbox so it will display in that textbox:
My Favourite Fruit: Lemon, Apple, Banana
Why am I doing this? Because the text file is being appended and the line number is random. The contents is also random because the texts is generated by Visual Basic. The text "Banana" can be in line 1, line 2 or can be in any line so how can I get the whole line of text that contain certain string?
Thank you in advance!
You can do this easily all in one line with LINQ:
TextBox1.Text = File.ReadAllLines("MyText.txt").FirstOrDefault(Function(x) x.Contains("Banana"))
However, if the file is rather large, that's not particularly efficient, since it will read the whole file into memory before searching for the line. If you want to make it stop loading the file once it finds the line, could use the StreamReader, like this:
Using reader As New StreamReader("Test.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains("Banana") Then
TextBox1.Text = line
Exit While
End If
End While
End Using
Just checked (should have done that first!). VB.Net does have a CONTAINS() method. So:
IF line1.Contains("Banana") THEN
'do something
END IF
What's the best way to read in a the contents of a textbox line by line in VB.net 2.0?
I'm currently using the one listed at TextBoxBase.Lines Property at MSDN but wanted to know what other ways could one read in a textbox line by line in VB.net.
It might not be the best way, but you could always use TextBox.Text.Split(vbNewLine), which will return an array of string. You could use that in a for each loop
For Each strLine As String In TextBox.Text.Split(vbNewLine)
...
Next
The Lines property is what you want to use. Parsing the Text property yourself requires more code and doesn't make it faster. Write a better question if you have a reason to look for an alternative.