简体   繁体   English

(c# + windows 窗体) 将项目添加到不同类中的 listBox

[英](c# + windows forms) Adding items to listBox in different class

I have two classes(forms), and I would like an item from class2 to be added to listBox in class1 when I click "Accept" button.我有两个类(表单),当我单击“接受”按钮时,我希望将class2一个项目添加到class1 listBox

I tried with the following code, but nothing changes in the listBox:我尝试使用以下代码,但列表框中没有任何变化:

private void button1_Click(object sender, EventArgs e)
{
    CarRental i = new CarRental();
    string id = idRental.Text.ToString();

    i.listBox1.Items.Add(id);
    i.listBox1.Update();
    this.Close();
}

Where did I make the mistake?我在哪里犯了错误?

Declare RentalId property on Form2 .Form2上声明RentalId属性。 And at CarRental form (your first form) do following:CarRental表格(您的第一个表格)中执行以下操作:

using(Form2 form2 = new Form2())
{
    if (fomr2.ShowDialog() != DialogResult.OK)
        return;

    listBox.Items.Add(form2.RentalId);
}

Implement Fomr2.RentalId property this way: Fomr2.RentalId这种方式实现Fomr2.RentalId属性:

public string RentalId
{
   get { return idRental.Text; } // you don't need ToString() call
}

Then select your "Accept" button and set its DialogResult property to OK .然后选择您的“接受”按钮并将其DialogResult属性设置为OK Thus clicking on that button will close your dialog form and return DialogResult.OK .因此,单击该按钮将关闭您的对话框窗体并返回DialogResult.OK

you created a new entity of type CarRental.您创建了一个 CarRental 类型的新实体。 what you should do is to send the first form to the second on construct, and modify things through that instance.您应该做的是在构造时将第一个表单发送到第二个表单,并通过该实例修改内容。

You need to access the open form instead of creating new instance of CarRental form您需要访问打开的表单而不是创建 CarRental 表单的新实例

private void button1_Click(object sender, EventArgs e)
{
    CarRental i = (CarRental)Application.OpenForms["CarRentalFormObjectName"];
    string id = idRental.Text.ToString();

    i.listBox1.Items.Add(id);
    i.listBox1.Update();
    this.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#从不同的类向MainForm ListBox添加项 - C# Adding items to MainForm ListBox from a different class 使用第二种形式将项目添加到C#中不同类的列表框中 - Adding items to a listbox on a different class in C# using a second form 在Windows窗体应用程序C#中更改列表框项目对齐方式 - Change listbox items alignment in windows forms application C# 将项目添加到Windows窗体应用程序的listBox中, - Adding Items to a listBox in a Windows Forms Application, 从不同的 class 将项目添加到列表框 - Adding items to a listbox from a different class 添加以不同标签显示的列表框项目和值(在不同表单之间) - adding listbox items and values showing it in different labels (between different forms) 重新排序ListBox中的项目-Windows窗体-(Java程序员学习C#) - Re-order Items in a ListBox - Windows Forms - (Java programmer learning C#) Select / 根据在一个列表框中选择一个项目取消选择多个列表框中的项目 - C# Windows forms - Select / Deselect Items in multiple listboxes based on selection of an item in one listbox - C# Windows forms Windows 8 C# - 如何保留ListBox项? - Windows 8 C# - how to keep ListBox items? 为列表框中的每个项目添加价格 - C# .Net、Windows 窗体应用程序 - Adding a price to each item in a listbox - C# .Net, Windows Forms Applications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM