2012-08-07 43 views
-1

我即將放棄這一點,我試圖將一個字符串傳遞給另一個類,並在該類中保存到列表中,但它不會這樣做。我知道List存在並且工作,因爲我製作了一個按鈕,將一個字符串放入該列表並在列表框中顯示。但由於某種原因,我不能讓它在傳遞的字符串上工作。不接受從另一個類傳遞的值的變量

這是兩個winforms讓我們的用戶給出的信息,然後它的處理和傳遞給另一個類,然後應保存到列表中,然後我要求更新應顯示在列表框中的列表。

的MainForm(帶有列表框)

public partial class MainForm : Form 
{   

    List<string> m_customers = new List<string>();  


    public MainForm() 
    { 
     InitializeComponent();    
    } 


    public void StringToList(string strnew) 
    {   
     m_customers.Add(strnew); 
     Updatelistbox(); 

     foreach (string custom in m_customers) 
     { 
      lstRegistry.Items.Add(custom); 
     } 
    } 

    private void strtest(string strnew) 
    { 
     string userinfo = strnew; 
     m_customers.Add(userinfo); 
    } 

    private void Updatelistbox() 
    { 
     lstRegistry.Items.Clear(); 
     for (int index = 0; index < m_customers.Count; index++) 
     { 
      lstRegistry.Items.Add(m_customers[index]); 
     } 
    }  

    private void button1_Click(object sender, EventArgs e) 
    { 
     using (ContactForm frm = new ContactForm()) 
     { 
      frm.ShowDialog(); 
     } 
    } 


    private void button2_Click(object sender, EventArgs e) 
    {     
     lstRegistry.Items.Add("Hey this works atleast..."); 

     m_customers.Add("add this to List"); //This works as this line becomes more and more. 
     foreach (string custom in m_customers) 
     { 
      lstRegistry.Items.Add(custom); 
     }   
    } 
} 

的inputform

public partial class ContactForm : Form 
{ 
    private ContactFiles.Contact m_contact = new ContactFiles.Contact(); 
    private ContactFiles.Email m_email = new ContactFiles.Email(); 
    private ContactFiles.Phone m_phone = new ContactFiles.Phone(); 
    private ContactFiles.Adress m_adress = new ContactFiles.Adress(); 

    private bool m_closeForm; 

    public ContactForm() 
    { 

     InitializeComponent(); 

     InitializeGUI(); 
    } 

    private void InitializeGUI() 
    { 
     txtFirstName.Text = string.Empty; 
     txtLastName.Text = string.Empty; 
     txtHomePhone.Text = string.Empty; 
     txtCellPhone.Text = string.Empty; 
     txtEmailBusiness.Text = string.Empty; 
     txtEmailPrivate.Text = string.Empty; 
     txtStreet.Text = string.Empty; 
     txtCity.Text = string.Empty; 
     txtZipCode.Text = string.Empty; 
     FillCountryComboBox(); 
     cmbCountries.Items.AddRange(FillCountryComboBox()); cmbCountries.SelectedIndex = 5; 
     m_closeForm = true; 
    } 


    public string[] FillCountryComboBox() 
    { 
     string[] m_countryStrings = Enum.GetNames(typeof(Countries)); 

     for (int index = 0; index < m_countryStrings.Length - 1; index++) 
     { 
      m_countryStrings[index] = m_countryStrings[index].Replace("_", " "); 
     } 
     return m_countryStrings; 
    } 

    private void btnOK_Click(object sender, EventArgs e) 
    {   
     string a_country = cmbCountries.SelectedItem.ToString(); 
     var oAdress = new ContactFiles.Adress(txtStreet.Text, txtCity.Text, txtZipCode.Text, a_country); 
     string adresslist = oAdress.ToString();    
     var oEmail = new ContactFiles.Email(txtEmailBusiness.Text, txtEmailPrivate.Text); 
     string emaillist = oEmail.ToString();    
     var oPhones = new ContactFiles.Phone(txtHomePhone.Text, txtCellPhone.Text); 
     string phonelist = oPhones.ToString(); 
     //This is actually working, the string is passed OK.    
     //MainForm strin = new MainForm();    
     var oContact = new ContactFiles.Contact(txtFirstName.Text, txtLastName.Text); 
     string namelist = oContact.ToString(); 

     //Create string from input and send to MainForm.StringToList() 
     MainForm instance = new MainForm(); 
     string strnew = string.Format("{0,-3} {1, -10} {2, -20} {3, -30}", namelist, phonelist, emaillist, adresslist); 
     instance.StringToList(strnew); 


     this.Close(); 
    } 

    private ContactFiles.Contact Contacts 
    { 
     get { return m_contact; } 
     set 
     { 
      if (value != null) 
       m_contact = value;     
     } 
    } 

    public ContactFiles.Email Email 
    { 
     get { return m_email; } 
     set 
     { 
      if (value != null) 
      m_email = value; 
     } 
    } 

    public ContactFiles.Phone Phone 
    { 
     get { return m_phone; } 
     set 
     { 
      if (value != null) 
       m_phone = value; 
     } 
    } 

    private ContactFiles.Adress Adress 
    { 
     get { return m_adress; } 
     set 
     { 
      if (value != null) 
       m_adress = value; 
     } 
    } 

    private void ContactForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     if (m_closeForm) 
      e.Cancel = false; //Close the Contact form. 
     else 
      e.Cancel = true; //Do not close (user has chosen Cancel) 
    }  
} 

有,你可以看到一些更多的類來處理包含構造的用戶輸入,但他們作爲一切工作我可以從所有的用戶輸入創建字符串,但然後我失敗了大量的時間,我和它的像3天現在,我仍然無法找到問題。 :'(

就如何解決我的問題,任何想法,我只是不能找到問題

+0

描述爲「不起作用」。運行時異常或編譯錯誤? – 2012-08-07 20:54:20

+1

你可以指定它在所有代碼中實際失敗的位置嗎?什麼是將字符串設置爲目標變量,以及在哪一行讀取它,並期望它在那裏時不在那裏? – David 2012-08-07 20:54:39

+0

列表沒有得到該值。 – user1501127 2012-08-07 20:54:49

回答

2
MainForm instance = new MainForm(); 
    string strnew = string.Format("{0,-3} {1, -10} {2, -20} {3, -30}", namelist, phonelist, emaillist, adresslist); 
    instance.StringToList(strnew); 

這是不好的你正在創建主窗體的一個新實例(使用新的列表)?!?。而不是使用舊的。合格名單聯繫表的conctructor和增加價值在那裏。

List<string> m_customers; 

public ContactForm(List<string> list) : this() 
{ 
    m_customers = list; 
} 

創建表單代碼

private void button1_Click(object sender, EventArgs e) 
{ 
    using (ContactForm frm = new ContactForm(m_customers)) 
    { 
     frm.ShowDialog(); 
    } 
} 

現在您可以將值添加到列表中。

更新:將您的StringToList方法帶到聯繫人窗體中,使其成爲私人並在聯繫人窗體實例中使用它。

+0

我在這裏,我在你臉上種了一個大的溼吻! :P非常感謝!我已經在這3天了!我知道那裏有一些小東西我錯過了...最後它將字符串放入列表中。非常感謝您的幫助!!!現在我可以主演其他百萬個小細節,但這就是爲什麼我們有夜晚,對吧?! ;)再次感謝Dantix! – user1501127 2012-08-07 21:16:03

+1

不是問題=) – dantix 2012-08-07 21:18:56

1

你可能會嘗試讓你的主窗體有一個輸入窗體的實例,而不是相反。主要的形式是最終要保存數據。讓輸入表單成爲一個新實例並以這種方式返回值。

+0

絕對!我正在倒退:P在這裏遲了;)謝謝! – user1501127 2012-08-07 21:17:05

相關問題