"TestArrayList.cs"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ListTest
{
class TestArrayList
{
public static ArrayList array;
}
}
"Form1.cs"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ListTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestArrayList.array = new System.Collections.ArrayList();
this.button1.Enabled = false;
this.button2.Enabled = true;
this.textBox3.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
TestArrayList.array.Add(this.textBox3.Text);
this.textBox1.Text = (TestArrayList.array.Capacity).ToString();
this.textBox2.Text = (TestArrayList.array.Count).ToString();
this.button3.Enabled = true;
this.button4.Enabled = true;
this.button5.Enabled = true;
this.button7.Enabled = true;
//this.textBox3.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
int size = TestArrayList.array.Count;
int index = TestArrayList.array.IndexOf(this.textBox3.Text);
if (index== 0)
{
this.textBox3.Text = TestArrayList.array[size-1].ToString();
}
else
{
this.textBox3.Text = TestArrayList.array[index-1].ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.button2.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
this.button5.Enabled = false;
this.button6.Enabled = false;
this.button7.Enabled = false;
this.textBox3.Enabled = false;
MaximumSize = new Size(350, 264);
MinimumSize = new Size(350, 264);
}
private void button4_Click(object sender, EventArgs e)
{
int size = TestArrayList.array.Count;
int index = TestArrayList.array.IndexOf(this.textBox3.Text);
if (index == size - 1)
{
this.textBox3.Text = TestArrayList.array[0].ToString();
}
else
{
this.textBox3.Text = TestArrayList.array[index + 1].ToString();
}
}
private void button7_Click(object sender, EventArgs e)
{
int index = TestArrayList.array.IndexOf(this.textBox3.Text);
int size = TestArrayList.array.Count;
if (index == -1)
{
MessageBox.Show(this.textBox3.Text + "not exit!");
}
else
{
if (index == size - 1)
{
this.textBox3.Text = TestArrayList.array[0].ToString();
}
else
{
this.textBox3.Text = TestArrayList.array[index + 1].ToString();
}
TestArrayList.array.Remove(TestArrayList.array[index]);
this.textBox1.Text = (TestArrayList.array.Capacity).ToString();
this.textBox2.Text = (TestArrayList.array.Count).ToString();
}
}
private void button5_Click(object sender, EventArgs e)
{
this.button6.Enabled = true;
StreamWriter sw = null;
sw = new StreamWriter("./hello.txt", true, System.Text.Encoding.UTF8);
foreach (string s in TestArrayList.array)
{
sw.WriteLine(s);
}
sw.Flush();
sw.Close();
}
private void button6_Click(object sender, EventArgs e)
{
this.button3.Enabled = true;
this.button4.Enabled = true;
this.button7.Enabled = true;
StreamReader sr = null;
sr = new StreamReader("./hello.txt", System.Text.Encoding.UTF8);
sr.BaseStream.Seek(0, SeekOrigin.Begin);//将文件指针设定到文件开头
string str = sr.ReadLine();//从流中读入一行字符
while (str != null)
{
TestArrayList.array.Add(str);
str = sr.ReadLine();
}
}
}
}