设置中的消息框文本

3
我想从设置中设置我的WinForm控件的文本。
如果将来我想更改程序语言,那么这很容易;
只需修改相应的设置即可。
其中一个messageBox的文本有一个换行符(\ n)。
当我从设置中插入它的文本时,\n会作为文本的一部分出现,而没有换行符。
MessageBox.Show(P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotification, 
P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotificationTitle, 
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

任何想法?

尝试使用 Environment.NewLine? - Max
@MaxMommersteeg:谢谢。我可以直接将它插入到字符串中吗? - user3165438
@MaxMommersteeg 你不能将它存储为 Setting 的值。 - dovid
@lomed 看看我的答案,它使用 Environment.NewLine,当从“Setting”获取字符串时。 - Max
2个回答

7
这对您应该有效。
string somestring = @"this is some text \n Some more text";
somestring = somestring.Replace(@"\n", Environment.NewLine);
MessageBox.Show(somestring);

1
+1 但我会至少处理“\”序列(否则,如果消息包含路径等内容,则无法正常工作)。也可以使用空格(例如,“\n”而不仅仅是“\n”)。 - Adriano Repetti

1
替换:
MessageBox.Show(P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotification, 
P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotificationTitle, 
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

With:

MessageBox.Show(P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotification.Replace(@"\n", Environment.NewLine)), 
P_Settings.NotificationMessageBoxes.Default.ProcessFinishedNotificationTitle, 
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

Replace函数将检查\n并用Environment.NewLine替换它。 转义序列在实际的字符串对象中没有意义,只有当C#编译器解释它们时才有意义。


1
你的答案重复了 @Ehsan 的回答。 - dovid
@Max Mommersteeg:谢谢,它起作用了!但是为什么从设置中检索的字符串不知道 \n? - user3165438
@lomed 看看我发表评论的时间,这并不完全相同,我没有创建一个新的字符串,而是直接从“Setting”中插入字符串,这需要更少的维护工作,也是TS所要求的。 - Max
@user3165438 转义序列在实际字符串对象中没有意义,只有当C#编译器解释它们时才有意义。 - Max
@MaxMommersteeg:明白了,谢谢。 - user3165438

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接