Advertisement
alien_fx_fiend

Saving ListBox items in C#

Jul 25th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System.IO;
  2. using System.Diagnostics;
  3. namespace WindowsFormsApplication1
  4. {
  5.     public partial class MainForm : Form
  6.     {
  7.     public MainForm()
  8.     {
  9.    
  10.         InitializeComponent();
  11.     }
  12. private void btnSubmit_Click(object sender, EventArgs e)
  13. {
  14.     listBox.Items.Add(txtInput.Text)
  15.     txtInput.Clear();
  16. }
  17. private void btnSaveToFile_Click(object sender, EventArgs e)
  18. {
  19.     if (listBox.Items.Count > 0)
  20. {
  21.     {
  22.     using (TextWriter TW = new StreamWriter("list.txt"))
  23.     {
  24.         foreach (string itemText in listBox.items)
  25.     {
  26.             TW.WriteLine(itemText);
  27.     }
  28. }
  29.     Process.Start("list.txt")
  30. }
  31. private void btnCopyToClip_Click(object sender. EventArgs e)
  32. {
  33.     if (listBox.Items.Count > 0)
  34.     {
  35.         StringBuilder SB = new StringBuilder();
  36.         foreach (string itemValue in listBox.Items)
  37.         {
  38.         SB.AppendLine(itemValue);
  39.         }
  40.  
  41.     //Further process text here, ex remove last newline https://www.youtube.com/watch?v=aIeiVfzJ1sU
  42.     string result = SB.ToString().TrimEnd('\n');
  43.  
  44.     Clipboard.SetText(result);
  45.     }
  46. }
  47. }
  48. }
  49. // textinputbox, submit, listbox, copytoclipboard, savetofile.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement