将列表变量绑定到文本框

时间:2011-06-26 23:58:41

标签: c# textbox

我有一个新问题。我有一个代码从二进制文件加载列表,我想我做得对,但我需要让每个变量显示在自己的文本框中。这是我无法找到怎么做的。任何人都可以帮助我或指出我可以找到信息的地方吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace test
 {

public partial class Form1 : Form
{
    [Serializable]
    public class ore
    {
        public float Titan;
        public float Eperton;
    }
    ore b1 = null;
    ore b2 = null;



    public Form1()
    {
        InitializeComponent();

        b2 = new ore();
        b1 = new ore();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        float tempFloat;


        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b1.Titan = tempFloat;
        }
        else
            MessageBox.Show("uh oh");



    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b2.Eperton = tempFloat;
        }
        else
            MessageBox.Show("uh oh");


    }


    private void button1_Click(object sender, EventArgs e)
    {
        List<ore> oreData = new List<ore>();
        oreData.Add(b1);
        oreData.Add(b2);

        FileStream fs = new FileStream("ore.dat", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, oreData);
        fs.Close();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    List<ore> books = new List<ore>();
    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        List<ore> books = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (books.Count > 1)
        {
            textBox3.Text = books[0];//update the first text
            textBox4.Text = books[1];//update the second text
        }
    }
}

}

3 个答案:

答案 0 :(得分:0)

一种解决方案是使用ListView控件而不是一堆Textbox。在Listview的模板中,放置Textbox控件并将其绑定到List对象。

或者,动态创建文本框控件。

for(int i=1; i<=books.Count; i++)
{
  var textBoxCtrl = new TextBox()
  textBoxCtrl.ID = "TextBox"+i.toString();
  textBoxCtrl.Text = books[i];

  Page.Controls.Add(textBoxCtrl);
}

答案 1 :(得分:0)

Windows窗体:

1-在课程级别定义您的列表书籍,以便在需要时可以在其他方法中使用它。

2-

public clas MyForm : Form
{
    List<ore> books = new List<ore>();//define at the class scope

    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        books = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (books.Count > 1)
        {
            textBox3.Text = books[0];//update the first text
            textBox4.Text = books[1];//update the second text
        }
    } 
}

如果您需要收集列表框以便在图书清单发生变化时添加和删除,那么这是另一个故事...写评论或更新您的问题......

答案 2 :(得分:-1)

在ASP.NET中: 您可以使用ASP.NET FormView对象创建表单,然后将数据集绑定到表单并调用DataBind。

<asp:FormView runat="server" ID="form1">
    <ItemTemplate>
        <asp:TextBox Text='<%# Bind("") %>'></asp:TextBox>
    </ItemTemplate>
</asp:FormView>
相关问题