簡體   English   中英

Windows窗體使用C#將文本框輸入保存到不同類的列表中

[英]Windows form save textbox input to list in different class using C#

C#Windows窗體當列表位於不同的類中時,如何將文本框中的文本保存到列表中。 我有一個帶有多個文本框的form1。 我希望將輸入保存在不同類的列表中。 我在form1中的代碼

public void button1_Click(object sender, EventArgs e)
{
    minLista.add(textBox1.Text);
    Form2 Form1 = new Form2();
    this.Hide();
    Form1.Show();
}

在另一堂課我有

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

我究竟做錯了什么?

假設minList是Form2.cs中的公共屬性,則應執行以下操作

public void button1_Click(object sender, EventArgs e)
    {
        ThirdClass newthirdClass = new ThirdClass();
        newthirdClass.MinLista = new List<string>();
        newthirdClass.MinLista.Add(textBox1.Text);
        Form2 myForm2 = new Form2(newthirdClass);
        this.Hide();
        myForm2 .Show();
    }

請記住,約定是針對屬性(例如我為minLista提議的屬性),以大寫字母(即MinLista)開頭

編輯

既然您需要進入三等班,我會這樣做

public class ThirdClass
{
  public List<string> MinLista {get; set;}
}

public Form Form2
{
   private List<string> minLista;

   public Form2(List<string> mlist)
   {
      minLista = mlist;
   }

}

這樣,您將創建的對象(具有對所需列表的引用)注入到Form2中。

公開Minlista

public List<string> minLista = new List<string>();

然后使用

 public void button1_Click(object sender, EventArgs e)
{
    Classname a = new Classname();
    a.minLista.add(textBox1.Text);
    Form2 Form1 = new Form2();
    this.Hide();
    Form1.Show();
}

其中, Classname是您在其中聲明minLista的類的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM