0

旅行表格,送货表格,取货表格

在主窗体上,我有一个按钮,可将您带到您将一些数据输入到文本框中的行程,然后当您按添加时,它将添加取件或交付,具体取决于您listbox在主窗体上输入的数据方式

我将如何获得它,以便如果我将交付添加到listbox主表单中,它还会将交付添加到listbox交付表单中的a

或者,如果我listbox在主表格上添加一个取件,它也会listbox在取件表格上添加一个取件

我如何添加交付的一些示例代码:

主窗体添加交付按钮:

    private void btnDelivery_Click(object sender, EventArgs e)
    {


        deliveryForm.deliverytrips = new DeliveryTrips();
        //New delivery- note added to the deliveryForm object


        deliveryForm.ShowDialog();
        //Show the deliveryForm. ShowDialog ensures that the form has the exclusive focus until it is closed.

        if (deliveryForm.deliverytrips != null)
        //if null then the "cancel" button was pressed
        {
            DeliveryTrips newApp = deliveryForm.deliverytrips;
            //Get the delivery object from the form

            theDelivery.addDeliveryTrip(newApp);
            //Add the delivery to the summary
        }
        updateList();
        //Update the list object to reflect the delivery in the summary

将交付添加到主窗体上的列表框的旅行代码:

    public partial class frmDelivery : Form
    {
    private DeliveryTrips theDeliveryTrips;
    //The DeliveryTrips object being created/edited


    public DeliveryTrips deliverytrips
    {   //Property to allow access to theDeliveryTrips
        get { return theDeliveryTrips; }
        set { theDeliveryTrips = value; }
    }

    public frmDelivery()
    {   //Constructor
        InitializeComponent();
    }


    private void frmDelivery_Load(object sender, EventArgs e)
    {
        //When makeing the form visible, set the text boxes to reflect the values in
        //theDeliveryTrips.

        if (theDeliveryTrips != null)
        {

            txtDescription.Text = "Delivery";
            txtDescription.ReadOnly = true;
            txtCustomerName.Text = theDeliveryTrips.customername;
            txtCustomerAddress.Text = theDeliveryTrips.customeraddress;
            txtTime.Text = theDeliveryTrips.arrivaltime.ToString();


        }

    }


    private void btnAdd_Click_1(object sender, EventArgs e)
    {


        theDeliveryTrips.description = txtDescription.Text;
        theDeliveryTrips.customername = txtCustomerName.Text;
        theDeliveryTrips.customeraddress = txtCustomerAddress.Text;
        theDeliveryTrips.arrivaltime = TimeSpan.Parse(txtTime.Text);

        this.Hide();
        //Hide the forum
    }

    private void btnCancel_Click_1(object sender, EventArgs e)
    {
        theDeliveryTrips = null;
        //Set theDeliveryTrips to null as to remove any changes made

        this.Hide();
        //Hide the forum
    }
}
4

1 回答 1

0

You could create a public method in your frmDelivery class:

public void ListBoxAdd(string item)
{
     listbox_frmDelivery.Items.Add(item); // or whatever your list box is called
}

then in your main where you initialized frmDelivery you can call:

deliveryForm.ListBoxAdd("Added From Main Form by pub method");

or you can search for it on your main form:

        int indexOfListBox = deliveryForm.Controls.IndexOfKey("frmDelivery listbox id");
        if (indexOfListBox != -1)// if not -1 we found it
        {
            ListBox deliveryListBox = (ListBox)deliveryForm.Controls[indexOfListBox];
            deliveryListBox.Items.Add("Added from Main form by searching index");
        }
于 2012-11-26T20:25:26.417 回答