Problem with Character Escape Sequences
I need to set text & tooltips of controls on a form from a XML file. A sample portion of the XML file is as follows:
<CheckBox
ToolTip="Check this Checkbox to include\r\n Accession Type in the \u000D\u000ASearch Criteria." >
<CheckBox>
I am using the following instruction to set the control tooltip:
Me.ToolTip1.SetToolTip(control, node.Attributes("ToolTip").Value)
Everything works fine, except the escape characters. Neither "\r\n" nor "\u000D\u000A" translate into a newline.
Can somebody suggest where the problem is?
What kind of application do you work on? Windows forms or Web forms?I created a sample windows forms application and used following code :
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.SetToolTip(button1, "Check this Checkbox to include\r\n Accession Type in the \u000D\u000ASearch Criteria.");
}It worked fine and showed the tooltip in 3 lines. Could you post some code to reproduce the problem?
VB.net doesn't understand those escape sequences. You would have to parse the string and replace them all with whatever characters are required. Or use C#, which does understand them.
Oh, I forgot this is a VB area....Sorry about that.
jo0ls is right.Use Environment.NewLine or vbCrLf for the newline.
Replace every occurance of \n\r with Environment.NewLine using String.Replace.
Hope this helps.
I have already parsed out the escape sequences. I did not knew that they do not work with VB. As I have gone to far into the project, switching to C# is simply not feasible.
There are too many strings that need to be parsed. So, I was looking at escape sequences to avoid costly parsing. Looks like there is no other choice.
Anyways, thanx both of you.
By the way, those strings are ultimately translated into MSIL. And all .NET classes (String etc.) are common across languages
. So, why is that escape sequences in Strings work in C#, butot in VB?
C# and VB.Net each provide functionality that the other lacks. This is simply becuase over the years VB developers and C++ developers each have come to expect certain things - so .Net strives to serve each.
Now, to get arround this issue, here is what I would suggest:
Write the XML string with string format codes. Then you can use the highly efficient String.Fomat() to reformat the XML value before using it in your application. Here is an example of how you would use this to get the tool tip on three lines:
<CheckBox
ToolTip = "Check this Checkbox to include{0}Accession Type in the{0}Search Criteria.">
</CheckBox>
Me.ToolTip1.SetToolTip(control, String.Format(node.Attributes("ToolTip").Value, ControlChars.NewLine))
This will be very fast and avoid messy parsing code. You can lookup the String.Format() method in the MSDN library to find links to additional format specifiers.
Good luck!
Although parsing is also straightforward a single line instruction in my case, you say Formatting is more efficient. And I take your word for that...
By the way, I checked out the resulting String after formatting. To my surprise, Format inserts "\r\n" in place of format specifier, {0}.
Any explanations?
I'm sorry, but I don't understand... where are you seeing "\r\n"? Those characters should no longer be in the string.
If you start with a string like:
Dim mStr As String = "This is line one{0}Here is line two{0}And this is line three"
And then use String.Format() as follows:
Dim mRes As String = String.Format(mStr, ControlChars.NewLine)
The resulting string, when displayed on the form, will appear as:
This is line one
Here is line two
And this is line three
If you look at mRes while debugging, it will appear as one long string, but the new line characters will still be in there.
Yes, they are in the string, and no they are not in the string!!!
What I mean is that, the string breaks up into lines, when it shows up in the tooltip, or at other places in the form.
But when I view it in the debugger by placing mouse on mRes, I see "\r\n" at places of original format specifiers.
Although that should not worry me, coz my string is displaying properly, but still if Format replaces {0}, with \r\n as I see it in the degugger, why Format it at all, instead of directly embedding them in the String?
Another point that I am not getting. Yes, I agree that C# & VB might have different facilities they offer, but in this case, we are talking about the String class, & how controls parse the strings to display them on their surface. As Strings & control classes belong to the runtime, rather than the languages, how can C# language peek inside a String & recognize Escape sequences, while VB cannot?
All this peeking belongs to the String or control classes, isn't it?
Rahul Singla wrote: |
| Yes, they are in the string, and no they are not in the string!!! What I mean is that, the string breaks up into lines, when it shows up in the tooltip, or at other places in the form. But when I view it in the debugger by placing mouse on mRes, I see "\r\n" at places of original format specifiers. Although that should not worry me, coz my string is displaying properly, but still if Format replaces {0}, with \r\n as I see it in the degugger, why Format it at all, instead of directly embedding them in the String? Another point that I am not getting. Yes, I agree that C# & VB might have different facilities they offer, but in this case, we are talking about the String class, & how controls parse the strings to display them on their surface. As Strings & control classes belong to the runtime, rather than the languages, how can C# language peek inside a String & recognize Escape sequences, while VB cannot? All this peeking belongs to the String or control classes, isn't it?
| |
Hi Rahul,Any language, or being specific to .Net, any .Net language has to provide some way to define string literals. And as some syntax characters are used to represent a string literal in a language, those characters cause trouble when we want them to become a part of the string value itself. If confusing, so let's take an example :
Both C# and VB use Double Quote (") character to enclose the string literal. So, when you use (") in the string itself, it causes syntax error.
String s = "He said : "Hi""; // This statement in C# thinks string literal definition is over after it finds (") after the (
character. So, it generates an error.// To avoid this, C# provides escape characters :
String s = "He said : \"Hi\"";
// This is a valid syntax
On the other hand, VB provides different way to handle such case
Dim s As String = "He said : ""Hi"""
VB doesn't provide escape characters, but provides a constant vbCrLf to define newline in VB. So, when the following string literal is defined in C# :
"Hello, \nWorld!"
C# treats it as
Hello,
World!
but VB treats it as
Hello, \nWorld!
In general, C# follows it's syntax from C, C++ both of which proivides escape characters and VB does not.
And the answer to your question is for Visual Studio, it has to show user the presence of newline in a string literal. But it must provide a standard way independent from any language that is being debugged. So, it chose the more famous and more appropriate way of using \n\r (most Operating systems use one or combination of them to represent newlines). Thus, you see those characters when debugging.
I hope this solves your confusion.Yes, that was interesting as well as informative...
Thanx!!!
Actually I still find this odd... On my computer, the debug value shown when hovering over the variable appears as a single line with no indication that there are line breaks... I wonder if this is becuase I'm using Vista?
Strangely enough, I again tried that... And this time, the variable appeared in my case also as a singla line, both when hovering over the variable, as weell as when the variable was put in the watch list.
And this is when I use XP.
But hovering over the value in the watchlist shows it in a tooltip with value broken over multiple lines at desired places!!!