How to ignore @ escape character (C#)

N

Newcomer

Hi,
I added a string property for my winforms control.
This string property can take multiple lines of text by using "\r\n".
For example, "Hello\r\nWorld"
SHOULD prints:
Hello
World

The problem is that c# _always_ treats my string as _path_ (using @-quoted).
It prints:
Hello\r\nWorld

I would like to get rid of this silly '@' in this case.
How do I correct this?
Thanks.
Minh
 
D

Dave

C# does not treat strings as "literals" unless they are constants, prefixed with the '@' character.

If your property is of type String, then your code must assume that the value passed in to the set accessor is already formatted
correctly. In other words, your code cannot determine whether the caller used the '@' symbol.

Do not set your property using the '@' character if you want to preserve the "\r\n" escape characters.
 
M

Mattias Sjögren

The problem is that c# _always_ treats my string as _path_ (using @-quoted).
It prints:
Hello\r\nWorld

Where do you print it? If you view the string in the debugger it may
appear that way.



Mattias
 
N

newcomer newcomer

Dave, thanks for your reply.

I DON'T use '@' in the string property.

I think, this problem only happends when I tried to set the string value
at _design_time_. C# will detects "\r\n" and treats that as path.

You can test this by having a simple form with a text editor (textBox1).
At design time, set the text value (textBox1) to "Hello\r\nWorld"
(ignore the quotes). Then in your test code, try
Console.WriteLine(textBox1.Text).
You _will_ get just a single line of print out.

That is my problem.
Thanks again.
Ming
 
D

Dave

Ok, I understand now. In the designer, yes but C# is not the causing the problem.

By default the designer doesn't support what you need to accomplish.
However, you can implement your own editor and set it to the property defined on your class using the EditorAttribute. This will
allow you to pop up an interface (or dropdown) that may allow the user to enter multi-line string data.

Another workaround is to not use the designer, instead manually set the properties value in the code-behind in the constructor of
the class after the InitializeComponent call.
 
N

newcomer newcomer

You are right, it is not C#...
It is MS Dev. Studio .NET ...

Yes, I've been using the second work around, it's kind of clumsy...as
you see, i'll have to create a seperate resource file for
localization...:(

Ming
 
D

Dave

Use the EditorAttribute as I stated. Create a modal dialog that pops up when the user wants to add multi-line data by inheriting
from UITypeEditor and overriding the appropriate methods. I suggest modal dialog because this way you can show a multiline textbox
with Ok and Cancel buttons to apply or discard the changes in a manner that's more intuitive to an end-user than a drop-down form.

The property grid will display an elisis button ("...") after the property and invoke a method on your custom type editor that can
display the dialog as I suggested above.

The default implementation of the string property will remain singleline, with the elipses added, which should be OK for your
implementation.

Creating a custom type editor isn't difficult, but it is tedious. You'll have to research the methods and figure out which
overrides you need to implement.

GL
 
J

James Curran

newcomer newcomer said:
You are right, it is not C#...
It is MS Dev. Studio .NET ...

The problem is that \r & \n only mean something to the C# compiler. By
the time you enter them into a text box, the compiler's work is long over.
Here, you are typing a slash & an R, and it's giving you a slash and an R.
What more do you want? If you want a newline, well, then, make you text box
multi-line, and and type a proper newline.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
B

Bruce Wood

No, this is definitely an issue with the designer. It doesn't allow you
to enter multi-line properties... it has no syntax that allows you to
indicate "insert a newline here in the property string."

You either have to create a custom editor for your property or
manipulate the code-behind or the resource file yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top