Skip to main content

How to: Handle DiagramControl Events to Save Diagrams to a Database instead of a File System

  • 10 minutes to read

This example demonstrates how to open and save diagrams to a custom storage (e.g., a database) instead of a file system. In the example, the following events are used to implement this functionality:

DiagramControl.ShowingOpenDialog - This event fires before the standard Open dialog is shown and allows you to customize the dialog options or replace the standard dialog with a custom one. You can also cancel the Open operation by setting the e.Cancel parameter to true.

DiagramControl.ShowingSaveDialog - Similarly to the ShowingOpenDialog event, the ShowingSaveDialog event allows you to customize the standard Save dialog in DiagramControl or replace it with a custom one. Setting the e.Cancel parameter to true will cancel the Save operation.

DiagramControl.CustomLoadDocument - This event fires after a user selected a document in the Open dialog or the DiagramControl.DocumentSource property was set in code. The event exposes the selected document source (e.g., a document name or a file stream) through the e.DocumentSource property and allows you to implement your own loading logic. For example, you can retrieve a diagram file from a database and load it into DiagramControl using the DiagramControl.LoadDocument method (as demonstrated in the example) or populate the diagram with items manually. After implementing your custom loading logic, set the e.Handled parameter to true, so that DiagramControl does not load the previously selected document source.

DiagramControl.CustomSaveDocument - This event allows you to specify custom saving logic for your diagram. The event fires after the Save operation was initiated and selection was made in the Save dialog (if there was a dialog). The e.DocumentSource property in the event args specifies the default location (file name, stream, etc.) where the diagram will be saved. You can set the e.Handled parameter to true to cancel the standard saving logic and implement your custom one. For example, save the diagram to a stream using the DiagramControl.SaveDocument method as demonstrated in the example or iterate through diagram items manually and read required information.

View Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace XtraDiagram.CustomDiagramStorage {
    public partial class DiagramOpenDialog : DevExpress.XtraEditors.XtraForm {
        public string SelectedItem { get; set; }

        public DiagramOpenDialog() {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            PopulateListBox();
        }

        private void PopulateListBox() {
            if (!DesignMode)
                listBoxControl1.DataSource = DiagramRepository.GetDiagramNames();
        }

        private void listBoxControl1_MouseDoubleClick(object sender, MouseEventArgs e) {
            var itemIndex = listBoxControl1.IndexFromPoint(e.Location);
            if (itemIndex > -1) {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e) {
            OnSelectedItemChanged();
        }

        protected virtual void OnSelectedItemChanged() {
            SelectedItem = listBoxControl1.SelectedItem as string;
        }
    }
}