VB.NET textbox to listbox new line - vb.net

I have a list of 4 digit extensions that I'm pulling out of my server
cmd = sshClient.RunCommand("asterisk -x 'sip show peers' | grep -w OK | awk '{print $1}' | awk -F'/' '{print $1}'")
TextBox1.Text = cmd.Result
TextBox1 does fill up with the extension list, but it is all in 1 line
(btw, if I change it from TextBox1 to Label1, then I get them in separate lines)
Then, with button2 (to get the list into a ListBox) I have
Dim ext As String
ext = TextBox1.Text
ListBox1.Items.Add(ext)
But I get the list in 1 item and no spaces (it doesn't mater if it is from the TextBox or Label)
I tried adding
If TextBox1.Text.Length > 4 Then TextBox1.Text = TextBox1.Text.Insert(4, Environment.NewLine)
But it adds a new line for the first 4 digits (1 extension) only, the rest are in 1 line
Any idea?
Thanks

The string data returned by asterisk is probably separated by line feeds (ASCII value of 10). Split the return string on Chr(10) and use the .AddRange(ext) function to add all of the items to the listbox:
ListBox1.Items.AddRange(ext.Split(New Char() {Chr(10)}, StringSplitOptions.RemoveEmptyEntries))

The answer will vary a bit depending on whether you are using WinForms, WPF, etc - I am assuming WinForms.
Check that your textbox is set to Multiline = true - if not, that may fix your first problem. But more fundamentally, #LarsTech is on the right track. Assign cmd.Result to a new string variable (instead of directly to TextBox1.Text) then split it into a string array with String.Split(), then loop through the array adding each one separately to your ListBox individually. You won't be able to get multiple items into a Listbox with a single Items.Add statement regardless of how you handle your line breaks.
If that is all that your loop is doing, then you can use ListBox.AddRange() to add the whole array at once.

Related

How to have a text box contain a character position under each character

I have a text box on an MS-Access 2007 Form (its old, but its what I have) called text_record. I want to display the characters in the text box with a positional line directly underneath the text. e.g.
ABCDEF
123456
I used the following code to set the field:
dim x as string
x = Forms![mapped field names].Form![text_record] & vbcrlf
For i = 1 To Len(x) - 1
x = x & CStr(i Mod 10)
Next
Forms![mapped field names].Form![text_record] = x
The code works and shows the integer positions underneath, but the characters are not aligned directly above the numbers. I need the characters directly above the positional number so a user can see what is in a particular column.
I tried several different font types, all said they were monospaced, and they all fail to align directly above the number. Depending on the text in the record, the position was off as much as 5 columns.
I must be missing something simple.
Thanks in advance for any and all responses
Use a fixed font. Courier New is a fixed size.
So, on the form, I drop in a text box, and right below another text box
(I set the border to transparent, and I set enabled = false (so user can't edit, or tab into).
so we have this:
And the font for both text boxes is this:
And my code (it puts numbers below as you type).
Private Sub Text0_Change()
Dim sResult As String
Dim i As Integer
For i = 1 To Len(Text0.Text)
sResult = sResult & i Mod 10
Next i
Me.Text2 = sResult
End Sub
So, we see this (and I typed in "i" and "l", since they are small compared to numbers, and we get this:
so, not sure what font you used, but Courier new is a fixed font, and should work as per above.
You might be better off splitting the text and putting it into a multi-column list box.

how to add certain number of blank lines into multiline textbox in vb .net?

Normally, a multi-line textbox does not contain any lines.
But I want to add a certain number of blank lines to the text box.
I have no idea how I can do this.
The Lines property of a TextBox is an array containing the lines of text. Just assign to it a String array of the appropriate length:
TextBox1.Lines = New String(lineCount - 1) {}
I haven't tested but I wouldn't expect that you would even have to set the elements, i.e. an array full of Nothing will do the job.

VB notepad spliter

So I just started a new program and in this program I need a feature that will basically take a text file or just a richtextbox, and then equally split it. So lets say I have a file with 200 lines of text. Then I want to split it up into 10 files of 20 lines. Please help I need some type of direction! -thanks
Count the textbox length by (textbox1.text.length)i.e.=200 character and divide it by 2 = 100 then make a loop which goes to 200 then keep condition in loop body if it equal to 100 put it to string variable and write it to file and so on and so forth...
First of all create a string array
Dim S() As String = TextBox1.Text.Split(vbNewLine)
this will allow you to put every line in an item of the array named S
to get the lines count use
S.Length
OR
S.Count()
now you got the lines and the lines' count all you have to do to divide them and go from there...

How to get the whole line of text that contain a string

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

adding characters into a datagrid in vb.net

I'm new to vb.net. I would like to know how i could i/p a string via textbox and display its individual characters into a datagrid view in diff rows?
Are you only using a single column in the datagridview? if so you could do this easily by stepping through each character in the string and adding a row containing that character... see the code example below
InputString = TextBox1.Text
For a = 0 To InputString.Length - 1
Datagridview1.Rows.Add(InputString(a))
Next
Cant see why you'd want to do this though - If this isn't what you're trying to accomplish please give us more details