Re: Newbie syntax question (Thank you Mike Williams)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"eBob.com" <fakename@xxxxxxxxxxxxxxxx> wrote in message news:edGT0pLOKHA.3192@xxxxxxxxxxxxxxxxxxxxxxx

On Windows systems the VB.Net Environment.Newline contains
both a carriage return (13) and a line feed (10). The same goes for
VB6, which has the vbNewLine constant (equivalent to vbCrLf
on Windows systems). I think you might be confusing NewLine
(both a carriage return and a line feed) with LineFeed (just a line feed)

Thank you Mike. I was indeed confusing NewLine and LineFeed.

You're welcome. The terms "carriage return" and "line feed" of course originally referred to the mechanical paper carriage of an old fashioned manual typewriter, whereby merely returning the carriage back to its horizontal starting position resulted in the print position returning to the start of the existing line of text (in which case any newly typed text would be on top of the existing line of text) and so a "line feed" (a mechanical movement of the paper roller causing the paper to move vertically) was also required in order to move the print position to the starting position of the next line. Both of those things (carriage return and line feed) needed to happen in order to move from the end of one line to the start of a new line. The confusion when using these terms in computing is understandable because even on systems that do use both a carriage return and a line feed to signify the move to the start of a new line (Windows, for example) the system will still in many cases perform the move to the start of a new line even if you send just one of them (either a carriage return on its own or a line feed on its own). This can cause confusion because in such cases the system does something other than you might reasonably expect it to do.

For example, if in VB.Net you use . . .

Dim s1 As String
s1 = "Hello " & Chr(13) & Chr(10) & "World"
Console.WriteLine(s1)

.. . . you will of course see the following in the debug window:

Hello
World

However, the above code will produce exactly the same result even if you include only Chr(13), or only Chr(10). Similar things happen when you send data to the printer in the normal Windows way (Printer.Print s1 in VB6 or whatever the equivalent is in VB.Net), with either a carriage return or a line feed on its own (or both together) all producing exactly the same result on the printed page. This can be confusing because everyone will tell you that Chr(13) is a carriage return and Chr(10) is a line feed and yet it seems that it does not matter, even in Windows, whether you include them both or not. This is because the Windows system is in such cases interpreting your data in a way that makes sense to Windows, resulting in Windows performing both operations even if your data specifies only one of them.

You can see what really should happen though by "moving Windows out of the way" when sending such data to an output device. Although modern printers are almost always used as full page printers (in the normal Windows way) many of them are capable of being used as old fashioned line printers, and such a printer will react to carriage returns and line feeds exactly in the way they were originally intended to work. The following code sends data directly to the printer bypassing the normal Windows printer drivers. It's VB6 code (because I don't use VB.Net myself) but because it uses mostly Windows API functions I'm sure it should easily convert into Net code. It sends a string containing the word "Hello" followed by a linefeed character followed by the word "World" directly to the printer, bypassing the normal windows printer driver. On printers that default to still being able to react to "old fashioned" printing code (most inkjet printers I would imagine) the output should be the word "Hello" on one line with the word "World" on the next line, but because we sent just a line feed character (instead of both a line feed character and a carriage return character) the word "World" should be horizontally positioned at a point immediately following end of the word "Hello".

Mike

Option Explicit
Private Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" (ByVal pPrinterName As String, _
phPrinter As Long, ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" _
Alias "StartDocPrinterA" (ByVal hPrinter As Long, _
ByVal Level As Long, pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long, pBuf As Any, _
ByVal cdBuf As Long, pcWritten As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type

Private Sub Command1_Click()
Dim myPrinter As Long, myDoc As Long, MyDocInfo As DOCINFO
Dim sWrite As String, charsWritten As Long, retVal As Long
retVal = OpenPrinter(Printer.DeviceName, myPrinter, 0)
'
sWrite = "Hello" & Chr(10) & "World"
'
If retVal = 0 Then
MsgBox "Printer Not found"
Exit Sub
End If
MyDocInfo.pDocName = "Any Name"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
myDoc = StartDocPrinter(myPrinter, 1, MyDocInfo)
Call StartPagePrinter(myPrinter)
retVal = WritePrinter(myPrinter, ByVal sWrite, _
Len(sWrite), charsWritten)
sWrite = vbFormFeed ' Chr(12)
retVal = WritePrinter(myPrinter, ByVal sWrite, _
Len(sWrite), charsWritten)
retVal = EndPagePrinter(myPrinter)
retVal = EndDocPrinter(myPrinter)
retVal = ClosePrinter(myPrinter)
End Sub



.



Relevant Pages

  • Re: End of Line
    ... new line feeds through the file, it ALSO removes the END OF LINE ... carriage return/line feed. ... For Each strLine In arrLines ...
    (microsoft.public.scripting.vbscript)
  • Re: Address Book Program
    ... first name, last name, age, id number. ... No need for carriage returns and line feeds to be stored; ... Data AcQuisition And Real-Time Analysis ...
    (comp.lang.asm.x86)
  • Re: End of Line
    ... new line feeds through the file, it ALSO removes the END OF LINE ... carriage return/line feed. ... For Each strLine In arrLines ...
    (microsoft.public.scripting.vbscript)
  • Re: [opensuse] What I Want (was Why I dont upgrade often)
    ... install I've put together... ... where it actually works BETTER in Linux than Windows. ... JumpTV is a website where you can subscribe to TV feeds from stations ... The trailers, news feeds, YouTube etc. all work fine in Linux IF you ...
    (SuSE)
  • Re: Read File Problem
    ... same code in a different perl script and I can read a file that does not have ... carriage returns and line feeds. ... Read the file into an array, ...
    (perl.beginners)