listbox怎么读取TXT文件,一行一个
展开全部
逐行读取txt并添加到listbox1中:
private void button3_Click(object sender, EventArgs e)
{
StreamReader file = new StreamReader(@"c:\IP.txt",System.Text.Encoding.Default);
string s = "";
while (s != null)
{
s = file.ReadLine();
if (s != null && !s.Equals(""))
listBox1.Items.Add(s);
}
file.Close();
}
随机选择listbox1中的某一个值加到listbox2中:
using System.Collections;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
randomNm(0, listBox1.Items.Count);
}
}
private void randomNm(int n, int m)
{
if(m>=n)
{
Random random = new Random();
int i = random.Next(n, m);
listBox2.Items.Add(listBox1.Items[i]);
}
}
private void button3_Click(object sender, EventArgs e)
{
StreamReader file = new StreamReader(@"c:\IP.txt",System.Text.Encoding.Default);
string s = "";
while (s != null)
{
s = file.ReadLine();
if (s != null && !s.Equals(""))
listBox1.Items.Add(s);
}
file.Close();
}
随机选择listbox1中的某一个值加到listbox2中:
using System.Collections;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
randomNm(0, listBox1.Items.Count);
}
}
private void randomNm(int n, int m)
{
if(m>=n)
{
Random random = new Random();
int i = random.Next(n, m);
listBox2.Items.Add(listBox1.Items[i]);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询