写出文本框会产生意想不到的结果
Writing Out a Textbox Produces Unexpected Results
我创建了 2 个包含姓名和电子邮件地址的文本框,将其存储在“text”文件中,并在列表框中显示内容。我已完成所有操作,但当它显示在列表框中时,这就是我得到的输出。
"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 { }
}
}
替换
outFile.WriteLine(txtBoxName);
outFile.WriteLine(txtBoxEmail + "\n");
和
outFile.WriteLine(txtBoxName.Text);
outFile.WriteLine(txtBoxEmail.Text + "\n");
然后尝试...
ADDED :当你说 txtBoxName
时,你指的是整个 txtBoxName
object ,其中包含许多属性 - 如 Text、ForeColor、Font 等...。要仅获取值或内容,您需要指定 属性 给你 - 这是 Text
属性
更多关于TextBox
outFile.WriteLine(txtBoxName);
等同于
outFile.WriteLine(txtBoxName.ToString());
对于大多数 classes,ToString()
方法与 Object.ToString()
相同。此方法仅显示您尝试显示的实例类型的名称。所以你可能只看过
System.Windows.Forms.TextBox
但是,TextBox
class 有用地覆盖了此方法并显示了 Text
属性.
的类型名称和值
但正如 Saagar Elias Jacky 所说。这不是你真正想要做的,所以按照他的建议显示 txtBoxName.Text
。
我创建了 2 个包含姓名和电子邮件地址的文本框,将其存储在“text”文件中,并在列表框中显示内容。我已完成所有操作,但当它显示在列表框中时,这就是我得到的输出。
"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 { }
}
}
替换
outFile.WriteLine(txtBoxName);
outFile.WriteLine(txtBoxEmail + "\n");
和
outFile.WriteLine(txtBoxName.Text);
outFile.WriteLine(txtBoxEmail.Text + "\n");
然后尝试...
ADDED :当你说 txtBoxName
时,你指的是整个 txtBoxName
object ,其中包含许多属性 - 如 Text、ForeColor、Font 等...。要仅获取值或内容,您需要指定 属性 给你 - 这是 Text
属性
更多关于TextBox
outFile.WriteLine(txtBoxName);
等同于
outFile.WriteLine(txtBoxName.ToString());
对于大多数 classes,ToString()
方法与 Object.ToString()
相同。此方法仅显示您尝试显示的实例类型的名称。所以你可能只看过
System.Windows.Forms.TextBox
但是,TextBox
class 有用地覆盖了此方法并显示了 Text
属性.
但正如 Saagar Elias Jacky 所说。这不是你真正想要做的,所以按照他的建议显示 txtBoxName.Text
。