テキストファイルを文字列リストに読み込んでリストボックスに表示する問題に取り組んでいますが、今はボタンイベントで空白になっているリストボックスのリストの内容。私はVBでこれを簡単に行うことができますが、私はC#にはかなり新しいです。winformsを使用してC#でメソッド間のリストを操作する
namespace texttoarray
{
public partial class Form1 : Form
{
public int counter;
public List<string> finalList;
public Form1()
{
InitializeComponent();
List<string> finalList = AddToList();
}
public List<string> AddToList()
{
counter = 0;
string line;
List<string> list = new List<string>();
System.IO.StreamReader file = new System.IO.StreamReader(@"list.txt");
while ((line = file.ReadLine()) != null)
{
listBox1.Items.Add(line);
list.Add(line);
counter++;
}
//listBox2.DataSource = list;
MessageBox.Show(counter.ToString());
return list;
}
public void button1_Click(object sender, EventArgs e)
{
listBox2.DataSource = finalList;
}
}
}
'ListBox.Refresh()'しようとしましたか? – devRicher
あなたはVB.NETでそれをやり取りできますか? –
私は答えを投稿しました。それが今あなたのために働くかどうか見てください。 –