2014-01-10 26 views
1

我有一個窗體類,其構造函數接受一堆參數,這些參數用於填充窗體上的控件。它們由一個複選框,一對夫婦組合框,一個日期時間選擇器和一對文本框組成。表單呈現時,文本框顯示爲空白。如果我單步執行代碼,我會看到正確設置了textbox.Text字段,但是當調用form.Show時,值將被清除。所有其他控件保留傳遞給構造函數的值。文本框的值在表單上清除。顯示

這些表格都是動態構建的。我不明白爲什麼會發生這種情況,在構造函數和渲染之間不會運行其他代碼。

public partial class Disposition : Form 
{ 
    string _ncmID; 
    int _baseNumber; 
    string _type; 
    string _by; 
    string _partNo; 
    DateTime _date; 
    int _qty; 
    bool _supplierCaused; 
    string _notes; 
    DateTime _dateReturned; 
    frmMain _parentForm; 
    GroupBox gbDisposition; 
    CheckBox cbSupplierCaused; 
    TextBox txtDispQuantity;   
    Label lblQuantity; 
    DateTimePicker dtDate; 
    Label lblDate; 
    ComboBox cboBy; 
    Label lblBy; 
    TextBox txtNotes; 
    Label lblNotes; 
    DateTimePicker dtDateReturned; 
    ComboBox cboPart; 

public Disposition(frmMain parent, string type, string by, DateTime date, int qty, bool supplierCaused, 
     string partNo, string notes, string ncmID, int baseNumber, DateTime dateReturned) 
    { 
     InitializeComponent(); 

     _parentForm = parent;    
     _type = type; 
     _by = by; 
     _qty = qty; 
     _date = date; 
     _supplierCaused = supplierCaused; 
     _partNo = partNo; 
     _notes = notes; 
     _ncmID = ncmID; 
     _baseNumber = baseNumber; 
     _dateReturned = dateReturned; 

     switch (_type) 
     { 
      case "Scrap":      
       DrawScrap(true); 
       break; 
      case "Rework": 
       DrawRework(true); 
       break; 
      case "Return to Vendor": 
       DrawReturn(true); 
       break; 
      case "Use as Is": 
       DrawUse(true); 
       break; 
      case "Void": 
       DrawVoid(true); 
       break; 
      default: 
       break; 
     } 

    } 

每個繪圖方法都向窗體添加控件,例如,

txtDispQuantity = new TextBox(); 
txtDispQuantity.Location = new Point(248, 31); 
txtDispQuantity.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F); 
txtDispQuantity.Size = new Size(55, 20); 
txtDispQuantity.Validating += new CancelEventHandler(txtDispQuantity_Validating); 
txtDispQuantity.Text = _qty.ToString(); 
gbDisposition.Controls.Add(txtDispQuantity); 

所以我只是設置Textbox的Text屬性,然後將其添加到groupbox。如果我在調試器中檢查課程,一切都顯得很好,但一旦Show被稱爲 ,Text字段將被清除並在觀察窗口中顯示爲紅色。所有其他控件保留在Draw方法中設置的值。

form_load中發生的唯一事情是組合框設置(向項目列表添加字符串)。

private void txtDispQuantity_Validating(object sender, CancelEventArgs e) 
{ 
    int q; 

    if (!Int32.TryParse(txtDispQuantity.Text, out q)) 
    { 
     MessageBox.Show("Please enter an integer value for the quantity", "NCM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
    } 
} 

無處的代碼我在這些控件的文本字段設置爲任何東西,但在數據庫中檢索的值,這是從來沒有空,因爲驗證不允許它...

+6

你能分享代碼嗎? – Ramashankar

+0

你使用'txtboxId.Text'來設置文本框的文本嗎? –

+0

你確定文本框在Form_Load中沒有被清除(稱爲你顯示錶單)嗎? – Akrem

回答

0

匝由於邏輯缺陷,表單上的另一個控件(組合框)拋出異常,但仍顯示正確的值。謝謝您的幫助。