// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; using System.IO; using System.Drawing; using System.Windows.Forms; namespace DefaultNamespace { public class MainForm : Form { private ListBox listBox1; private Button button1; private Button button2; public MainForm() { InitializeComponent(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } private void InitializeComponent() { listBox1 = new ListBox(); button1 = new Button(); button2 = new Button(); SuspendLayout(); listBox1.Location = new Point(50, 50); listBox1.Size = new Size(150, 300); StreamReader sr = new StreamReader(@"C:\data.txt"); string line; while((line = sr.ReadLine()) != null) { listBox1.Items.Add(line); } sr.Close(); button1.Location = new Point(50, 450); button1.Size = new Size(50, 25); button1.Text = "Tilføj"; button1.Click += new EventHandler(Button1Click); button2.Location = new Point(150, 450); button2.Size = new Size(50, 25); button2.Text = "Gem"; button2.Click += new EventHandler(Button2Click); ClientSize = new Size(250, 500); Controls.Add(listBox1); Controls.Add(button1); Controls.Add(button2); Text = "Listbox test"; ResumeLayout(false); } private void Button1Click(object sender, EventArgs e) { listBox1.Items.Add("Dette er en tilføjet linie"); } private void Button2Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(@"C:\data.txt"); foreach(object item in listBox1.Items) { sw.WriteLine(item); } sw.Close(); } } }