VintageRice

[C#] - MultiClip (Main)

Feb 17th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace clipKeep
  13. {
  14.     public partial class mainForm : Form
  15.     {
  16.         public mainForm()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.        
  21.         //Holds all of the strings, allows for a search function with one listbox
  22.         List<string> allItems = new List<string>();
  23.  
  24.         //Where the save file is stored
  25.         string listLocation = Directory.GetCurrentDirectory() + "/savedList.ini";
  26.  
  27.         //How the strings are split. Not reading line by line because some C&P's may have multiple lines
  28.         string splitVar = "[][][][][]";
  29.  
  30.         //Base variable for the saving
  31.         string doBuild = "";
  32.  
  33.         private void mainForm_Load(object sender, EventArgs e)
  34.         {
  35.             //If the file doesn't exist; then create one
  36.             if (!File.Exists(listLocation))
  37.             {
  38.                 var tempOpen = File.Create(listLocation);
  39.                 tempOpen.Close();
  40.  
  41.                 MessageBox.Show("Couldn't find a saved list; created new one!", "MultiClip");
  42.  
  43.                 allItems.Add("Created by vRice");
  44.                 allItems.Add("http://www.youtube.com/vintagerice");
  45.  
  46.                 updateProgram();
  47.             }
  48.             else
  49.             {
  50.                 //Splits with a string rather than a char
  51.                 string[] splitText = File.ReadAllText(listLocation).Split(new string[] { splitVar }, StringSplitOptions.None);
  52.  
  53.                 //Adds all items to the listbox
  54.                 foreach (string newItem in splitText)
  55.                 {
  56.                     if(newItem != "")
  57.                     {
  58.                         allItems.Add(newItem);
  59.                         updateProgram();
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.  
  65.         //'Brain' of the program, monitors all clipboard content
  66.         private void checkTimer_Tick(object sender, EventArgs e)
  67.         {
  68.             string newText = Clipboard.GetText();
  69.  
  70.             if (!itemList.Items.Contains(newText))
  71.             {
  72.                 if (newText != "")
  73.                 {
  74.                     allItems.Add(newText);
  75.                     updateProgram();
  76.                 }
  77.             }
  78.         }
  79.  
  80.         public void updateProgram()
  81.         {
  82.             itemList.Items.Clear();
  83.             Clipboard.Clear();
  84.  
  85.             doBuild = "";
  86.  
  87.             foreach (string newItem in allItems)
  88.             {
  89.                 //Adds each string to the variable with a split between them
  90.                 itemList.Items.Add(newItem);
  91.                 doBuild += (newItem + splitVar);
  92.             }
  93.  
  94.             // Writes the string to the save file
  95.             if (File.Exists(listLocation))
  96.                 File.WriteAllText(listLocation, doBuild);
  97.         }
  98.  
  99.         private void delButton_Click(object sender, EventArgs e)
  100.         {
  101.             if (itemList.SelectedIndex >= 0)
  102.             {
  103.                 allItems.RemoveAt(itemList.SelectedIndex);
  104.                 updateProgram();
  105.             }
  106.         }
  107.  
  108.         private void clearButton_Click(object sender, EventArgs e)
  109.         {
  110.             DialogResult userConfirm = MessageBox.Show("Are you sure you want to delete all items?", "multiClip", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  111.  
  112.             //If user selects yes then delete everything
  113.             if(userConfirm == DialogResult.Yes)
  114.             {
  115.                 allItems.Clear();
  116.                 updateProgram();
  117.             }
  118.         }
  119.  
  120.         private void searchBox_TextChanged(object sender, EventArgs e)
  121.         {
  122.             if(searchBox.Text!= "Search")
  123.             {
  124.                 itemList.Items.Clear();
  125.  
  126.                 //If the search box contains anything that's in the item list; show the items
  127.                 foreach(string toFind in allItems)
  128.                     if (toFind.ToLower().Contains(searchBox.Text.ToLower()))
  129.                         if (!itemList.Items.Contains(toFind))
  130.                             itemList.Items.Add(toFind);
  131.             }
  132.         }
  133.  
  134.         private void expandButton_Click(object sender, EventArgs e)
  135.         {
  136.             if(itemList.SelectedIndex >= 0)
  137.             {
  138.                 //Open a new form that passes on the selected item string
  139.                 expandForm openForm = new expandForm(this.itemList.SelectedItem.ToString());
  140.                 openForm.ShowDialog();
  141.  
  142.                 //Return the data from the other form if it isn't empty
  143.                 if(openForm.textData() != "")
  144.                 {
  145.                     allItems.Remove(itemList.SelectedItem.ToString());
  146.                     allItems.Add(openForm.textData());
  147.  
  148.                     updateProgram();
  149.                 }
  150.             }
  151.         }
  152.  
  153.         private void mainForm_Resize(object sender, EventArgs e)
  154.         {
  155.             //Make the program responsive
  156.             itemList.Height = this.Height - 70;
  157.             itemList.Width = this.Width - 180;
  158.         }
  159.  
  160.         private void clearBox(object sender, EventArgs e)
  161.         {
  162.             searchBox.Text = "";
  163.         }
  164.  
  165.         private void itemList_MouseDoubleClick(object sender, MouseEventArgs e)
  166.         {
  167.             Uri uriResult;
  168.             bool urlCheck = Uri.TryCreate(itemList.SelectedItem.ToString(), UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
  169.  
  170.             //If the item is a link; launch it
  171.             if (urlCheck)
  172.                 System.Diagnostics.Process.Start(itemList.SelectedItem.ToString());
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment