C# 为什么表单有多行标题?
嘿,伙计们 我正在用c编写一个新程序,其中从资源中读取字符串(.txt),但当我将外部化字符串设置为表单标题时,它似乎有两行。我相信这是因为字符串的外部化,但我有一个命令,它将删除新行。代码如下:C# 为什么表单有多行标题?,c#,.net,winforms,C#,.net,Winforms,嘿,伙计们 我正在用c编写一个新程序,其中从资源中读取字符串(.txt),但当我将外部化字符串设置为表单标题时,它似乎有两行。我相信这是因为字符串的外部化,但我有一个命令,它将删除新行。代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Globalization; using S
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
namespace Language
{
public class Language_Reader
{
private static CultureInfo ci = Thread.CurrentThread.CurrentCulture;
private static int word_count = 2;
private static string[] string_array;
private static string temp_string;
private static char[] remove_chars = {'\n'};
public static void makeLanguageStrings() {
if((ci.Name == "en-Us") || (ci.Name == "en-AU") || (ci.Name == "en-BZ") || (ci.Name == "en-CA") || (ci.Name == "en-CB") || (ci.Name == "en-IE") || (ci.Name == "en-JM") || (ci.Name == "en-NZ") || (ci.Name == "en-PH") || (ci.Name == "en-ZH") || (ci.Name == "en-TT") || (ci.Name == "en-GB") || (ci.Name == "en-ZW"))
{
Debug.WriteLine("Loading Resource: English");
//Loading resource
temp_string = Minecraft_Texturepack_Reload.Properties.Resources.Englisch;
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for(int i = 0; i < word_count; i++) {
string_array[i].Trim(remove_chars);
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if(i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
}
if((ci.Name == "de-DE")) {
Debug.WriteLine("Loading Resource: Deutsch");
//Loading resource
temp_string = Minecraft_Texturepack_Reload.Properties.Resources.Deutsch;
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for (int i = 0; i < word_count; i++)
{
string_array[i].Trim(Environment.NewLine.ToCharArray());
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if (i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
}
}
}
}
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for(int i = 0; i < word_count; i++) {
string_array[i].Trim(remove_chars);
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if(i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
利用制度全球化;
使用系统线程;
使用系统诊断;
名称空间语言
{
公共类语言阅读器
{
私有静态CultureInfo ci=Thread.CurrentThread.CurrentCulture;
私有静态整数字计数=2;
私有静态字符串[]字符串_数组;
私有静态字符串temp_字符串;
私有静态字符[]删除\u字符={'\n'};
公共静态void makeLanguageStrings(){
(ci.名称=“en BZ”的名称=“en BZ”的第124;(ci.名称=“en BZ”的第124;(ci.名称=“en BZ”的第124;(ci.名称=“en CA”的第124;(ci.名称=“en CA”的名名名=“en美国的名名=“en U U U U U U U们们们们们们们们们们们”的U们们们们们们们们们们们们们们们们们们们们们们们”的美国))的)若若若(如果(如果(如果(ci.名名名名名名名名名为(ci(ci.名名名名名名名名名名为为为为为为(ci.名名名名名名名名为为==“=“=“=““在在(ci.名名名名名名名名名名名名为为为为为为为为为为为为为为为为“在美国的| |(ci.Name==“en ZW”))
{
Debug.WriteLine(“加载资源:英语”);
//加载资源
temp_string=Minecraft_Texturepack_Reload.Properties.Resources.Englisch;
WriteLine(“成功加载资源”);
//将资源拆分为数组
WriteLine(“将上层资源拆分为字符串[]”);
字符串数组=临时字符串.Split(删除字符);
for(int i=0;i
我不明白为什么还有新线。请帮忙 换成这行怎么样:
temp_string = Minecraft_Texturepack_Reload.Properties.Resources.Englisch;
仅此而已:
var reader = new StringReader(Minecraft_Texturepack_Reload.Properties.Resources.Englisch);
Language_Strings.main_form_Text = reader.ReadLine()
Language_Strings.project_menu_item_Text = reader.ReadLine();
并删除代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
namespace Language
{
public class Language_Reader
{
private static CultureInfo ci = Thread.CurrentThread.CurrentCulture;
private static int word_count = 2;
private static string[] string_array;
private static string temp_string;
private static char[] remove_chars = {'\n'};
public static void makeLanguageStrings() {
if((ci.Name == "en-Us") || (ci.Name == "en-AU") || (ci.Name == "en-BZ") || (ci.Name == "en-CA") || (ci.Name == "en-CB") || (ci.Name == "en-IE") || (ci.Name == "en-JM") || (ci.Name == "en-NZ") || (ci.Name == "en-PH") || (ci.Name == "en-ZH") || (ci.Name == "en-TT") || (ci.Name == "en-GB") || (ci.Name == "en-ZW"))
{
Debug.WriteLine("Loading Resource: English");
//Loading resource
temp_string = Minecraft_Texturepack_Reload.Properties.Resources.Englisch;
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for(int i = 0; i < word_count; i++) {
string_array[i].Trim(remove_chars);
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if(i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
}
if((ci.Name == "de-DE")) {
Debug.WriteLine("Loading Resource: Deutsch");
//Loading resource
temp_string = Minecraft_Texturepack_Reload.Properties.Resources.Deutsch;
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for (int i = 0; i < word_count; i++)
{
string_array[i].Trim(Environment.NewLine.ToCharArray());
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if (i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
}
}
}
}
Debug.WriteLine("Succeed to load resource.");
//splitting up resource into array
Debug.WriteLine("Splitting up upper resource into string[]");
string_array = temp_string.Split(remove_chars);
for(int i = 0; i < word_count; i++) {
string_array[i].Trim(remove_chars);
//setting Language_Strings strings
if (i == 0)
{
Language_Strings.main_form_Text = string_array[i];
}
if(i == 1)
{
Language_Strings.project_menu_item_Text = string_array[i];
}
}
Debug.WriteLine("Succeeded to set strings' texts");
Debug.WriteLine(“成功加载资源”);
//将资源拆分为数组
WriteLine(“将上层资源拆分为字符串[]”);
字符串数组=临时字符串.Split(删除字符);
for(int i=0;i
德国(德语)也有同样的变化
然后我建议重构以注入语言处理程序,但这只是我挑剔的自我:)重构了代码。不确定为什么要使用临时字符串,或者为什么要使用循环来获取
0
和1
的索引,而索引总是相同的
这将删除任何新行字符。如果这不起作用,请告诉我
public static void makeLanguageStrings()
{
switch (ci.Name)
{
case "en-Us":
case "en-AU":
case "en-BZ":
case "en-CA":
case "en-CB":
case "en-IE":
case "en-JM":
case "en-NZ":
case "en-PH":
case "en-ZH":
case "en-TT":
case "en-GB":
case "en-ZW":
string_array = Minecraft_Texturepack_Reload.Properties.Resources.Englisch.Split(Environment.NewLine).Replace(Environment.NewLine, string.Empty);
break;
case "de-DE":
string_array = Minecraft_Texturepack_Reload.Properties.Resources.Deutsch.Split(Environment.NewLine).Replace(Environment.NewLine, string.Empty);
break;
}
Language_Strings.main_form_Text = string_array[0];
Language_Strings.project_menu_item_Text = string_array[1];
}
Trim不修改原始字符串;它返回修改后的字符串。您需要将结果分配回数组。我将使用字符串数组[I]=字符串数组[I]。在for循环的第一行上替换(Environment.NewLine,string.Empty)。字符串数组[I]=字符串数组[I]。替换(Environment.NewLine,string.Empty)不起作用:(