Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
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.
ImportsSystemImportsSystem.Collections.GenericImportsSystem.ComponentModelImportsSystem.DataImportsSystem.DrawingImports System.TextImportsSystem.LinqImportsSystem.Windows.FormsImportsDevExpress.XtraEditorsNamespace XtraDiagram.CustomDiagramStorage
PartialPublicClass DiagramOpenDialog
Inherits DevExpress.XtraEditors.XtraForm
PublicProperty SelectedItem() AsStringPublicSubNew()
InitializeComponent()
EndSubProtectedOverridesSub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
PopulateListBox()
EndSubPrivateSub PopulateListBox()
IfNot DesignMode Then
listBoxControl1.DataSource = DiagramRepository.GetDiagramNames()
EndIfEndSubPrivateSub listBoxControl1_MouseDoubleClick(ByVal sender AsObject, ByVal e As MouseEventArgs) Handles listBoxControl1.MouseDoubleClick
Dim itemIndex = listBoxControl1.IndexFromPoint(e.Location)
If itemIndex > -1Then
DialogResult = DialogResult.OK
Close()
EndIfEndSubPrivateSub listBoxControl1_SelectedIndexChanged(ByVal sender AsObject, ByVal e As EventArgs) Handles listBoxControl1.SelectedIndexChanged
OnSelectedItemChanged()
EndSubProtectedOverridableSub OnSelectedItemChanged()
SelectedItem = TryCast(listBoxControl1.SelectedItem, String)
EndSubEndClassEndNamespace
ImportsSystemImportsSystem.Collections.GenericImportsSystem.ComponentModelImportsSystem.DataImportsSystem.DrawingImportsSystem.IOImportsSystem.LinqImports System.TextImportsSystem.Windows.FormsImportsSystem.Windows.ThreadingImportsDevExpress.Diagram.CoreImportsDevExpress.XtraDiagramNamespace XtraDiagram.CustomDiagramStorage
PartialPublicClass Form1
Inherits DevExpress.XtraBars.Ribbon.RibbonForm
PublicSubNew()
InitializeComponent()
EndSubProtectedOverridesSub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
diagramControl1.InitializeRibbon(ribbonControl1)
diagramControl1.OpenFile()
EndSubPrivateSub OnShowingOpenDialog(ByVal sender AsObject, ByVal e As DiagramShowingOpenDialogEventArgs) Handles diagramControl1.ShowingOpenDialog
Dim diagramName = DiagramSelector.SelectDiagramToOpen()
If diagramName IsNotNothingThen
e.DocumentSourceToOpen = diagramName
Else
e.Cancel = TrueEndIfEndSubPrivateSub OnCustomLoadDocument(ByVal sender AsObject, ByVal e As DiagramCustomLoadDocumentEventArgs) Handles diagramControl1.CustomLoadDocument
If e.DocumentSource IsNothingThen
diagramControl1.NewDocument()
Text = "(New Document)"ReturnEndIfDim diagramName = CStr(e.DocumentSource)
Text = diagramName
Dim diagramData = DiagramRepository.GetDiagramData(diagramName)
If diagramData IsNotNothingThen
diagramControl1.LoadDocument(New MemoryStream(diagramData))
EndIf
e.Handled = TrueEndSubPrivateSub OnShowingSaveDialog(ByVal sender AsObject, ByVal e As DiagramShowingSaveDialogEventArgs) Handles diagramControl1.ShowingSaveDialog
Dim diagramName = DiagramSelector.SelectDiagramToSave()
If diagramName IsNotNothingThen
e.DocumentSourceToSave = diagramName
Else
e.Cancel = TrueEndIfEndSubPrivateSub OnCustomSaveDocument(ByVal sender AsObject, ByVal e As DiagramCustomSaveDocumentEventArgs) Handles diagramControl1.CustomSaveDocument
Dim diagramName = CStr(e.DocumentSource)
Text = diagramName
Dim stream = New MemoryStream()
diagramControl1.SaveDocument(stream)
Dim diagramData = stream.ToArray()
DiagramRepository.SaveDiagramData(diagramName, diagramData)
e.Handled = TrueEndSubEndClassEndNamespace
ImportsSystemImportsSystem.Collections.GenericImportsSystem.Data.EntityImportsSystem.LinqImports System.TextImportsSystem.Threading.TasksNamespace XtraDiagram.CustomDiagramStorage
PublicClass DiagramStorage
Inherits DbContext
PublicProperty DiagramData() As DbSet(Of DiagramData)
EndClassPublicClass DiagramData
PublicProperty Id() AsIntegerPublicProperty Name() AsStringPublicProperty Data() AsByte()
EndClassPublicClass DiagramRepository
PublicSharedFunction GetDiagramNames() As IEnumerable(OfString)
Dim storage = New DiagramStorage()
Return storage.DiagramData.Select(Function(x) x.Name).ToList()
EndFunctionPublicSharedFunction GetDiagramData(ByVal diagramName AsString) AsByte()
Dim storage = New DiagramStorage()
Return storage.DiagramData.FirstOrDefault(Function(x) x.Name = diagramName).Data
EndFunctionPublicSharedSub SaveDiagramData(ByVal diagramName AsString, ByVal diagramData() AsByte)
Dim storage = New DiagramStorage()
Dim diagramInfo = storage.DiagramData.FirstOrDefault(Function(x) x.Name = diagramName)
If diagramInfo IsNothingThen
diagramInfo = New DiagramData() With {.Name = diagramName}
storage.DiagramData.Add(diagramInfo)
EndIf
diagramInfo.Data = diagramData
storage.SaveChanges()
EndSubEndClassEndNamespace
ImportsDevExpress.Diagram.CoreImportsSystemImportsSystem.Collections.GenericImportsSystem.Data.EntityImportsSystem.IOImportsSystem.LinqImports System.TextImportsSystem.Threading.TasksImportsSystem.WindowsImportsDevExpress.XtraDiagramNamespace XtraDiagram.CustomDiagramStorage
FriendClass DiagramStorageInitializer
Inherits DropCreateDatabaseIfModelChanges(Of DiagramStorage)
ProtectedOverridesSub Seed(ByVal storage As DiagramStorage)
MyBase.Seed(storage)
Dim diagram = New DiagramControl()
For i AsInteger = 0To4
diagram.Items.Add(New DiagramShape() With {.Position = New DevExpress.Utils.PointFloat(200, 100 + i * 100), .Width = 100, .Height = 50, .Content = "Item " & (i + 1).ToString()})
If i = 0ThenContinueForEndIf
diagram.SelectedStencils = New StencilCollection(NewString() { DiagramToolboxRegistrator.Stencils.ElementAt(i).Id })
Using stream = New MemoryStream()
diagram.SaveDocument(stream)
Dim diagramData = New DiagramData() With {.Name = (i + 1).ToString() & " items", .Data = stream.ToArray()}
storage.DiagramData.Add(diagramData)
EndUsingNext i
storage.SaveChanges()
EndSubEndClassEndNamespace
ImportsSystemImportsSystem.Collections.GenericImportsSystem.LinqImports System.TextImportsSystem.Windows.FormsNamespace XtraDiagram.CustomDiagramStorage
PublicClass DiagramSelector
PublicSharedFunction SelectDiagramToOpen() AsStringDim selector = New DiagramOpenDialog() With {.Text = "Choose a diagram to open"}
Return ShowDialogCore(selector)
EndFunctionPublicSharedFunction SelectDiagramToSave() AsStringDim selector = New DiagramSaveDialog() With {.Text = "Choose a save location"}
Return ShowDialogCore(selector)
EndFunctionProtectedSharedFunction ShowDialogCore(ByVal dialog As DiagramOpenDialog) AsString
dialog.ShowDialog()
ReturnIf(dialog.DialogResult = DialogResult.OK, dialog.SelectedItem, Nothing)
EndFunctionEndClassEndNamespace
ImportsSystemImportsSystem.Collections.GenericImportsSystem.Data.EntityImportsSystem.LinqImportsSystem.Windows.FormsImports DevExpress.Internal
Namespace XtraDiagram.CustomDiagramStorage
FriendNotInheritableClass Program
PrivateSubNew()
EndSub'''<summary>''' The main entry point for the application.'''</summary>
<STAThread> _
SharedSub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
DbEngineDetector.PatchConnectionStringsAndConfigureEntityFrameworkDefaultConnectionFactory()
Database.SetInitializer(New DiagramStorageInitializer())
Application.Run(New Form1())
EndSubEndClassEndNamespace
Was this page helpful?
Thanks for your feedback!
How can we improve this help topic?
Additional comments/thoughts:
If you have any questions, submit a ticket to our Support Center.