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

时间:2015-04-17 02:35:52

标签: c# listbox tostring

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

  

" System.Windows.Forms.TextBox,Text:tony"
  " System.Windows.Forms.TextBox,Text: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 { }
        }
     }

2 个答案:

答案 0 :(得分:0)

替换

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

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

并尝试...

已添加:当您说txtBoxName时,您指的是txtBoxName 对象作为一个整体,其中包含许多属性 - 例如Text ,ForeColor,Font等....要获得唯一的值或内容,您需要指定给您的属性 - 这是Text属性

有关TextBox

的更多信息

答案 1 :(得分:0)

outFile.WriteLine(txtBoxName);

相当于

outFile.WriteLine(txtBoxName.ToString());

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

  

System.Windows.Forms.TextBox

但是,TextBox类有助于覆盖此方法,并显示类型名称和Text属性的值。

但正如Saagar Elias Jacky所说。这不是你真正打算做的,所以只要按照他的建议显示txtBoxName.Text