如何将列表框中的项目添加到c#

时间:2015-12-16 20:04:24

标签: c# .net winforms

我正在尝试在c#中创建一个简单的库应用程序。我目前正在尝试创建一个编辑表单 - 用户将单击列表框中显示的书籍,然后打开编辑表单。打开编辑表单时,我希望书籍详细信息(如作者,标题等)显示在编辑表单的相关文本框中。有没有人有关于我如何做到这一点的任何提示?顺便提一句,我对此还是比较新的。到目前为止,这是我的代码:

 private void lstBooks_SelectedIndexChanged(object sender, EventArgs e)
    {
        string currentBook = lstBooks.SelectedItem.ToString();


    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        lstBooks_SelectedIndexChanged(null, null);

        frmEditBook tempEditBook = new frmEditBook();
        tempEditBook.Show();
        frmkeepBookstore.Hide();


    }

书类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace prjBooks
{
class Book
{
    private string isbn;
    private string title;
    private string author;
    private int yearPublished;
    private int numCopies;

    public Book(string theAuthor, string theTitle, string theYearPublished,  string theNumCopies, string theIsbn)
    {
        isbn = theIsbn;
        title = theTitle;
        author = theAuthor;
        setYearPublished(theYearPublished);
        setNumCopies(theNumCopies);
    }

    //getters

    public string getIsbn()
    {
        return isbn;
    }

    public string getTitle()
    {
        return title;
    }

    public string getAuthor()
    {
        return author;
    }

    public int getYearPublished()
    {
        return yearPublished;
    }

    public int getNumCopies()
    {
        return numCopies;
    }

    // setters

    public void setIsbn(string inIsbn)
    {
        isbn = inIsbn;
    }

    public void setTitle(string inTitle)
    {
        title = inTitle;
    }

    public void setAuthor(string inAuthor)
    {
        author = inAuthor;
    }

    public void setYearPublished(string inYearPublished)
    {
        try
        {
            yearPublished = Convert.ToInt32(inYearPublished);
        }
        catch (FormatException e)
        {
            System.Windows.Forms.MessageBox.Show("ERROR:" +
                    e.Message + " Please input a valid year");
        }
    }

    public void setNumCopies(string inNumCopies)
    {
        try
        {
            numCopies = Convert.ToInt32(inNumCopies);
        }
        catch (FormatException e)
        {
            System.Windows.Forms.MessageBox.Show("ERROR:" + e.Message
                                + " Please input a number of copies:");
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

我没有太多地阅读你的代码,但我会回复你的标题。 如果要以单独的形式将文本从列表框添加到文本框,只需打开带有参数的表单。参数应该是文本框,您希望以单独的形式使用它们。

Form2 f2 = new Form2(textBox1);
f2.Show();

单独表格:

TextBox x;
public Form2(TextBox y)
{
       InitializeComponent();
       x = y;
}

然后您可以像这样轻松地将文本添加到文本框中:

x.Text = listBox1.SelectedItem.ToString();

也许我有点困惑,从你想传输文字的地方。

修改

或许最好将其作为参数列表框

Form1中:

frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();

窗体2:

ListBox listBoxBooks;
public frmEditBook(ListBox lstBooks)
{
      InitializeComponent();
      listBoxBooks = lstBooks;
}

private void frmEditBook_Load(object sender, EventArgs e)
{
      textBoxName.Text = listBoxBooks.SelectedItem.ToString();
}

我试图保留你的控件名称。