C# 写出文本框会产生意外的结果

C# 写出文本框会产生意外的结果,c#,listbox,tostring,C#,Listbox,Tostring,我已经创建了两个文本框,其中包含姓名和电子邮件地址,将其存储在“text”文件中,并在列表框中显示内容。我已经完成了所有工作,但当它显示在列表框中时,这就是我得到的输出 “System.Windows.Forms.TextBox,文本:tony” “System.Windows.Forms.TextBox,文本:tony@tony.com" 有人能告诉我为什么会这样吗?我对c#还是新手,我知道这是一件小事,我只是不知道去哪里找 using System; using System.Collect

我已经创建了两个文本框,其中包含姓名和电子邮件地址,将其存储在“text”文件中,并在列表框中显示内容。我已经完成了所有工作,但当它显示在列表框中时,这就是我得到的输出

“System.Windows.Forms.TextBox,文本:tony”
“System.Windows.Forms.TextBox,文本:tony@tony.com"

有人能告诉我为什么会这样吗?我对c#还是新手,我知道这是一件小事,我只是不知道去哪里找

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace _iLab_Week7
{
    public partial class Form1 : Form
    {  
        private StreamReader inFile; //streamreader for input
        private StreamWriter outFile;  //streamwriter for output

        const string filename = "contacts.txt";

        public Form1()
        {          
            InitializeComponent();
        }

        private void btnAddContact_Click(object sender, EventArgs e)
        {
            //Disable the "add contact" and "email" 
            btnAddContact.Enabled=false;
            txtBoxEmail.Enabled=false;

            //check for and create contacts.txt file
            if(!File.Exists(filename))
                File.Create(filename);

            if(File.Exists(filename))
            {
                try
                {
                    //create the file stream
                    outFile = new StreamWriter(filename, true);

                    //get the item from the name and email text box
                    //write it to the file
                    outFile.WriteLine(txtBoxName);
                    outFile.WriteLine(txtBoxEmail + "\n");

                    //close the file
                    outFile.Close();

                    //clear the textbox
                    txtBoxName.Text="";
                    txtBoxEmail.Text="";

                    //the cursor in the text box
                    txtBoxName.Focus();
                    txtBoxEmail.Focus();
                }
                catch (DirectoryNotFoundException exc)
                {
                     lstBoxContact.Items.Add(exc.Message);
                }
                catch (System.IO.IOException exc)
                {
                    lstBoxContact.Items.Add(exc.Message);
                }

                string listItem;
                this.lstBoxContact.Items.Clear();
                btnAddContact.Enabled = false;

                try
                {
                    //open file for reading
                    inFile=new StreamReader(filename);
                    //read from file and add to list box
                    while ((listItem=inFile.ReadLine()) !=null)
                    {
                       this.lstBoxContact.Items.Add(listItem);
                    }
                    //Close the file
                    inFile.Close();
                }
                catch (System.IO.IOException exc)
                {
                   this.lstBoxContact.Items.Add(exc);
                }
            }
            else
            {
                this.lstBoxContact.Items.Add("File Unabailable");
            }

        }

        private void lstBoxContact_SelectedIndexChanged(object sender, EventArgs e)
        {
            //enable button
            btnAddContact.Enabled = true;
            txtBoxName.Enabled = true;
            txtBoxEmail.Enabled = true;
        }

        private void Form1_FormClosing(object sender, FormClosedEventArgs e)
        {
            //make sure files are closed
            try
            {
                inFile.Close();
                outFile.Close();
            }
            catch { }
        }
     }
替换

outFile.WriteLine(txtBoxName);
outFile.WriteLine(txtBoxEmail + "\n");

试着

添加:当您说
txtBoxName
时,您指的是
txtBoxName
对象作为一个整体,它包含许多属性,如文本、前景色、字体等。。。。要仅获取值或内容,需要指定为您提供内容的属性,即
Text
属性

更多关于

相当于

outFile.WriteLine(txtBoxName.ToString());
对于大多数类,
ToString()
方法与
Object.ToString()
方法相同。此方法仅显示您尝试显示的实例类型的名称。所以你可能只看到

System.Windows.Forms.TextBox

但是,
TextBox
类可以帮助重写此方法,并显示
Text
属性的类型名称和值


但正如萨加·埃利亚斯·杰基所说。这不是您实际想要做的,所以只需按照他的建议显示
txtBoxName.Text

您可以发布代码吗?您需要发布代码。。没有它,我们就很难说出任何事情。看起来你是在保存教科书对象,而不是保存文本框内容……谢谢你做得很好。你能给我一个快速的解释,为什么我必须在对象名称后使用.Text吗?@Monti我看到了更新的答案。。。如果它解决了您的问题,请标记它的答案。谢谢您的帮助。我还是这个网站的新手。这学期刚开始学C。
outFile.WriteLine(txtBoxName);
outFile.WriteLine(txtBoxName.ToString());