Exemplo n.º 1
0
        private void GetSnippets()
        {
            var xmlfi = new FileInfo("snippets.xml");

            if (xmlfi.Exists)
            {
                var serializer = new XmlSerializer(typeof(List <TextSnippet>));
                if (xmlfi.Length > 0)
                {
                    using (var sr = new StreamReader(xmlfi.OpenRead()))
                    {
                        Snippets = (List <TextSnippet>)serializer.Deserialize(sr);
                    }
                }
                return;
            }

            //Old snippets text file...
            var fi = new FileInfo("snippets.txt");

            if (!fi.Exists)
            {
                return;
            }
            using (var sr = new StreamReader(fi.OpenRead()))
            {
                var s = sr.ReadLine();
                while (s != null)
                {
                    try
                    {
                        var idx = s.IndexOf(",", StringComparison.Ordinal);
                        if (idx > -1)
                        {
                            var splits = new string[2];
                            splits[0] = s.Substring(0, idx);
                            splits[1] = s.Substring(idx + 1);
                            var snip = new TextSnippet()
                            {
                                Name = splits[0],
                                Text = splits[1].Replace(@"\r", "\r").Replace(@"\n", "\n").Replace(@"\t", "\t")
                            };
                            Snippets.Add(snip);
                        }
                        s = sr.ReadLine();
                    }
                    catch
                    {
                        //
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void BuildSnippetMenu()
        {
            wizardToolStripMenuItem.DropDownItems.Clear();
            foreach (var item in Snippets)
            {
                var theItem = item;
                var ts      = new ToolStripMenuItem(item.Name, null, delegate(object sender, EventArgs args)
                {
                    if (ModifierKeys == Keys.Control)
                    {
                        var checkTs = (ToolStripMenuItem)sender;

                        var dr = MessageBox.Show(
                            string.Format("Remove item \"{0}\"?", checkTs.Text),
                            "Remove Item",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question);

                        if (dr == DialogResult.Yes)
                        {
                            wizardToolStripMenuItem.DropDownItems.Remove(checkTs);
                            Snippets.Remove((TextSnippet)checkTs.Tag);
                        }
                    }
                    else
                    {
                        textBox1.InsertText(textBox1.SelectionStart, theItem.Text);
                    }
                })
                {
                    Tag = item
                };
                wizardToolStripMenuItem.DropDownItems.Add(ts);
            }

            var insertFieldSnippet = new ToolStripMenuItem("Insert Field List", null, delegate
            {
                InsertFields();
            });

            var addSnippet = new ToolStripMenuItem("Add Snippet", null, delegate
            {
                if (textBox1.SelectedText.Length == 0)
                {
                    MessageBox.Show("You might want to try selecting some text.", "Select some Text",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                var frmName = new FrmNameInput();
                frmName.ShowDialog();

                if (string.IsNullOrEmpty(frmName.UserName))
                {
                    return;
                }

                var snippet = new TextSnippet()
                {
                    Name = frmName.UserName,
                    Text = textBox1.SelectedText
                };
                Snippets.Add(snippet);
                BuildSnippetMenu();
            });

            wizardToolStripMenuItem.DropDownItems.Add(insertFieldSnippet);
            wizardToolStripMenuItem.DropDownItems.Add(addSnippet);
        }