// show context menu when user presses the right button private void _flex_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { // hide current note, if any if (_note != null) { _note.HideNote(); _note = null; } // make sure user clicked a valid cell int row = _flex.MouseRow; int col = _flex.MouseCol; if (row >= _flex.Rows.Fixed && col >= _flex.Cols.Fixed) { // select cell that was clicked _flex.Select(row, col); // update context menu bool hasNote = GetNoteAtMouse() != null; _mInsertNote.Visible = !hasNote; _mEditNote.Visible = hasNote; _mDeleteNote.Visible = hasNote; // show the menu _menuActive = true; _menu.Show(_flex, new Point(e.X, e.Y)); //Application.DoEvents(); _menuActive = false; } } }
// show all notes on the grid // (this could be used to persist the notes in a file for example) void _btnShowNotes_Click(object sender, System.EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append("** Cell Notes:"); int count = 0; for (int r = 0; r < _flex.Rows.Count; r++) { for (int c = 0; c < _flex.Cols.Count; c++) { CellRange rg = _flex.GetCellRange(r, c); CellNote note = rg.UserData as CellNote; if (note != null) { count++; sb.Append("\r\n-----------------------------\r\n"); sb.AppendFormat("Notes for cell {0}, {1}: (form size is {2} by {3})\r\n", r, c, note.Width, note.Height); sb.Append(note.NoteText); } } } if (count == 0) { sb.Append("\r\nThis form contains no notes."); } MessageBox.Show(sb.ToString()); }
void Form1_Load(object sender, System.EventArgs e) { // load some data string conn = GetConnectionString(); string sql = "select * from employees"; OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); // bind to grid _flex.DataSource = dt; // hide Notes column (we'll use CellNotes instead) _flex.Cols["Notes"].Visible = false; // create cell notes for every employee int noteColumn = _flex.Cols["FirstName"].Index; for (int r = _flex.Rows.Fixed; r < _flex.Rows.Count; r++) { // create note CellNote note = new CellNote(_flex[r, "Notes"] as string); // attach note to "FirstName" column CellRange rg = _flex.GetCellRange(r, noteColumn); rg.UserData = note; } // create manager to display/edit the cell notes CellNoteManager mgr = new CellNoteManager(_flex); }
private void _mInsertNote_Click(object sender, System.EventArgs e) { // create note CellNote note = new CellNote("Note: "); // attach note to cell CellRange rg = _flex.Selection; rg.UserData = note; _flex.Invalidate(); // go edit the new note _mEditNote_Click(sender, e); }
//------------------------------------------------------------------------------------------------------ #region ** menu event handlers private void _mDeleteNote_Click(object sender, System.EventArgs e) { // get note CellRange rg = _flex.Selection; CellNote note = rg.UserData as CellNote; // clear note if (note != null) { rg.UserData = null; _flex.Invalidate(); } // resume showing notes _menuActive = false; }
private void _mEditNote_Click(object sender, System.EventArgs e) { // get note CellRange rg = _flex.Selection; CellNote note = rg.UserData as CellNote; // edit note if (note != null) { Rectangle r = _flex.GetCellRect(rg.r1, rg.c1); r = _flex.RectangleToScreen(r); note.EditNote(r); } // resume showing notes _menuActive = false; }
//------------------------------------------------------------------------------------------------------ #region ** grid event handlers // show/hide cell notes private void _flex_MouseHoverCell(object sender, System.EventArgs e) { // hide current note, if any if (_note != null) { _note.HideNote(); _note = null; } // don't show note if the grid is not focused or if the context menu is active if (!_flex.Focused || _menuActive) { return; } // show new note, if any _note = GetNoteAtMouse(); if (_note != null) { Rectangle r = _flex.GetCellRect(_flex.MouseRow, _flex.MouseCol); r = _flex.RectangleToScreen(r); _note.ShowNote(r); } }