将4个文本框中的数据存储到一个简单的数组中

时间:2015-07-19 08:52:35

标签: c# arrays textbox binaryfiles

我需要帮助将4个文本框中的数据存储到数组中并将数据写入二进制文件。

这是我特别需要的:

将4个文本框中的数据存储到一个简单的数组中,然后添加一个保存按钮:

  • 将4个文本框中的信息存储到数组

  • 将4个文本框中的信息作为四个单独的项添加到二进制文件中 按书名

  • 排序的文件books.dat

到目前为止,这是我的代码:

连接按钮:

private void button1_Click(object sender, EventArgs e)
    {
      string[] strArray = new string[8];
      int index1 = 0;
      string text1 = this.txtAuthor.Text;
      strArray[index1] = text1;
      int index2 = 1;
      string str1 = ", ";
      strArray[index2] = str1;
      int index3 = 2;
      string text2 = this.txtDate.Text;
      strArray[index3] = text2;
      int index4 = 3;
      string str2 = ". ";
      strArray[index4] = str2;
      int index5 = 4;
      string text3 = this.txtTitle.Text;
      strArray[index5] = text3;
      int index6 = 5;
      string str3 = ". ";
      strArray[index6] = str3;
      int index7 = 6;
      string text4 = this.txtPublisher.Text;
      strArray[index7] = text4;
      int index8 = 7;
      string str4 = ".";
      strArray[index8] = str4;
      this.txtOutput.Text = string.Concat(strArray);          
    }

受保护的覆盖:

protected override void Dispose(bool disposing)
    {
      if (disposing && this.components != null)
        this.components.Dispose();
      base.Dispose(disposing);
    }

初始化组件:

private void InitializeComponent()
    {
            this.txtAuthor = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.txtDate = new System.Windows.Forms.TextBox();
            this.txtTitle = new System.Windows.Forms.TextBox();
            this.txtPublisher = new System.Windows.Forms.TextBox();
            this.txtOutput = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.btnSave = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtAuthor
            // 
            this.txtAuthor.Location = new System.Drawing.Point(14, 27);
            this.txtAuthor.Name = "txtAuthor";
            this.txtAuthor.Size = new System.Drawing.Size(100, 20);
            this.txtAuthor.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(14, 53);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(418, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Concatenate";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // txtDate
            // 
            this.txtDate.Location = new System.Drawing.Point(120, 27);
            this.txtDate.Name = "txtDate";
            this.txtDate.Size = new System.Drawing.Size(100, 20);
            this.txtDate.TabIndex = 2;
            // 
            // txtTitle
            // 
            this.txtTitle.Location = new System.Drawing.Point(226, 27);
            this.txtTitle.Name = "txtTitle";
            this.txtTitle.Size = new System.Drawing.Size(100, 20);
            this.txtTitle.TabIndex = 4;
            // 
            // txtPublisher
            // 
            this.txtPublisher.Location = new System.Drawing.Point(332, 27);
            this.txtPublisher.Name = "txtPublisher";
            this.txtPublisher.Size = new System.Drawing.Size(100, 20);
            this.txtPublisher.TabIndex = 6;
            // 
            // txtOutput
            // 
            this.txtOutput.Location = new System.Drawing.Point(14, 82);
            this.txtOutput.Name = "txtOutput";
            this.txtOutput.ReadOnly = true;
            this.txtOutput.Size = new System.Drawing.Size(418, 20);
            this.txtOutput.TabIndex = 8;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(11, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(38, 13);
            this.label1.TabIndex = 9;
            this.label1.Text = "Author";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(117, 11);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(30, 13);
            this.label2.TabIndex = 10;
            this.label2.Text = "Date";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(223, 11);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(27, 13);
            this.label3.TabIndex = 11;
            this.label3.Text = "Title";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(329, 11);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(50, 13);
            this.label4.TabIndex = 12;
            this.label4.Text = "Publisher";
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(14, 108);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(418, 23);
            this.btnSave.TabIndex = 13;
            this.btnSave.Text = "Save";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(446, 142);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtOutput);
            this.Controls.Add(this.txtPublisher);
            this.Controls.Add(this.txtTitle);
            this.Controls.Add(this.txtDate);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.txtAuthor);
            this.Name = "Form1";
            this.Text = "Book Details";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
    }

图书类:

private class Book
    {
      private string Author
      {
        get
        {
          return this.Author;
        }
        set
        {
          this.Author = value;
        }
      }

      private string Date
      {
        get
        {
          return this.Date;
        }
        set
        {
          this.Date = value;
        }
      }

      private string Title
      {
        get
        {
          return this.Title;
        }
        set
        {
          this.Title = value;
        }
      }

      private string Publisher
      {
        get
        {
          return this.Publisher;
        }
        set
        {
          this.Publisher = value;
        }
      }
    }

清空保存按钮:

private void btnSave_Click(object sender, EventArgs e)
        {         
        }

1 个答案:

答案 0 :(得分:1)

我建议你以错误的方式解决这个问题。我对这个问题的理解是你想从你GUI中的4个输入创建你的“Book”类,这是Serializable。在这种情况下,我会修改你的书类,以包含一个带有4个参数的构造函数(标题,作者,出版商和日期):

internal class Book
{
    public Book(string title, string author, string publisher, string date)
    {
        Title = title;
        Author = author;
        Publisher = publisher;
        Date = date;
    }

    public string Title { get; private set; }
    public string Author { get; private set; }
    public string Publisher { get; private set; }
    public string Date { get; private set; }

    public override string ToString()
    {
        return string.Format("{0}, {1}. {2}. {3}.", Author, Date, Title, Publisher);
    }
}

然后你的button1_Click方法看起来像:

private void button1_Click(object sender, EventArgs e)
{
    var title = this.txtTitle.Text;
    // var author, date, publisher are similar

    this.currentBook = new Book(title, author, publisher, date);
}

其中currentBook是Book。的类型成员变量。

最后,您的保存按钮方法现在看起来像:

private void btnSave_Click(object sender, EventArgs e)
{
    var currentBookText = this.currentBook.ToString();

    // write currentBookText to file. 
}

您可以查看:https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx,了解有关写入文件的信息。

然后,您可以轻松创建一个public static Book Book.FromString(string input)方法来反序列化您的图书文件。

C#还可以为您处理序列化和反序列化。有关详细信息,请查看此处:https://msdn.microsoft.com/en-us/library/et91as27.aspx