当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Add items from a listbox to another listbox on another form

问题描述:

Here is my code.

private void PlaceOrder_Click(object sender, EventArgs e)

{

MenuBox.Items.Clear();

TotalBox.Items.Clear();

total.Clear();

ordertotal = 0;

}

I want to add what is in the menu box to a another list box on another form.

Update

(added by jp2code)

Form1 (Main):

namespace WindowsFormsApplication1 {

public partial class RESTAURANT : Form

{

double soup = 2.49;

double ordertotal;

public RESTAURANT()

{

InitializeComponent();

}

private void RESTAURANT_Load(object sender, EventArgs e)

{

}

private void Add_Click(object sender, EventArgs e)

{

MenuBox.Items.Add("Soup");

TotalBox.Items.Add(String.Format("{0:C}", soup));

ordertotal += soup;

total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));

}

private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)

{

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

{

}

private void PlaceOrder_Click(object sender, EventArgs e)

{

new AreYouSure().Show();

this.Show();

MenuBox.Items.Clear();

TotalBox.Items.Clear();

total.Clear();

ordertotal = 0;

}

}

}

Form2 (Confirmation)

namespace WindowsFormsApplication1 {

public partial class Confirmation : Form

{

public Confirmation()

{

InitializeComponent();

}

private void label1_Click(object sender, EventArgs e)

{

}

private void Confirmation_Load(object sender, EventArgs e)

{

}

private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)

{

}

}

}

When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2

网友答案:
OtherForm.OtherListbox.Items.Clear(); 
foreach(var itm in MenuBox.Items)
    OtherForm.OtherListbox.Items.Add(itm);
网友答案:

Form1 (Main):

namespace WindowsFormsApplication1 {

public partial class RESTAURANT : Form
{
    double soup = 2.49;

    double ordertotal;

    public RESTAURANT()
    {
        InitializeComponent();
    }

    private void RESTAURANT_Load(object sender, EventArgs e)
    {

    }

    private void Add_Click(object sender, EventArgs e)
    {
        MenuBox.Items.Add("Soup");
        TotalBox.Items.Add(String.Format("{0:C}", soup));
        ordertotal += soup;
        total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
    }

    private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void PlaceOrder_Click(object sender, EventArgs e)
    {

        new AreYouSure().Show();
        this.Show();

        MenuBox.Items.Clear();
        TotalBox.Items.Clear();
        total.Clear();
        ordertotal = 0;

    }

}

}

Form2 (Confirmation)

namespace WindowsFormsApplication1 {

public partial class Confirmation : Form
{
    public Confirmation()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void Confirmation_Load(object sender, EventArgs e)
    {

    }

    private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2

网友答案:

It is better that the controls on your other form (ListBox, in this case) are set to Private by default.

In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.

Consider something like this:

public void ListBoxData(object[] array)
{
  listBox1.Clear();
  listBox1.AddRange(array);
}

To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:

public object SelectedItem { get { return listBox1.SelectedItem; } }

I hope that is what you were looking for.

UPDATE:

Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.

If you had a ComboBox, you could do something like follows:

public partial class Confirmation : Form
{

    private ComboBox comboBox1;

    public void AddRange(object[] array)
    {
        comboBox1.Items.AddRange(array);
    }

}

That does not place the ComboBox anywhere on your form. You would need to work that out.

With that done, I'm guessing you need to edit your PlaceOrder_Click routine:

    private void PlaceOrder_Click(object sender, EventArgs e)
    {
        //new AreYouSure().Show();
        //this.Show();
        using (var obj = new Confirmation())
        {
            var list = new List<object>(MenuBox.Items.Count);
            foreach (var o in MenuBox.Items)
            {
                list.Add(o);
            }
            obj.AddRange(list.ToArray());
            if (obj.ShowDialog(this) == DialogResult.OK)
            {
                MenuBox.Items.Clear();
                TotalBox.Items.Clear();
                total.Items.Clear();
                ordertotal = 0;
            }
        }
    }

If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.

Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU

分享给朋友:
您可能感兴趣的文章:
随机阅读: