//changes cursor and item background color private void Listbox_entries_MouseMove(object sender, MouseEventArgs e) { int index = Listbox_entries.IndexFromPoint(e.Location.X, e.Location.Y); //Console.WriteLine("Mousemove: " + e.Location.X + " " + e.Location.Y); try { //redraw hover item background if (index != prev_index && index != -1) { //refreshes the rectangle area that contains items at index and prev index Listbox_entries.Invalidate(Listbox_entries.GetItemRectangle(index)); Listbox_entries.Invalidate(Listbox_entries.GetItemRectangle(prev_index)); prev_index = index; } if (index != -1) { if (this.Cursor == Cursors.Arrow) { this.Cursor = Cursors.Hand; } } else { if (this.Cursor == Cursors.Hand) { this.Cursor = Cursors.Arrow; } } } catch (Exception ex) { } }
//draws customized listbox including text color and hover background color private void myListBox_DrawItem(object sender, DrawItemEventArgs e) { Brush myBrush; //changes listbox items text to black if they have media files associated if (media_entry_indices.IndexOf(e.Index) != -1) { myBrush = Brushes.Black; } else { myBrush = Brushes.Gray; } //gets index of listbox item that is being hovered over var relativePoint = Listbox_entries.PointToClient(Cursor.Position); int index = Listbox_entries.IndexFromPoint(relativePoint.X, relativePoint.Y); //if the item state is selected then change the back color if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State ^ DrawItemState.Selected, e.ForeColor, main_color);//Choose the color } //if item is being hovered else if (index == e.Index) { e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State, e.ForeColor, Color.LightGray);//Choose the color } //-1 is invalid index if (e.Index >= 0) { e.DrawBackground(); e.Graphics.DrawString(Listbox_entries.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); } }
//right click only when selecting listbox item private void listBox1_MouseDown(object sender, MouseEventArgs e) { contextMenuStrip1.Hide(); if (e.Button == MouseButtons.Right) { //select the item under the mouse pointer if (Listbox_entries.IndexFromPoint(e.Location).ToString() != "-1") { //selects entry that is right clicked since it isn't selected by default Listbox_entries.SelectedIndex = Listbox_entries.IndexFromPoint(e.Location); load_entry(null, new EventArgs()); contextMenuStrip1.Show(); contextMenu_item_delete.Tag = Listbox_entries.SelectedItem.ToString(); contextMenu_item_delete.Click += delete_entry; } } }
//deletes an image from the selected journal entry private void delete_image(object sender, EventArgs e) { string image_path = contextMenu_item_delete.Tag.ToString(); contextMenu_item_delete.Click -= delete_image; if (File.Exists(image_path)) { //deletes the image and its thumbnail File.Delete(image_path); //File.Delete(Path.ChangeExtension(image_path, "thumb")); } //if no media for entry before, make entry date text black to show now has media string[] existing_images = Directory.GetFiles(path + "/" + Textbox_log.Tag.ToString()); if (existing_images.Length == 1) { get_journal_entries(""); Listbox_entries.Refresh(); } load_entry(null, new EventArgs()); }
//Copyies images that are dragged and dropped void Form1_DragDrop(object sender, DragEventArgs e) { //gets selected journal entry string selected_date = get_selected_date(); //no entry date selected if (selected_date == "") { drag_status.Text = "Please select an entry to add media."; drag_status.Show(); return; } string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string[] existing_images = Directory.GetFiles(path + "/" + selected_date); bool successful = true; foreach (string file in files) { //gets only file name string file_name = Path.GetFileName(file); //makes sure not duplicate bool exists = false; for (int x = 0; x < existing_images.Length; x++) { if (existing_images[x].Contains(file_name)) { exists = true; } } if (exists == false) { //copies only images if (file_name.ToLower().Contains(".jpg") || file_name.ToLower().Contains(".jpeg") || file_name.ToLower().Contains(".png") || file_name.ToLower().Contains(".gif")) { File.Copy(file, path + "/" + selected_date + "/" + file_name); } else { drag_status.Text = "Image needs to be a jpg, png, or gif"; drag_status.Show(); successful = false; } } else { drag_status.Text = "Image \"" + file_name + "\" already exists"; drag_status.Show(); successful = false; } } if (successful) { drag_status.Text = "Image copy successful!"; //if no media for entry before, make entry date text black to show now has media if (existing_images.Length == 1) { get_journal_entries(""); Listbox_entries.Refresh(); } } drag_status.Show(); }