Adding Item in listbox on parent form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suexicano
    New Member
    • Nov 2009
    • 6

    Adding Item in listbox on parent form

    Hi -

    I call a method (method is within the parent form) in the child form right before I close the child form. looks something like this:

    m_parent.GetOrd erDetailsFunc() ;
    this.Close();


    The method should add an items to a listbox located on the parent form, but it does not add the items. I had strategically placed a message box to make sure that the code was being executed, and it is working. Yet, the listbox is blank.

    Can someone help? Or, am I going about this incorrectly?

    Thank you for your time and help!!

    - suexicano
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Well everything you suggest, suggests what you have posted is working fine. You might like to post the code within GetOrderDetails Func() or at least something that interacts with a listbox.

    Also I don't recommend ending you're methods with Func(), its not standard and methods are called methods, rather than functions in C#.

    Comment

    • suexicano
      New Member
      • Nov 2009
      • 6

      #3
      Thanks Ian for responding....

      Here is the condensed code within the child form....
      public partial class RaspadoForm : Form
      {
      Form1 m_parent = new Form1();

      private void EnterButton_Cli ck(object sender, EventArgs e)
      {
      conn.Open();
      SqlCommand InsertOrderDeta il = new SqlCommand("INS ERT INTO
      OrderDetailsTb (OrderID, ProductID, Quantity, UnitPrice) VALUES('" +
      OrderDetailID + "', '" + OrderProductID + "', '" + QuantityAmount + "', '"
      + OrderProductUni tPrice + "');", conn);
      InsertOrderDeta il.ExecuteNonQu ery();
      conn.Close();
      conn.Dispose();

      m_parent.GetOrd erDetails();

      this.Close();
      }
      }


      This is the GetOrderDetails () method on the parent form:

      public void GetOrderDetails ()
      {
      MessageBox.Show ("Function called");
      DataTable OrderDataTb = new DataTable();

      conn.Open();

      SqlCommand GetOrderDetails Query = new SqlCommand("SEL ECT *
      FROM OrderDetailView WHERE OrderID='" + OrderID + "'", conn);
      SqlDataAdapter GetOrderDetails Adapter = new SqlDataAdapter( );
      GetOrderDetails Adapter.SelectC ommand = GetOrderDetails Query;
      GetOrderDetails Adapter.Fill(Or derDataTb);
      conn.Close();

      int CountOfDetailRo ws;
      CountOfDetailRo ws = OrderDataTb.Row s.Count;

      if (CountOfDetailR ows > 0) {
      string OrderD;

      listBox1.Items. Clear();

      for (int i = 0; i < CountOfDetailRo ws; i++)
      {
      OrderD = OrderDataTb.Row s[i]["SizeName"].ToString() + ", " + OrderDataTb.Row s[i]["PrCategory "].ToString() + " de " + OrderDataTb.Row s[i]["Flavor"].ToString() + " " + OrderDataTb.Row s[i]["ExtraDesc"].ToString() + " - " + OrderDataTb.Row s[i]["Quantity"].ToString() + " @ " + String.Format(" {0:C}", Convert.ToDecim al(OrderDataTb. Rows[i]["UnitPrice"])) + "....... " + String.Format(" {0:c}", Convert.ToDecim al(OrderDataTb. Rows[i]["LineSubtot al"]));

      listBox1.Items. Add(OrderD);
      MessageBox.Show ("Here: " + CountOfDetailRo ws + OrderDataTb.Row s[i]["SizeName"].ToString() + ", " + OrderDataTb.Row s[i]["PrCategory "].ToString() + " de " + OrderDataTb.Row s[i]["Flavor"].ToString() + " " + OrderDataTb.Row s[i]["ExtraDesc"].ToString() + " - " + OrderDataTb.Row s[i]["Quantity"].ToString() + " @ " + String.Format(" {0:C}", Convert.ToDecim al(OrderDataTb. Rows[i]["UnitPrice"])) + "....... " + String.Format(" {0:c}", Convert.ToDecim al(OrderDataTb. Rows[i]["LineSubtot al"])));

      }

      }
      }


      As you can see, I placed a Messagebox within the code to see if it was processing, which it does get fired. The Method does update the listbox when I am on the parent form(I placed a test button on the parent form to trigger it), but when I am on the child form, it does not update the list box.

      Thank you for your help!

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I would further advise to stop accessing the parts of one form directly from another form. It ties the two together in a tight and ugly way.

        From form2 raise an event with an argument. The arguement is the new item to be inserted in the to the listbox. Form1 subscribes to form2's event.

        This way form2 doesn't have to know anything about form1 or its controls.
        Let form1 be responsible for form1. let form2 be responsible for form2.

        Comment

        • suexicano
          New Member
          • Nov 2009
          • 6

          #5
          Thank you for the advice. I am a beginner and tips like that are valued from those who know better programming practises. I will have to figure out how to evoke the method in fchild orm once I close the child form

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Once you close the form nothing exists, so you aren't going to invoke any methods.

            Comment

            Working...