2012-08-07 46 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

相关问题