【问题标题】:How to remove 'System.Windows.Forms.TextBox, Text:' from message and text如何从消息和文本中删除 'System.Windows.Forms.TextBox, Text:'
【发布时间】:2020-06-09 23:12:45
【问题描述】:

我用 C# 编写了一个简单的程序,其中

  1. 使用如下语句显示消息:
    {
     MessageBox.Show("Enter the Correct Values. ", "Error", 
                     MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
     return;
    }
  1. 使用 StreamWriter 将某些文本框内容写入文本文件。 在这两种情况下,除了正确的内容外,我还得到了前缀: System.Windows.Forms.TextBox,文本:

  2. 在使用 StreamReader 读取文本文件的内容时,我得到了类似的前缀(和后缀 \r\n)。

如何避免这种情况?

【问题讨论】:

  • 发布一些代码。听起来你在不应该出现的对象上调用 ToString()

标签: c# system message


【解决方案1】:

没有看到您的代码,我猜您是直接将TextBox 传递给StreamWriter,而不是传递TextBox.Text 属性值。将TextBox 引用直接传递给StreamWriter.WriteLine 将调用TextBox.ToString() 以获取它可以写入的字符串值,看起来TextBox.ToString() 正在生成该前缀。传入TextBox.Text,应该就看不到了。

【讨论】:

  • 我很高兴它有帮助。您应该单击此答案旁边的复选标记以将其标记为正确。
【解决方案2】:

我发现的最好和最简单的解决方案是拆分字符串并使用所需的字符串

        string str = TextBox1.Text;
        char[] spearator = { ' ' };
        String[] splitStr = str.Split(spearator);
        string RequiredString =  splitStr[1];

试试这个代码希望它会工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多