I use email to send my emails out. In body clause i use this code below. In bodyText variable i insert text comming from stringbuilder. However at the end when my email is received all text are not line b line but in one line. What am i doing wrong?
Mail.Body = "<HTML><HEAD></head><BODY style='font-size: 11px; font-family: Tahoma'>" + "<P>" & "Hi there," & ",</p>" + "<p>" & bodyText & "</p>" & "<p>This e-mail is generated automatically therefore <b>do not reply to this email.</b></p>" + "<p>Developer, </p>" & "Development team" & "</BODY></HTML>"
Stringbuilder:
_strbuild.Append("Start" + Environment.NewLine).AppendLine()
_strbuild.Append("Start" + Environment.NewLine).AppendLine()
You create some HTML so you have to use the HTML new line (<br>):
_strbuild.Append("Start" + "<br>")
_strbuild.Append("Start" + "<br>")
Instead of using the system new line (\r\n or \n) you have to use the <br> element. In HTML you can use the system new line only to format the HTML code not to format the output.
When the body of the message is sent in HTML format, add the (break - br) tags right in your String. vbCrLf and StringBuilder are not suggested as they don't work if the body is in HTML format.
Dim mail As New MailMessage
mail.IsBodyHtml = True
mail.Body = "Line one<br>"
mail.Body += "Line Two<br>"
mail.Body += "More Lines"
Environment.NewLine does not work because the current environment for your program is not the same environment where the user will read your result. Your environment is a console program (masquerading as a scheduled task or service), or web server application, or windows form, or something else where outputting a simple \n character results in a new line. The same applies to AppendLine(). It's using Environment.NewLine behind the scenes, and again, the problem is that your program environment is different than your user's environment.
In this case, your user's environment is an html document. HTML treats all simple whitespace the same, as a single space. This is true whether you have a tab, newline, space, or any multiples or combination of the above. It all consolidates to a single space in html.
So for html, in order to force a line break, you should instead include a <br> tag with your html. More than this, HTML will break up the lines automatically; you might get away without doing anything. Even in cases where you want an explicit line break what you're typically really doing from a semantic standpoint is asking for a new paragraph. This means <p> is commonly more appropriate:
_strbuild.Append("Start").Append("<p>")
If you really don't want the extra blank line from the new paragraph you can use styles to remove or reduce it (though admittedly e-mail is the one place where breaking the correct p vs br semantics might be appropriate: e-mail renderers can be difficult).
Related
I have a form in Access 2016 with a textbox in which I need to have multiple, semi-colon delimited hyperlinks (which will be dynamically created). What I've decided to do is create a "hyperlink construction string" in VBA, then assign them to the value of the textbox. So, something like:
Me.Field.Value = {link: www.google.com : "Google"} & "; " & {link: www.yahoo.com : "Yahoo"}
...would result in this being in the text box:
Google; Yahoo
My problem is, I can't seem to figure out the syntax to create an individual link in the textbox without making the entire textbox a single hyperlink, which isn't gonna work.
I was working with a few solutions that I've found. I read that this would create the link in the way I need, but it just comes through as literal text with the pound signs:
"Google # www.google.com # Some Argument"
I also tried setting the textbox to rich text, then setting the value to include rich text code for a hyperlink... but that's not working:
"{\field{\*\fldinst HYPERLINK ""http://www.google.com/""}{\fldrslt http://www.google.com}}"
I also thought about designing a Query that will return the hyperlinks. But, I kind of wanted to make it a VBA thing, because I'll have more flexibility in how I create the value. Does anyone have any ideas?
Note: I understand that multiple values should be in a 1:M relational database. They are. But, the requirements of the task are to get all the M values for a 1 entity, then list them out in semi-colon, delimited fashion, which all serve as links to a Details table for the M entity.
Regular textboxes (text only) don't support this.
It is possible with Rich text textboxes. In contrast to the name, they actually use a subset of HTML, not RTF.
With ideas from here I got this working:
Private Sub cmdInsertHyperlinks_Click()
Dim url1 As String, url2 As String
url1 = "D:\tmp\test.jpg"
url2 = "D:\tmp\test space.txt"
Me.rText.Value = "<div>" & _
"file://" & url1 & "" & _
" other text between hyperlinks " & _
"file://" & url2 & "" & _
"</div>"
End Sub
Note: the linked thread says you must URL-encode the links (space to %20 etc), but at least for my simple test, that wasn't necessary.
Note 2: You can't have a different display text and link url, at least I didn't get that to work.
So I'm working on my A Level coursework and have hit a block that I can't seem to work out. Basically, I have a bit of code that sends an email using text from a text file as well as a bit of hard coded text, all put into one variable before being passed onto the subroutine for sending an email.
I'm using a stream reading for both the email subject and body (as they're each in separate text files) and, while they're working fine elsewhere in the program, they're not working here. Rather than putting the contents of the text file into the variable before it's used to send an email, they're just putting the name of the text file there.
Also, the name of the file does not appear anywhere in the text stored within
E.g. The body of the email that it sent had the hard coded message and then it just said 'AbsenceEmailBody' (name of the text file) rather than what was inside the text file itself
Here's the lines of code where the files are read and put into variables
Dim objReaderSubject As New System.IO.StringReader("AbsenceEmailSubject")
Dim objReaderBody As New System.IO.StringReader("AbsenceEmailBody")
Dim EmailBody As String
Dim EmailSubject As String
EmailBody = "Dear " & CadetDS.Tables("CADET").Rows(0).Item("CadetFirstName") & "," & vbNewLine & vbNewLine & "It has come to the staff's attention that you have attended only " & ProblemAttendances(i, 1, 0, 0, 0) & "% of sessions." & vbNewLine & objReaderBody.ReadToEnd
EmailSubject = objReaderSubject.ReadToEnd
I also check the files exist before any of this
If System.IO.File.Exists("AbsenceEmailSubject") = True And System.IO.File.Exists("AbsenceEmailBody") = True Then
I've looked around everywhere and can't seem to find an answer. Also, this is my first time ever asking a question on here (or anywhere online) so if you need any more information just ask
I've been globalizing an application and have been using Resx Manager to make my life easier. I ran into a multi-line string literal and it stumped me.
How would I handle the escape characters when making this string into a resource?
If Not RelayMessage(
"Are you sure you want to do the selected action?" & vbCrLf &
"A confirmation message will be sent to the user." & vbCrLf &
"Please ensure you want to perform this action before hitting accept.",
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
How would I make that string into a resource?
In the standard VS resource manager (is this the manager you're using?) you can enter a multi-line string resource directly in the editor by using shift-Enter:
Note that this is actually stored as a string with CR+LF pairs, assisted by the space="preserve" attribute. Viewing the .resx file in a text editor:
Results using a standard message box:
MessageBox.Show(strings.myString)
I don't know how it is usually handled in globalization problems. But an easy way would be to define your own escape character formats. For example you could define \n as a newline character. When you actually use your ressource you could then use
If Not RelayMessage(Strings.Replace(myResourceString, "\n", vbCrLf),
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
instead of
If Not RelayMessage(myResourceString,
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
Or you could manually add chars with character codes 10 and 13 (e.g. ChrW(10) & ChrW(13)) at the vbCrLf location in your ressource string. This equals a vbCrLf (meaning a carriage return (10) + line feed (13)). This would avoid manipulation of the source code. Other stuff like Tab (9) have codes, too. These are called control characters. Take a look at the wikipedia http://en.wikipedia.org/wiki/Control_character
I have a form with several text boxes and I want to write the contents of each of them to a new line in a .txt file. As in, the user fills in a form, and the info is stored in the file. Then I want to be able to retrieve the info from the file into the same text boxes. I am able to do this, so far, but I encounter problems when one of the text boxes is multiline.
Printline(1, txtBox1.text)
Printline(1, txtBox2.text)´which is the multiline one
Printline(1, txtBox3.text)
When I read this back from the file I get the second line of the multiline text box where I want the text from txtBox3 to be.
LineInput(1, txtBox1.text)
LineInput(1, txtBox2.text)
LineInput(1, txtBox3.text)
How can I get all the lines from the multiline text box to write to one line in the file, and then read it back as separate lines in a multiline text box?
I hope I am making sense? I really would like to keep the logic of "one txtBox - one line in the file"
I guess I need to use different methods of writing and reading, but I am not that familiar with this, so any help is much appreciated.
You can rely on the Lines Property in case of having more than one line. Sample code (curTextBox is the given TextBox Control):
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("path", True)
Dim curLine As String = curTextBox.Text
If (curTextBox.Lines.Count > 1) Then
curLine = ""
For Each line As String In curTextBox.Lines
curLine = curLine & " " & line
Next
curLine = curLine.Trim()
End If
writer.WriteLine(curLine)
End Using
NOTE: this code puts in one line all the text from the given TextBox independently upon its number of lines. If it has more than one line, it includes a blank space to separate the individual lines (all of them fitting in a single line of the file anyway). You might want to change this last feature by adding a different separating character (replace & " " & with the one you want).
One option would be to escape the newlines so that they aren't in the output, then unescape them on reading back in.
Here's some example code that will do this (I've never written VB before, so this probably isn't idiomatic):
' To output to a file:
Dim output As String = TextBox2.Text
' Escape all the backslashes and then the vbCrLfs
output = output.Replace("\", "\bk").Replace(vbCrLf, "\crlf")
' Write the data from output to the file
' To read data from the file:
Dim input As String = ' Put the data from the file in input
' Put vbCrLfs back for \crlf, then put \ for \bk
input = input.Replace("\crlf", vbCrLf).Replace("\bk", "\")
' Put the text back in its box
TextBox2.Text = input
Another option would be to store your data in XML, JSON, or YAML. Any of those are text-based formats that will require a library to parse, but should cleanly handle the multiline text you have, along with providing increased future flexibility.
the next simple code works for me.
Saving multiline text to a single line in a file:
str = Replace(MyTextBox.Text, Chr(13) & Chr(10), "*LineFeed*") 'something recognizable
Print #1, str 'no quotes
To get the string from the file and put it on a TextBox:
Line Input #1, str
MyTextBox.Text = Replace(str, "*LineFeed*", Chr(13) & Chr(10))
Hope this helps
I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.
Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.
Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.
However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.
The proper way to do this in VB is to use on of the VB constants for newlines. The main three are
vbCrLf = "\r\n"
vbCr = "\r"
vbLf = "\n"
VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.
C++ file path string: "c:\\foo\\bar.txt"
VB file path string: "c:\foo\bar.txt"
C# file path string: C++ way or #"c:\foo\bar.txt"
You need to use HTML on a web page to get line breaks. For example "<br/>" will give you a line break.
If you are using something like this.
Response.Write("Hello \r\n")
Response.Write("World \r\n")
and the output is
Hello\r\nWorld\r\n
Then you are basically looking for something like this
Response.Write("Hello <br/>")
Response.Write("World <br/>")
This will output
Hello
World
you can also just define "<br />" as constant and reuse it
eg.
Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " & HtmlNewLine)
Response.Write("World " & HtmlNewLine)
it's :
vbnewline
for example
Msgbox ("Fst line" & vbnewline & "second line")
Try Environment.NewLine.
Your need to use the html/xhtml break character:
<br />
you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:
System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))
In asp.net for giving new line character in string you should use <br> .
For window base application Environment.NewLine will work fine.
VbCr
Try that.
In this case, I can use vbNewLine, vbCrLf or "\r\n".
vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.
You can call the String.Replace() function to avoid concatenation of many single string values.
MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))
Environment.NewLine or vbCrLf or Constants.vbCrLf
More information about VB.NET new line:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
I had the need to store line breaks in a string in a SQL table and have them displayed in vb.NET. My solution was to include a string like this in my database:
"This is the first line{0}This is the second{0}This is the third"
In vb.NET, I processed the string like this before using it:
Label2.Text = String.Format(stringFromSQLquery, vbCrLf)
This replaces every occurance of {0} with vbCrLf