回 帖 发 新 帖 刷新版面

主题:创建一个应用程序来保存客户信息到文本文件。

创建一个应用程序来保存客户信息到文本文件,界面如图:

[img]http://zikao.dd.topzj.com/images/attachicons/image.gif[/img]
[url=http://zikao.dd.topzj.com/member.php?action=credits&view=getattach]图片附件[/url]

[url=http://zikao.dd.topzj.com/attachment.php?CHECKID=1172119510&aid=226455]未命名.JPG[/url]
 (2007-2-22 12:45 PM, 28.53 K)
[img]http://dd.up.topzj.com/data/dd/13/71/48/a/07/02/22/zrTDMP7_OTljqneAdOt4.jpg[/img]
以下是我写的代码:添加功能已经实现,就是那个查找功能还没实现。
关键是查找已经存在的文本文件中的字符串怎么查找?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
namespace WindowsApplication4
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.TextBox txtAddress;
        private System.Windows.Forms.TextBox txtPhone;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.txtPhone = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(16, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(128, 24);
            this.label1.TabIndex = 0;
            this.label1.Text = "输入客户姓名";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(16, 80);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 24);
            this.label2.TabIndex = 0;
            this.label2.Text = "输入客户地址";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 192);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(128, 24);
            this.label3.TabIndex = 0;
            this.label3.Text = "输入客户电话号码";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(160, 24);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(168, 21);
            this.txtName.TabIndex = 1;
            this.txtName.Text = "";
            // 
            // txtAddress
            // 
            this.txtAddress.AutoSize = false;
            this.txtAddress.Location = new System.Drawing.Point(152, 72);
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.Size = new System.Drawing.Size(184, 80);
            this.txtAddress.TabIndex = 1;
            this.txtAddress.Text = "";
            // 
            // txtPhone
            // 
            this.txtPhone.Location = new System.Drawing.Point(152, 184);
            this.txtPhone.Name = "txtPhone";
            this.txtPhone.Size = new System.Drawing.Size(168, 21);
            this.txtPhone.TabIndex = 1;
            this.txtPhone.Text = "";
            this.txtPhone.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(48, 256);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 32);
            this.button1.TabIndex = 2;
            this.button1.Text = "查找";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(176, 256);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(88, 32);
            this.button2.TabIndex = 2;
            this.button2.Text = "添加";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(312, 256);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(88, 32);
            this.button3.TabIndex = 2;
            this.button3.Text = "退出";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(448, 350);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtAddress);
            this.Controls.Add(this.txtPhone);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Name = "Form1";
            this.Text = "客户详情";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void textBox3_TextChanged(object sender, System.EventArgs e)
        {
        
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            StreamWriter writer = new StreamWriter(@"c:\Customer.txt",true,Encoding.Default);
            writer.WriteLine("电话:" + this.txtPhone.Text);
            writer.WriteLine("姓名:" + this.txtName.Text);
            writer.WriteLine("地址:" + this.txtAddress.Text);
            writer.Close();
            MessageBox.Show("追加成功","提示");
            this.txtName.Clear();
            this.txtAddress.Clear();
            this.txtPhone.Clear();
            this.txtName.Focus();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            string name = this.txtName.Text.Trim();
            if(name == null)
            {
                MessageBox.Show("请输入客户姓名");
            }
            else
            {
                StreamReader sr = new StreamReader(@"c:\Customer.txt",Encoding.Default);
            
                    string s = sr.ReadToEnd();
                if(s.IndexOf(name) > -1 )
                {
                        
                    MessageBox.Show(s.Split('\n')[1]);
                }
                else
                {
                    MessageBox.Show("没有这个人");
                }
                
            }
        }
    }
}

回复列表 (共2个回复)

沙发

写文件时没设定好格式,读文件时操作麻烦,,建议改格式。
追加文件内容,好像用writeline或write语句不行吧?!

板凳

[quote]写文件时没设定好格式,读文件时操作麻烦,,建议改格式。
追加文件内容,好像用writeline或write语句不行吧?!
[/quote]
 StreamWriter writer = new StreamWriter(@"c:\Customer.txt",true,Encoding.Default);
看看这句你就知道了,是可以实现追加的。
第二个参数为true是追加,为false是修改。
建议你看看MSDN就知道了~

我来回复

您尚未登录,请登录后再回复。点此登录或注册