2012-11-26 29 views
0

行程表,發貨單,在主窗體上皮卡形式添加到數據到2個不同的列表框上的2種不同形式的

我有一個按鈕,把你的旅行從那裏你輸入一些數據轉換成文本,然後裝箱當你按添加它會或者添加一個皮卡或交付取決於你輸入到主表格

你輸入什麼數據我將如何得到它,所以如果我添加一個交貨到listbox在主窗體中還將交貨添加到listbox的交貨形式

或者如果我添加了pi ckup到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 

CODE FOR從到添加發送到列表框控件ON主要形式有:

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 
    } 
} 
+0

您是否嘗試將它們全部設置爲使用相同的DataContext? –

+0

我該怎麼做,即時通訊新的C#所以不長的開始學習如何編碼在C# – Michael

+0

考慮使用反應式擴展。它也有一個調度程序,尊重UI上下文 –

回答

0

你可以在你的frmDelivery類中創建一個公共方法:

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

然後在你的主,你初始化frmDelivery您可以撥打:

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

你可以搜索你的主形式:

 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"); 
     } 
+0

不會將主'listbox'上的所有項目交給表單中的'listbox'? – Michael

+0

「我將如何得到它,這樣如果我在主窗體中添加一個傳遞到列表框,它也會將傳遞添加到交付窗體中的列表框中。」肯定會的。這不是你的要求嗎?如果不是,我可能會誤解! – clamchoda

相關問題