c#从另一种形式执行方法来执行命令

时间:2014-01-15 23:27:41

标签: c# winforms

我使用另一种形式的方法,但它不起作用,这是我的代码里面的样本 尝试调试并设置断点但没有返回错误。 请告诉我我错过了什么或者我需要做什么 这是我做过的第一个c#程序

//form main :    
public partial class frmMain : RibbonForm
{
      Form frm;
      public frmMain()
      {
         InitializeComponent();
      }

      Form form_name
      {
      get
      {
       if (ActiveMdiChild == null)
          return null;
       else
          return ActiveMdiChild;
      }
      }
   //call or show child form from frmMain
   private void biBarang_ItemClick(object sender, ItemClickEventArgs e)
   {
      foreach (Form form in MdiChildren)
      {
         if (form is frmBarang)
         {
            frm = form;
            break;
         }
      }
      if(frm == null)
      {
         frm = new frmBarang() { MdiParent = this };
         frm.Show();
      }
      else
      {
         frm.Activate();
      }
         frm = null;
      }
   }

//execute insert method in frmBarang from frmMain
private void btnSimpan_ItemClick(object sender, ItemClickEventArgs e)
{
   if(form_name == null)
   {
      MessageBox.Show("Sorry no page!");
   }
   else
      if(form_name is frmBarang)
      {
         frmBarang brg = new frmBarang();
         brg.ins_data(); 
      }
}

//frmBarang insert data execution    
public void ins_data()
{
   //this condition is not work, 
   //Try debugging (set breakpoint at the code line calling if (error provider set to false) 
   //but it always jump at else condition
   if (DxErrorProvider1.HasErrors == true)
   {
      MessageBox.Show("Data belum lengkap", "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
   }
   else
   {
      dml.query = string.Format("INSERT INTO barang (barcode, id_supplier, kategori, nama, harga_jual, harga_beli, stok, stok_minimal, bisa_dijual, kadaluarsa) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", txtBarcode.Text, cboSupplier.EditValue, txtKategori.Text, txtNama.Text, txtHargaJual.EditValue, txtHargaBeli.EditValue, txtJumlahStok.EditValue, txtStokMinimal.EditValue, general.status, string.Format("yyyy-MM-dd",dtKadaluarsa.EditValue));
      dml.iud_data(dml.query);
      MessageBox.Show("Data berhasil disimpan", "Berhasil", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }

   //or i set textbox value to null and nothing happen
   txtNama.Text=null;
}
}

0 个答案:

没有答案