Escape Characters in a resource file string? - vb.net

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

Related

How can I use special chars in VBA of Microsoft Word?

I've created a set of macro files in Microsoft Word's VBA as a sort of a CAT tool (CAT = https://en.wikipedia.org/wiki/Computer-assisted_translation). The problem is that there are cases where I display the text needed to be translated and the user needs to input text in his own language. That might include some special chars, like "ăîâșț/ĂÎÂȘȚ", or even quotes or brackets. Is there any way to use those in some InputBox function? Or, at least, some way to let the user input the text he needs in some TextBox or something?... Or how should I approach this?... Maybe UTF-8 support would be what I need? Or?... Any help would be appreciated!...
I've tried Microsoft Word's vba function InputBox. I'm also thinking if, maybe, I would be able to create my own InputBox, with my conditions on it, I might be able to have one that accepts those chars too, or all the chars into some string variable... Here is something someone on StackOverflow says:
Is it possible to create an 'input box' in VBA that can take a text selection with multiple lines as an input? (I'm referring to gizlmo's answer...)
Here are 3 lines of code that contain that (although it's more of a how to question, not a debugging question, so those are not really needed...)
MsgBox ("Ziua " & Str(ziua) & " - " & titlurien(ziua))
titluales = InputBox("Titlul original: " & titlurien(ziua), "Ziua: " & Str(ziua) & ", Rapsodia Realitatilor " & monthname(lunanecesara) & Str(annecesar))
titluriro(ziua) = titluales
I expect the output to be exactly what he typed, whether it's quotes, brackets or special characters (like "ăîâșț"/"ĂÎÂȘȚ")...
A VBA InputBox will take any character typed or pasted into it. The characters available to type depends on the Language version of Windows and Office that the end user has installed.
Below is a test I just made with your example character string "ăîâșț/ĂÎÂȘȚ"
SpecialCharInput()
Dim str As String
str = InputBox("Enter you text", "Special Test Input Box")
Debug.Print str
End Sub
On my English language system, the only trouble it had was with the upper and lower case "ȘȚ" Turkish characters. By trouble I mean it turned those characters into question marks "??" in the result string. I'm sure though, if my system supported the Turkish language that those characters would be recognized and outputted properly.

Email body line by line

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).

Creating Multiple Links in an Access Textbox

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.

Pass Quotes from a text box to external URL

I have a simple VB.net app that has a text box that I pass to a URL for searching. So let's say I want to send this:
search "*ball"
So, my search will look for everything with ball after the *. The problem is, it strips the quotes when I send it.
System.Diagnostics.Process.Start("https://searchgames.local/search?game=" & TextBox1.Text)
How can I look for Quotes in my text box, then if they are there, pass them appropriately to the URL I am sending to. Below my code just makes sure they actually enter something into the textbox. As always, help is always appreciated.
If TextBox1.Text = "" Then ' If user does not enter any text
MsgBox("Enter text to search on." & vbCrLf & Err.Description, MsgBoxStyle.Information, "Need search term to search on.")
TextBox1.Focus() 'Set the cursor back to the text box
If you are in a web app, you can use HttpUtility.UrlEncode to properly escape the quotation mark characters. Outside of a web application, MSDN recommends that you use WebUtility.UrlEncode. You could also use Uri.EscapeDataString, but there seem to be some problems with it.
So, for instance, you could do this:
Process.Start("https://searchgames.local/search?game=" & HttpUtility.UrlEncode(TextBox1.Text))
Or this:
Process.Start("https://searchgames.local/search?game=" & WebUtility.UrlEncode(TextBox1.Text))
Or this:
Process.Start("https://searchgames.local/search?game=" & Uri.EscapeDataString(TextBox1.Text))
Try this:
Dim url as string = "https://searchgames.local/search?game=" & TextBox1.Text
Dim encodedurl as string = HttpContext.Current.Server.UrlEncode(url)
System.Diagnostics.Process.Start(encodedurl)

New line character in VB.Net?

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