C# String 属性和字符串文字连接问题

标签 c#

我对 C# 有点陌生,遇到了字符串连接问题。我希望有人能给我提示并帮助我解决这个问题。我在 Google 上进行了广泛的搜索,并为此花费了一个多星期的时间,因此我们将不胜感激任何帮助/建议。

我已经为字符串属性创建了一个自定义 PathEditor。该属性基本上允许用户键入要在应用程序中使用的文件。如果输入的文件正确,它将按应有的方式显示在属性单元格中。我想做的是,如果输入的文件不存在,则向属性单元格输出一条错误消息 - 我在文件验证器中检查这一点。这是字符串文字问题。

如果我使用:

return inputFile+"Error_";

这工作正常,我在属性网格单元格中得到了输出 file123.txtError_。

如果我使用:

return "Error_"+inputFile;

我只得到没有文字“Error_”的输入文件。因此,属性网格单元格在属性网格单元格中显示 file123.txt。

我检查过,inputFile是字符串类型。关于为什么会发生这种情况有什么想法吗?

此外,有什么方法可以更改消息输出的字体和/或颜色吗?我尝试更改属性网格单元格的背景,但我知道这是不可能做到的。

谢谢。 Z

更多代码:

[
Description("Enter or select the wave file. If no extension, or a non .wav extension, is specified, the default extension .wav will be added to the filename."),
GridCategory("Sound"),
Gui.Design.DisplayName ("Input Sound"),
PathEditor.OfdParamsAttribute("Wave files (*.wav)|*.wav", "Select Audio File"),
Editor(typeof(PathEditor), typeof(System.Drawing.Design.UITypeEditor))
]
public string InputWavefile
{
    get { return System.IO.Path.GetFileName(inputtWavefile); }
    set 
    {
        if (value != inputWavefile)  // inputWavefile has been changed
        {                           
            // validate the input stringg
             _inputWavefile = FileValidation.ValidateFile(value);

            // assign validated value
            inputWavefile = _inputWavefile;
       }
    }
}

最佳答案

我的猜测是,您在 inputFile 的开头有一个时髦的字符,这令人困惑 - 尝试使用 inputFile 在调试器中查看它.ToCharArray() 获取字符数组。

字符串连接本身应该没问题 - 我怀疑这就是值的解释方式,这就是问题所在......

关于C# String 属性和字符串文字连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6656625/

相关文章:

C# File.ReadAllBytes 与 std::ifstream (Windows)

c# - 第一次加载页面时,如何获取下拉列表的选定项?

C# 有没有办法设置控制台应用程序的滚动位置

c# - 在接口(interface)内部声明 IEnumerable<T>,同时在具体类中声明 IList<T>

c# - 我破坏了与 Dotfuscator 的兼容性...请帮助我了解如何

c# - 如何声明和使用来自 json 对象的函数名?

c# - XSLT 的输出参数

c# - 将 DataGrid 绑定(bind)到 ObservableCollection<Dictionary>

c# - 为什么控制台不断为数组中的元素写入相同的输出?

c# - 为什么 C++ 和 C# 编译器的输出不同?