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

本文关键字:数组 一个 简单 存储 文本 4个 数据 | 更新日期: 2023-09-27 18:03:21

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

这就是我需要做的:

将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();
    }

Book类:

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)
        {         
        }

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

我认为你处理这件事的方法是错误的。我对问题的理解是,您希望从GUI中的4个输入创建"Book"类,这是可序列化的。在这种情况下,我将修改您的book类,以包含一个构造函数,它接受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