-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForm4.cs
182 lines (153 loc) · 6.11 KB
/
Form4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Data.SQLite;
using System.Windows.Forms;
namespace Code_Snippet_Manager
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
#region DELETE button
private void button1_Click(object sender, EventArgs e)
{
//check whether any searched item is selected or not
if (string.IsNullOrEmpty(search_result_listbox.GetItemText(search_result_listbox.SelectedItem)))
{
MessageBox.Show("First select something!!");
return;
}
//ask for confirmation before deletion
DialogResult result = MessageBox.Show("Are you sure don't want it? Proceed Deletion ?", "Confirmation", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
SQLiteConnection Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM snippet WHERE (Name LIKE @Name)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
cmd.Parameters.AddWithValue("@Name", search_result_listbox.SelectedItem.ToString());
Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();
//update the list box
search_result_listbox.Items.Remove(search_result_listbox.SelectedItem);
}
else
{
return;
}
}
#endregion
#region HOME button
private void home_b_Click(object sender, EventArgs e)
{
this.Dispose();
}
#endregion
#region SEARCH button
private void search_b_Click(object sender, EventArgs e)
{
//check whether the title is empty or not
if (String.IsNullOrEmpty(title_txt.Text))
{
MessageBox.Show("Enter Title!! ");
return;
}
SQLiteConnection Conn;
SQLiteCommand cmd;
//check whether the database already exist in the target machine
if (System.IO.File.Exists("Snippetdb.sqlite") == false)
{
SQLiteConnection.CreateFile("Snippetdb.sqlite");
Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
//connect to database
Conn.Open();
string query = "create table snippet (Name TEXT, Code TEXT)";
cmd = new SQLiteCommand(query, Conn);
cmd.ExecuteNonQuery();
//close conection
Conn.Close();
}
//clear the list box
search_result_listbox.Items.Clear();
Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
cmd = new SQLiteCommand("SELECT* FROM snippet WHERE (Name LIKE @Name)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
cmd.Parameters.AddWithValue("@Name", "%" + title_txt.Text + "%");
Conn.Open();
cmd.ExecuteNonQuery();
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.Read() == false)
MessageBox.Show("No results Found!!!");
while (reader.Read())
{
search_result_listbox.Items.Add(reader["Name"]);
}
Conn.Close();
}
#endregion
private void preview_snippet_b_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(search_result_listbox.GetItemText(search_result_listbox.SelectedItem)))
{
MessageBox.Show("First select something!!");
return;
}
Form5 f5 = new Form5(search_result_listbox.SelectedItem.ToString());
f5.ShowDialog();
}
/// <summary>
/// for displaying the code snippet of the currently selected title from the list box
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void search_result_listbox_Click(object sender, EventArgs e)
{
SQLiteConnection Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
SQLiteCommand cmd = new SQLiteCommand("SELECT* FROM snippet WHERE (Name = @Name)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
cmd.Parameters.AddWithValue("@Name", search_result_listbox.SelectedItem.ToString());
Conn.Open();
cmd.ExecuteNonQuery();
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//giving rich text box its contents
richTextBox1.Text = reader["Code"].ToString();
}
Conn.Close();
}
#region ALL SNIPPETS button
private void button2_Click(object sender, EventArgs e)
{
SQLiteConnection Conn;
SQLiteCommand cmd;
//check whether the database already exist in the target machine
if (System.IO.File.Exists("Snippetdb.sqlite") == false)
{
MessageBox.Show("No Snippets to show !");
return;
}
//clear the list box
search_result_listbox.Items.Clear();
Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
cmd = new SQLiteCommand("SELECT* FROM snippet");
// cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
Conn.Open();
cmd.ExecuteNonQuery();
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.Read() == false)
MessageBox.Show("No results Found!!!");
while (reader.Read())
{
search_result_listbox.Items.Add(reader["Name"]);
}
Conn.Close();
}
#endregion
}
}