Jump to content

VB Multiline TextBox Line Numbers

Dim InputFromUser As String = ""
For j As Integer = 1 To InputTextBox.Lines.Length
	InputFromUser = InputFromUser & j & ".   " & InputTextBox.Lines(j - 1) & vbNewLine
Next
OutputTextBox.Text = InputFromUser

Sample output by putting the above code in "InputTextBox_TextChanged"

Capture.jpg.8955bc2f766a3f77615d3024d467ac26.jpg

 

This was a problem I had a few weeks ago and couldn't find a solution online and came up with this solution after some trail and error. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Franck said:

what is the problem ?

The way I read this, I think OP is showing people you can use a for-loop to get a list.

A sort of basic 'how to' I guess.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

You could have used  Split()  on the newline character , then go through the array and add the number and dot in front of each line then  do a join() on the array

Also technically, you could have a text document written on a Linux or Mac where they use a single invisible character to signify a new line, instead of two.

Windows uses CRLF  (carriage return and line feed), character codes 0x0D and 0x0A while Mac and Linux often use only 0x0A

 

If you load a text file you may get a single long string instead of multiple lines.

 

Here's example of proper way to do it, in php ... i wrote in comments equivalent functions in VB where i remembered them

 

<?php

$CR = chr(0x0D);
$LF = chr(0x0A);

$text = file_get_contents(__DIR__ .'/a.txt'); //read a.txt from drive

$lines = explode($LF,$text);  // equivalent of split() in VB
$linesCount = count($lines);  // length of array

$lines[0] = (1).'.  '.$lines[0]; // add 1 to the first line
if ($linesCount>1) {
	for ($i=0;$i<$linesCount-1;$i++) {
		$lastCharacter = "";
		$lineLength = strlen($lines[$i]);
		// get last character from string : mid in visual basic
		if ($lineLength != 0) {
			$lastCharacter = substr($lines[$i],$lineLength-1,1);
			if ($lastCharacter==$CR) $lines[$i] = substr($lines[$i],0,$lineLength-1);
		}
		// build the string on the next line (add enter, number dot and space) in front
		$newContent = '';
		if ($lastCharacter==$CR) $newContent .= $lastCharacter;
		$newContent .=  $LF.($i+2).'.  '.$lines[$i+1];
		// overwrite next line with the new content
		$lines[$i+1] = $newContent;
	}
}
// now merge the lines into a big string - you jave Join in VB

$newText = implode('',$lines);
echo $newText;
?>

 

code_and_text_file.zip

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, minibois said:

The way I read this, I think OP is showing people you can use a for-loop to get a list.

A sort of basic 'how to' I guess.

what is OP?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, bindydad123 said:

what is OP?

Original Poster (so you, in this case).

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Then if it's just for formatting a second textbox Your code is okay. There is a one liner to if you prefer. What it does is convert the line text as it grabs it and set it to the other control directly.

// format lines to be [line number]. [text value].
OutputTextBox.Lines = InputTextBox.Lines.Select((value, index) => $"{index}. {value}").ToList();

 

Although your question / problem is very vague, Since you mention you have a problem and posted that you solved it and are unhappy with i assume this is not want you want but i don't know what you want. You want line number automatically into 1 textbox ? want the line number to refresh in the second textbox ? both are totally different solutions.

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, Franck said:

Then if it's just for formatting a second textbox Your code is okay. There is a one liner to if you prefer. What it does is convert the line text as it grabs it and set it to the other control directly.


// format lines to be [line number]. [text value].
OutputTextBox.Lines = InputTextBox.Lines.Select((value, index) => $"{index}. {value}").ToList();

 

Although your question / problem is very vague, Since you mention you have a problem and posted that you solved it and are unhappy with i assume this is not want you want but i don't know what you want. You want line number automatically into 1 textbox ? want the line number to refresh in the second textbox ? both are totally different solutions.

Sorry, I guess I wasn't clear enough. I am satified with my solution (to a problem I had a few weeks ago) and just wanted to share my solution. 

Link to comment
Share on other sites

Link to post
Share on other sites

bindy, ty for posting! I just want to encourage you to make these posts in the future. Posts like this are absolute gold to find on google. There's been a handful of posts I've found like this over the years and they have been really flippin' helpful. So for the future coders out there, please keep this mentality!

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to do the output one line at a time, this should work (I didn't test it, sorry if it doesn't work, I will fix it)

    Dim lineNumber As Integer
    Sub AddToTextBox(ByVal _inputText As String)
        lineNumber += 1
        OutputTextBox.Text = lineNumber & ".   " & _inputText & vbNewLine
    End Sub

 

So to get the same output as in your post, it would work like:

AddToTextBox("Line 1")
AddToTextBox("Line 2")
AddToTextBox("Line 3")
AddToTextBox("Line 4")
AddToTextBox("Line 5")
AddToTextBox("Line 6")
AddToTextBox("Line 7")
AddToTextBox("Line 8")
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×