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.
Related
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.
I'm trying to increment specific elements by 1, in order to log results as they come in. I'm trying to read an element, add 1 to it, and then write it back to the same memory address. Why isn't this simple?
In code it would be something as simple as;
array1[element1] = (array1[element1]+1)
or
array1[element1]++
Arrays seem to be either read (indicators) or write (controls)? This is really frustrating, and there's very little help online.
You can use an "Array index/replace" element inside a "In place element structure":
You should use ReplaceArraySubset in the Array palette. For simple replacements, it's much faster than the In Place Element Structure
As an infrequent, novice Labview user I have the same problem ... until I found the code that I used 10 years ago. Surely the answer to sgccarey is:-
Right click on the array control or indicator and 'create local variable'
This variable will appear on the block diagram and can be set as 'Change to Write' or 'Change to Read' as necessary to use as the input and / or output array to a simple 'replace array subset'.
This way the array data only appears once on the Front Panel and is updated as required.
I have no idea if using Local Variables affects runtime efficiency but it works for me. Hope this helps.
LogEvents(System.Text.Encoding.UTF8.GetString(queryPlaces.ToBson))
I want to be able to output not just the content of System.Text.Encoding.UTF8.GetString(queryPlaces.ToBson) but also the actual string of System.Text.Encoding.UTF8.GetString(queryPlaces.ToBson) itself, perhaps with line numbers and file names.
I know that I can do this easily with objective-c. How can I do that with .net?
I've heard that that's what reflextion is for. But how?
There is no macro in vb.net right?
EDIT: Accepted answer points out what my issue was. I added another answer which shows an even easier way.
I have a multiline textbox from which I create a single string:
Dim wholeThing As String
Dim line as String
For Each line In txtMultiline.Lines
wholeThing = wholeThing & line & Environment.Newline
Next
What I'd like to do is write this to a DB as-is by adding wholeThing as a parameter using to SqlCommand.Parameters.AddWithValue, but all of my painstakingly-inserted newlines go away (or appear to).
I saw this and was hoping I didn't need to concern myself with CHAR(nnn) SQL stuff.
Any suggestions? Thanks!
What do you mean when you say "appears to"? A newline character can be easily stored in an nvarchar field. What tools are you using to verify the results of your actions?
It's even easier than this. As per the discussion in the accepted answer, I wasn't seeing all of the lines in my view. Putting up a messagebox showed everything.
What's more, I don't need to take things line by line:
wholeThing = txtMultiline.Text
works, too, and it keeps all of the line breaks.